Class FSDataInputStream
- All Implemented Interfaces:
Closeable,DataInput,AutoCloseable,ByteBufferPositionedReadable,ByteBufferReadable,CanSetDropBehind,CanSetReadahead,CanUnbuffer,org.apache.hadoop.fs.HasEnhancedByteBufferAccess,org.apache.hadoop.fs.HasFileDescriptor,PositionedReadable,Seekable,org.apache.hadoop.fs.statistics.IOStatisticsSource,StreamCapabilities
- Direct Known Subclasses:
HdfsDataInputStream
FSInputStream in a DataInputStream
and buffers input through a BufferedInputStream.-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.hadoop.fs.StreamCapabilities
StreamCapabilities.StreamCapability -
Field Summary
Fields inherited from class java.io.FilterInputStream
inFields inherited from interface org.apache.hadoop.fs.StreamCapabilities
ABORTABLE_STREAM, DROPBEHIND, HFLUSH, HSYNC, IOSTATISTICS, IOSTATISTICS_CONTEXT, PREADBYTEBUFFER, READAHEAD, READBYTEBUFFER, UNBUFFER, VECTOREDIO, VECTOREDIO_BUFFERS_SLICED -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGet the IO Statistics of the nested stream, falling back to null if the stream does not implement the interfaceIOStatisticsSource.longgetPos()Get the current position in the input stream.Get a reference to the wrapped input stream.booleanhasCapability(String capability) Query the stream for a specific capability.intWhat is the largest size that we should group ranges together as?intWhat is the smallest reasonable seek?intread(long position, byte[] buffer, int offset, int length) Read bytes from the given position in the stream to the given buffer.intread(long position, ByteBuffer buf) Reads up tobuf.remaining()bytes into buf from a given position in the file and returns the number of bytes read.intread(ByteBuffer buf) Reads up to buf.remaining() bytes into buf.final ByteBufferread(ByteBufferPool bufferPool, int maxLength) read(ByteBufferPool bufferPool, int maxLength, EnumSet<ReadOption> opts) Get a ByteBuffer containing file data.voidreadFully(long position, byte[] buffer) voidreadFully(long position, byte[] buffer, int offset, int length) Read bytes from the given position in the stream to the given buffer.voidreadFully(long position, ByteBuffer buf) Delegate to the underlying stream.voidreadVectored(List<? extends org.apache.hadoop.fs.FileRange> ranges, IntFunction<ByteBuffer> allocate) Read fully a list of file ranges asynchronously from this file.voidreadVectored(List<? extends org.apache.hadoop.fs.FileRange> ranges, IntFunction<ByteBuffer> allocate, Consumer<ByteBuffer> release) Extension ofPositionedReadable.readVectored(List, IntFunction)where arelease(buffer)operation may be invoked if problems surface during reads.voidreleaseBuffer(ByteBuffer buffer) Release a ByteBuffer which was created by the enhanced ByteBuffer read function.voidseek(long desired) Seek to the given offset.booleanseekToNewSource(long targetPos) Seek to the given position on an alternate copy of the data.voidsetDropBehind(Boolean dropBehind) Configure whether the stream should drop the cache.voidsetReadahead(Long readahead) Set the readahead on this stream.toString()String value.voidunbuffer()Reduce the buffering.Methods inherited from class java.io.DataInputStream
read, read, readBoolean, readByte, readChar, readDouble, readFloat, readFully, readFully, readInt, readLine, readLong, readShort, readUnsignedByte, readUnsignedShort, readUTF, readUTF, skipBytesMethods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, reset, skipMethods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Constructor Details
-
FSDataInputStream
-
-
Method Details
-
seek
Seek to the given offset.- Specified by:
seekin interfaceSeekable- Parameters:
desired- offset to seek to- Throws:
IOException- raised on errors performing I/O.
-
getPos
Get the current position in the input stream.- Specified by:
getPosin interfaceSeekable- Returns:
- current position in the input stream
- Throws:
IOException- raised on errors performing I/O.
-
read
Read bytes from the given position in the stream to the given buffer.- Specified by:
readin interfacePositionedReadable- Parameters:
position- position in the input stream to seekbuffer- buffer into which data is readoffset- offset into the buffer in which data is writtenlength- maximum number of bytes to read- Returns:
- total number of bytes read into the buffer, or
-1if there is no more data because the end of the stream has been reached - Throws:
IOException- IO problems.
-
readFully
Read bytes from the given position in the stream to the given buffer. Continues to read untillengthbytes have been read.- Specified by:
readFullyin interfacePositionedReadable- Parameters:
position- position in the input stream to seekbuffer- buffer into which data is readoffset- offset into the buffer in which data is writtenlength- the number of bytes to read- Throws:
IOException- IO problemsEOFException- If the end of stream is reached while reading. If an exception is thrown an undetermined number of bytes in the buffer may have been written.
-
readFully
- Specified by:
readFullyin interfacePositionedReadable- Parameters:
position- position within filebuffer- destination buffer- Throws:
IOException- IO problems.EOFException- the end of the data was reached before the read operation completed
-
seekToNewSource
Seek to the given position on an alternate copy of the data.- Specified by:
seekToNewSourcein interfaceSeekable- Parameters:
targetPos- position to seek to- Returns:
- true if a new source is found, false otherwise
- Throws:
IOException- raised on errors performing I/O.
-
getWrappedStream
Get a reference to the wrapped input stream. Used by unit tests.- Returns:
- the underlying input stream
-
read
Description copied from interface:ByteBufferReadableReads up to buf.remaining() bytes into buf. Callers should use buf.limit(..) to control the size of the desired read.After a successful call,
buf.position()will be advanced by the number of bytes read andbuf.limit()will be unchanged.In the case of an exception, the state of the buffer (the contents of the buffer, the
buf.position(), thebuf.limit(), etc.) is undefined, and callers should be prepared to recover from this eventuality.Callers should use
StreamCapabilities.hasCapability(String)withStreamCapabilities.READBYTEBUFFERto check if the underlying stream supports this interface, otherwise they might get aUnsupportedOperationException.Implementations should treat 0-length requests as legitimate, and must not signal an error upon their receipt.
- Specified by:
readin interfaceByteBufferReadable- Parameters:
buf- the ByteBuffer to receive the results of the read operation.- Returns:
- the number of bytes read, possibly zero, or -1 if reach end-of-stream
- Throws:
IOException- if there is some error performing the read
-
getFileDescriptor
- Specified by:
getFileDescriptorin interfaceorg.apache.hadoop.fs.HasFileDescriptor- Returns:
- the FileDescriptor
- Throws:
IOException- raised on errors performing I/O.
-
setReadahead
Description copied from interface:CanSetReadaheadSet the readahead on this stream.- Specified by:
setReadaheadin interfaceCanSetReadahead- Parameters:
readahead- The readahead to use. null means to use the default.- Throws:
IOException- If there was an error changing the dropBehind setting. UnsupportedOperationException If this stream doesn't support setting readahead.UnsupportedOperationException
-
setDropBehind
Description copied from interface:CanSetDropBehindConfigure whether the stream should drop the cache.- Specified by:
setDropBehindin interfaceCanSetDropBehind- Parameters:
dropBehind- Whether to drop the cache. null means to use the default value.- Throws:
IOException- If there was an error changing the dropBehind setting. UnsupportedOperationException If this stream doesn't support setting the drop-behind.UnsupportedOperationException
-
read
public ByteBuffer read(ByteBufferPool bufferPool, int maxLength, EnumSet<ReadOption> opts) throws IOException, UnsupportedOperationException Description copied from interface:org.apache.hadoop.fs.HasEnhancedByteBufferAccessGet a ByteBuffer containing file data. This ByteBuffer may come from the stream itself, via a call like mmap, or it may come from the ByteBufferFactory which is passed in as an argument.- Specified by:
readin interfaceorg.apache.hadoop.fs.HasEnhancedByteBufferAccess- Parameters:
bufferPool- If this is non-null, it will be used to create a fallback ByteBuffer when the stream itself cannot create one.maxLength- The maximum length of buffer to return. We may return a buffer which is shorter than this.opts- Options to use when reading.- Returns:
- We will always return an empty buffer if maxLength was 0, whether or not we are at EOF. If maxLength > 0, we will return null if the stream has reached EOF. Otherwise, we will return a ByteBuffer containing at least one byte. You must free this ByteBuffer when you are done with it by calling releaseBuffer on it. The buffer will continue to be readable until it is released in this manner. However, the input stream's close method may warn about unclosed buffers.
- Throws:
IOException- if there was an error reading.UnsupportedOperationException- if factory was null, and we needed an external byte buffer.
-
read
public final ByteBuffer read(ByteBufferPool bufferPool, int maxLength) throws IOException, UnsupportedOperationException -
releaseBuffer
Description copied from interface:org.apache.hadoop.fs.HasEnhancedByteBufferAccessRelease a ByteBuffer which was created by the enhanced ByteBuffer read function. You must not continue using the ByteBuffer after calling this function.- Specified by:
releaseBufferin interfaceorg.apache.hadoop.fs.HasEnhancedByteBufferAccess- Parameters:
buffer- The ByteBuffer to release.
-
unbuffer
public void unbuffer()Description copied from interface:CanUnbufferReduce the buffering. This will also free sockets and file descriptors held by the stream, if possible.- Specified by:
unbufferin interfaceCanUnbuffer
-
hasCapability
Description copied from interface:StreamCapabilitiesQuery the stream for a specific capability.- Specified by:
hasCapabilityin interfaceStreamCapabilities- Parameters:
capability- string to query the stream support for.- Returns:
- True if the stream supports capability.
-
toString
String value. Includes the string value of the inner stream -
read
Description copied from interface:ByteBufferPositionedReadableReads up tobuf.remaining()bytes into buf from a given position in the file and returns the number of bytes read. Callers should usebuf.limit(...)to control the size of the desired read andbuf.position(...)to control the offset into the buffer the data should be written to.After a successful call,
buf.position()will be advanced by the number of bytes read andbuf.limit()will be unchanged.In the case of an exception, the state of the buffer (the contents of the buffer, the
buf.position(), thebuf.limit(), etc.) is undefined, and callers should be prepared to recover from this eventuality.Callers should use
StreamCapabilities.hasCapability(String)withStreamCapabilities.PREADBYTEBUFFERto check if the underlying stream supports this interface, otherwise they might get aUnsupportedOperationException.Implementations should treat 0-length requests as legitimate, and must not signal an error upon their receipt.
This does not change the current offset of a file, and is thread-safe.
- Specified by:
readin interfaceByteBufferPositionedReadable- Parameters:
position- position within filebuf- the ByteBuffer to receive the results of the read operation.- Returns:
- the number of bytes read, possibly zero, or -1 if reached end-of-stream
- Throws:
IOException- if there is some error performing the read
-
readFully
Delegate to the underlying stream.- Specified by:
readFullyin interfaceByteBufferPositionedReadable- Parameters:
position- position within filebuf- the ByteBuffer to receive the results of the read operation.- Throws:
IOException- on a failure from the nested stream.UnsupportedOperationException- if the inner stream does not support this operation.- See Also:
-
getIOStatistics
Get the IO Statistics of the nested stream, falling back to null if the stream does not implement the interfaceIOStatisticsSource.- Specified by:
getIOStatisticsin interfaceorg.apache.hadoop.fs.statistics.IOStatisticsSource- Returns:
- an IOStatistics instance or null
-
minSeekForVectorReads
public int minSeekForVectorReads()Description copied from interface:PositionedReadableWhat is the smallest reasonable seek?- Specified by:
minSeekForVectorReadsin interfacePositionedReadable- Returns:
- the minimum number of bytes
-
maxReadSizeForVectorReads
public int maxReadSizeForVectorReads()Description copied from interface:PositionedReadableWhat is the largest size that we should group ranges together as?- Specified by:
maxReadSizeForVectorReadsin interfacePositionedReadable- Returns:
- the number of bytes to read at once
-
readVectored
public void readVectored(List<? extends org.apache.hadoop.fs.FileRange> ranges, IntFunction<ByteBuffer> allocate) throws IOException Description copied from interface:PositionedReadableRead fully a list of file ranges asynchronously from this file. The default iterates through the ranges to read each synchronously, but the intent is that FSDataInputStream subclasses can make more efficient readers. As a result of the call, each range will have FileRange.setData(CompletableFuture) called with a future that when complete will have a ByteBuffer with the data from the file's range.The position returned by getPos() after readVectored() is undefined.
If a file is changed while the readVectored() operation is in progress, the output is undefined. Some ranges may have old data, some may have new and some may have both.
While a readVectored() operation is in progress, normal read api calls may block.
- Specified by:
readVectoredin interfacePositionedReadable- Parameters:
ranges- the byte ranges to readallocate- the function to allocate ByteBuffer- Throws:
IOException- any IOE.
-
readVectored
public void readVectored(List<? extends org.apache.hadoop.fs.FileRange> ranges, IntFunction<ByteBuffer> allocate, Consumer<ByteBuffer> release) throws IOException Description copied from interface:PositionedReadableExtension ofPositionedReadable.readVectored(List, IntFunction)where arelease(buffer)operation may be invoked if problems surface during reads.The
releaseoperation is invoked after an IOException to return the actively buffer to a pool before reporting a failure in the future.The default implementation calls
PositionedReadable.readVectored(List, IntFunction).pImplementations SHOULD override this method if they can release buffers as part of their error handling.
- Specified by:
readVectoredin interfacePositionedReadable- Parameters:
ranges- the byte ranges to readallocate- function to allocate ByteBufferrelease- callable to release a ByteBuffer.- Throws:
IOException- any IOE.
-