@Override public long skip(long n) throws IOException { if (mClosed) { throw new IOException("Cannot skip bytes in a closed stream."); } return mInputStream.skip(n); } }
/** * Tests that skipping a negative amount correctly reports that 0 bytes were skipped. */ @Test public void skipNegative() throws IOException { assertEquals(0, mTestStream.skip(-10)); }
@Override protected void runPlainPath(AlluxioURI path, CommandLine cl) throws AlluxioException, IOException { URIStatus status = mFileSystem.getStatus(path); int numOfBytes = Constants.KB; if (cl.hasOption('c')) { numOfBytes = (int) FormatUtils.parseSpaceSize(cl.getOptionValue('c')); Preconditions.checkArgument(numOfBytes > 0, "specified bytes must be > 0"); } if (status.isFolder()) { throw new IOException(ExceptionMessage.PATH_MUST_BE_FILE.getMessage(path)); } OpenFilePOptions options = OpenFilePOptions.getDefaultInstance(); try (FileInStream is = mFileSystem.openFile(path, options)) { byte[] buf = new byte[numOfBytes]; long bytesToRead; if (status.getLength() > numOfBytes) { bytesToRead = numOfBytes; } else { bytesToRead = status.getLength(); } is.skip(status.getLength() - bytesToRead); int read = is.read(buf); if (read != -1) { System.out.write(buf, 0, read); } } }
/** * Tests skip, particularly that skipping the start of a block will cause us not to cache it, and * cancels the existing cache stream. */ @Test public void testSkip() throws IOException { int skipAmount = (int) (BLOCK_LENGTH / 2); int readAmount = (int) (BLOCK_LENGTH * 2); byte[] buffer = new byte[readAmount]; // Skip halfway into block 1 mTestStream.skip(skipAmount); // Read two blocks from 0.5 to 2.5 mTestStream.read(buffer); assertArrayEquals(BufferUtils.getIncreasingByteArray(skipAmount, readAmount), buffer); assertEquals(0, mTestStream.skip(0)); // Skip the next half block, bringing us to block 3 assertEquals(BLOCK_LENGTH / 2, mTestStream.skip(BLOCK_LENGTH / 2)); assertEquals(BufferUtils.byteToInt((byte) (BLOCK_LENGTH * 3)), mTestStream.read()); }
int len = (int) Math.min(5 * Constants.KB, status.getLength() - offset); byte[] data = new byte[len]; long skipped = is.skip(offset); if (skipped < 0) {
/** * Tests that {@link FileInStream#remaining()} is correctly updated during reads, skips, and * seeks. */ @Test public void testRemaining() throws IOException { assertEquals(FILE_LENGTH, mTestStream.remaining()); mTestStream.read(); assertEquals(FILE_LENGTH - 1, mTestStream.remaining()); mTestStream.read(new byte[150]); assertEquals(FILE_LENGTH - 151, mTestStream.remaining()); mTestStream.skip(140); assertEquals(FILE_LENGTH - 291, mTestStream.remaining()); mTestStream.seek(310); assertEquals(FILE_LENGTH - 310, mTestStream.remaining()); mTestStream.seek(130); assertEquals(FILE_LENGTH - 130, mTestStream.remaining()); }
@Override public long skip(long n) throws IOException { if (mClosed) { throw new IOException("Cannot skip bytes in a closed stream."); } return mInputStream.skip(n); } }
int len = (int) Math.min(5 * Constants.KB, status.getLength() - offset); byte[] data = new byte[len]; long skipped = is.skip(offset); if (skipped < 0) {