@InterfaceAudience.Public @InterfaceStability.Stable public class FSDataInputStream extends DataInputStream implements Seekable, PositionedReadable, ByteBufferReadable, org.apache.hadoop.fs.HasFileDescriptor, CanSetDropBehind, CanSetReadahead, org.apache.hadoop.fs.HasEnhancedByteBufferAccess, CanUnbuffer, StreamCapabilities, ByteBufferPositionedReadable, org.apache.hadoop.fs.statistics.IOStatisticsSource
FSInputStream
in a DataInputStream
and buffers input through a BufferedInputStream
.in
ABORTABLE_STREAM, DROPBEHIND, HFLUSH, HSYNC, IOSTATISTICS, IOSTATISTICS_CONTEXT, PREADBYTEBUFFER, READAHEAD, READBYTEBUFFER, UNBUFFER, VECTOREDIO
Constructor and Description |
---|
FSDataInputStream(InputStream in) |
Modifier and Type | Method and Description |
---|---|
FileDescriptor |
getFileDescriptor() |
IOStatistics |
getIOStatistics()
Get the IO Statistics of the nested stream, falling back to
null if the stream does not implement the interface
IOStatisticsSource . |
long |
getPos()
Get the current position in the input stream.
|
InputStream |
getWrappedStream()
Get a reference to the wrapped input stream.
|
boolean |
hasCapability(String capability)
Query the stream for a specific capability.
|
int |
maxReadSizeForVectorReads()
What is the largest size that we should group ranges together as?
|
int |
minSeekForVectorReads()
What is the smallest reasonable seek?
|
int |
read(ByteBuffer buf)
Reads up to buf.remaining() bytes into buf.
|
ByteBuffer |
read(ByteBufferPool bufferPool,
int maxLength) |
ByteBuffer |
read(ByteBufferPool bufferPool,
int maxLength,
EnumSet<ReadOption> opts)
Get a ByteBuffer containing file data.
|
int |
read(long position,
byte[] buffer,
int offset,
int length)
Read bytes from the given position in the stream to the given buffer.
|
int |
read(long position,
ByteBuffer buf)
Reads up to
buf.remaining() bytes into buf from a given position
in the file and returns the number of bytes read. |
void |
readFully(long position,
byte[] buffer)
|
void |
readFully(long position,
byte[] buffer,
int offset,
int length)
Read bytes from the given position in the stream to the given buffer.
|
void |
readFully(long position,
ByteBuffer buf)
Reads
buf.remaining() bytes into buf from a given position in
the file or until the end of the data was reached before the read
operation completed. |
void |
readVectored(List<? extends org.apache.hadoop.fs.FileRange> ranges,
IntFunction<ByteBuffer> allocate)
Read fully a list of file ranges asynchronously from this file.
|
void |
releaseBuffer(ByteBuffer buffer)
Release a ByteBuffer which was created by the enhanced ByteBuffer read
function.
|
void |
seek(long desired)
Seek to the given offset.
|
boolean |
seekToNewSource(long targetPos)
Seek to the given position on an alternate copy of the data.
|
void |
setDropBehind(Boolean dropBehind)
Configure whether the stream should drop the cache.
|
void |
setReadahead(Long readahead)
Set the readahead on this stream.
|
String |
toString()
String value.
|
void |
unbuffer()
Reduce the buffering.
|
read, read, readBoolean, readByte, readChar, readDouble, readFloat, readFully, readFully, readInt, readLine, readLong, readShort, readUnsignedByte, readUnsignedShort, readUTF, readUTF, skipBytes
available, close, mark, markSupported, read, reset, skip
public FSDataInputStream(InputStream in)
public void seek(long desired) throws IOException
seek
in interface Seekable
desired
- offset to seek toIOException
- raised on errors performing I/O.public long getPos() throws IOException
getPos
in interface Seekable
IOException
- raised on errors performing I/O.public int read(long position, byte[] buffer, int offset, int length) throws IOException
read
in interface PositionedReadable
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-1
if there is no more data because the end of the stream has been
reachedIOException
- IO problems.public void readFully(long position, byte[] buffer, int offset, int length) throws IOException
length
bytes have been read.readFully
in interface PositionedReadable
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 readIOException
- 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.public void readFully(long position, byte[] buffer) throws IOException
readFully
in interface PositionedReadable
position
- position within filebuffer
- destination bufferIOException
- IO problems.EOFException
- the end of the data was reached before
the read operation completedpublic boolean seekToNewSource(long targetPos) throws IOException
targetPos
- position to seek toIOException
@InterfaceAudience.Public @InterfaceStability.Stable public InputStream getWrappedStream()
public int read(ByteBuffer buf) throws IOException
ByteBufferReadable
After a successful call, buf.position()
will be advanced by the
number of bytes read and buf.limit()
will be unchanged.
In the case of an exception, the state of the buffer (the contents of the
buffer, the buf.position()
, the buf.limit()
, etc.) is
undefined, and callers should be prepared to recover from this
eventuality.
Callers should use StreamCapabilities.hasCapability(String)
with
StreamCapabilities.READBYTEBUFFER
to check if the underlying
stream supports this interface, otherwise they might get a
UnsupportedOperationException
.
Implementations should treat 0-length requests as legitimate, and must not signal an error upon their receipt.
read
in interface ByteBufferReadable
buf
- the ByteBuffer to receive the results of the read operation.IOException
- if there is some error performing the readpublic FileDescriptor getFileDescriptor() throws IOException
getFileDescriptor
in interface org.apache.hadoop.fs.HasFileDescriptor
IOException
- raised on errors performing I/O.public void setReadahead(Long readahead) throws IOException, UnsupportedOperationException
CanSetReadahead
setReadahead
in interface CanSetReadahead
readahead
- The readahead to use. null means to use the default.IOException
- If there was an error changing the dropBehind
setting.
UnsupportedOperationException If this stream doesn't support
setting readahead.UnsupportedOperationException
public void setDropBehind(Boolean dropBehind) throws IOException, UnsupportedOperationException
CanSetDropBehind
setDropBehind
in interface CanSetDropBehind
dropBehind
- Whether to drop the cache. null means to use the
default value.IOException
- If there was an error changing the dropBehind
setting.
UnsupportedOperationException If this stream doesn't support
setting the drop-behind.UnsupportedOperationException
public ByteBuffer read(ByteBufferPool bufferPool, int maxLength, EnumSet<ReadOption> opts) throws IOException, UnsupportedOperationException
org.apache.hadoop.fs.HasEnhancedByteBufferAccess
read
in interface org.apache.hadoop.fs.HasEnhancedByteBufferAccess
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.IOException
- if there was an error reading.UnsupportedOperationException
- if factory was null,
and we needed an external byte buffer.public final ByteBuffer read(ByteBufferPool bufferPool, int maxLength) throws IOException, UnsupportedOperationException
public void releaseBuffer(ByteBuffer buffer)
org.apache.hadoop.fs.HasEnhancedByteBufferAccess
releaseBuffer
in interface org.apache.hadoop.fs.HasEnhancedByteBufferAccess
buffer
- The ByteBuffer to release.public void unbuffer()
CanUnbuffer
unbuffer
in interface CanUnbuffer
public boolean hasCapability(String capability)
StreamCapabilities
hasCapability
in interface StreamCapabilities
capability
- string to query the stream support for.public String toString()
public int read(long position, ByteBuffer buf) throws IOException
ByteBufferPositionedReadable
buf.remaining()
bytes into buf from a given position
in the file and returns the number of bytes read. Callers should use
buf.limit(...)
to control the size of the desired read and
buf.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 and buf.limit()
will be unchanged.
In the case of an exception, the state of the buffer (the contents of the
buffer, the buf.position()
, the buf.limit()
, etc.) is
undefined, and callers should be prepared to recover from this
eventuality.
Callers should use StreamCapabilities.hasCapability(String)
with
StreamCapabilities.PREADBYTEBUFFER
to check if the underlying
stream supports this interface, otherwise they might get a
UnsupportedOperationException
.
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.
read
in interface ByteBufferPositionedReadable
position
- position within filebuf
- the ByteBuffer to receive the results of the read operation.IOException
- if there is some error performing the readpublic void readFully(long position, ByteBuffer buf) throws IOException
ByteBufferPositionedReadable
buf.remaining()
bytes into buf from a given position in
the file or until the end of the data was reached before the read
operation completed. Callers should use buf.limit(...)
to
control the size of the desired read and buf.position(...)
to
control the offset into the buffer the data should be written to.
This operation provides similar semantics to
ByteBufferPositionedReadable.read(long, ByteBuffer)
, the difference is that this method is
guaranteed to read data until the ByteBuffer
is full, or until
the end of the data stream is reached.
readFully
in interface ByteBufferPositionedReadable
position
- position within filebuf
- the ByteBuffer to receive the results of the read operation.IOException
- if there is some error performing the readEOFException
- the end of the data was reached before
the read operation completedByteBufferPositionedReadable.read(long, ByteBuffer)
public IOStatistics getIOStatistics()
IOStatisticsSource
.getIOStatistics
in interface org.apache.hadoop.fs.statistics.IOStatisticsSource
public int minSeekForVectorReads()
PositionedReadable
minSeekForVectorReads
in interface PositionedReadable
public int maxReadSizeForVectorReads()
PositionedReadable
maxReadSizeForVectorReads
in interface PositionedReadable
public void readVectored(List<? extends org.apache.hadoop.fs.FileRange> ranges, IntFunction<ByteBuffer> allocate) throws IOException
PositionedReadable
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.
readVectored
in interface PositionedReadable
ranges
- the byte ranges to readallocate
- the function to allocate ByteBufferIOException
- any IOE.Copyright © 2024 Apache Software Foundation. All rights reserved.