/** * Get the current position in the input stream. * * @return current position in the input stream */ @Override public long getPos() throws IOException { return ((Seekable)in).getPos(); }
/** * Seek to the given offset. * * @param desired offset to seek to */ @Override public void seek(long desired) throws IOException { ((Seekable)in).seek(desired); }
/** * Seek to the given position on an alternate copy of the data. * * @param targetPos position to seek to * @return true if a new source is found, false otherwise */ @Override public boolean seekToNewSource(long targetPos) throws IOException { return ((Seekable)in).seekToNewSource(targetPos); }
/** * If input stream is {@link org.apache.hadoop.fs.Seekable}, return it's * current position, otherwise return 0; */ public static long getInputStreamOffset(InputStream in) throws IOException { if (in instanceof Seekable) { return ((Seekable) in).getPos(); } return 0; } }
/** * Creates CompressionInputStream to be used to read off uncompressed data * in one of the two reading modes. i.e. Continuous or Blocked reading modes * * @param seekableIn The InputStream * @param start The start offset into the compressed stream * @param end The end offset into the compressed stream * @param readMode Controls whether progress is reported continuously or * only at block boundaries. * * @return CompressionInputStream for BZip2 aligned at block boundaries */ public SplitCompressionInputStream createInputStream(InputStream seekableIn, Decompressor decompressor, long start, long end, READ_MODE readMode) throws IOException { if (!(seekableIn instanceof Seekable)) { throw new IOException("seekableIn must be an instance of " + Seekable.class.getName()); } ((Seekable)seekableIn).seek(start); return new BZip2CompressionInputStream(seekableIn, start, end, readMode); }
@Override public boolean seekToNewSource(long targetPos) throws IOException { Preconditions.checkArgument(targetPos >= 0, "Cannot seek to negative offset."); checkStream(); try { boolean result = ((Seekable) in).seekToNewSource(targetPos); resetStreamOffset(targetPos); return result; } catch (ClassCastException e) { throw new UnsupportedOperationException("This stream does not support " + "seekToNewSource."); } }
/** * This method returns the current position in the stream. * * @return Current position in stream as a long */ @Override public long getPos() throws IOException { if (!(in instanceof Seekable) || !(in instanceof PositionedReadable)){ //This way of getting the current position will not work for file //size which can be fit in an int and hence can not be returned by //available method. return (this.maxAvailableData - this.in.available()); } else{ return ((Seekable)this.in).getPos(); } }
/** Seek to a position. */ @Override public void seek(long pos) throws IOException { if (pos < 0) { throw new EOFException(FSExceptionMessages.NEGATIVE_SEEK); } checkStream(); try { /* * If data of target pos in the underlying stream has already been read * and decrypted in outBuffer, we just need to re-position outBuffer. */ if (pos <= streamOffset && pos >= (streamOffset - outBuffer.remaining())) { int forward = (int) (pos - (streamOffset - outBuffer.remaining())); if (forward > 0) { outBuffer.position(outBuffer.position() + forward); } } else { ((Seekable) in).seek(pos); resetStreamOffset(pos); } } catch (ClassCastException e) { throw new UnsupportedOperationException("This stream does not support " + "seek."); } }
@Override public boolean seekToNewSource(long targetPos) throws IOException { return ((Seekable) in).seekToNewSource(targetPos); } }
private synchronized long getFilePosition() throws IOException { long retVal; if (isCompressedInput() && filePosition != null) { retVal = filePosition.getPos(); } else { retVal = getPos(); } return retVal; }
@Override public ByteBuffer read(ByteBufferPool bufferPool, int maxLength, EnumSet<ReadOption> opts) throws IOException, UnsupportedOperationException { checkStream(); try { if (outBuffer.remaining() > 0) { // Have some decrypted data unread, need to reset. ((Seekable) in).seek(getPos()); resetStreamOffset(getPos()); } final ByteBuffer buffer = ((HasEnhancedByteBufferAccess) in). read(bufferPool, maxLength, opts); if (buffer != null) { final int n = buffer.remaining(); if (n > 0) { streamOffset += buffer.remaining(); // Read n bytes final int pos = buffer.position(); decrypt(buffer, n, pos); } } return buffer; } catch (ClassCastException e) { throw new UnsupportedOperationException("This stream does not support " + "enhanced byte buffer access."); } }
@Override public boolean seekToNewSource(long targetPos) throws IOException { return ((Seekable) in).seekToNewSource(targetPos); } }
@Override public long getPos() throws IOException { return ((Seekable) in).getPos(); }
@Override public void seek(long pos) throws IOException { ((Seekable) in).seek(pos); }
@Override public boolean seekToNewSource(long targetPos) throws IOException { return ((Seekable) in).seekToNewSource(targetPos); } }
@Override public long getPos() throws IOException { return ((Seekable) in).getPos(); }
@Override public void seek(long pos) throws IOException { ((Seekable) in).seek(pos); }
@Override public boolean seekToNewSource(long targetPos) throws IOException { return ((Seekable) in).seekToNewSource(targetPos); } }
private long getFilePosition() throws IOException { long retVal; if (isCompressedInput && null != filePosition) { retVal = filePosition.getPos(); } else { retVal = pos; } return retVal; }
@Override public int read(final byte[] b, final int off, int len) throws IOException { if ((off | len | (off + len) | (b.length - (off + len))) < 0) { throw new IndexOutOfBoundsException(); } final int n = (int) Math.min(Integer.MAX_VALUE, Math.min(len, (end - pos))); if (n == 0) return -1; Integer ret = 0; synchronized (in) { // ensuring we are not closed which would be followed by someone else reusing the decompressor if (closed) { throw new IOException("Stream closed"); } ((Seekable) in).seek(pos); ret = in.read(b, off, n); } if (ret < 0) { end = pos; return -1; } pos += ret; return ret; }