| java.lang.Object java.io.InputStream it.unimi.dsi.fastutil.io.MeasurableInputStream it.unimi.dsi.fastutil.io.FastBufferedInputStream
FastBufferedInputStream | public class FastBufferedInputStream extends MeasurableInputStream implements RepositionableStream(Code) | | Lightweight, unsynchronized, aligned input stream buffering class with
,
,
and
support.
This class provides buffering for input streams, but it does so with
purposes and an internal logic that are radically different from the ones
adopted in
java.io.BufferedInputStream . The main features follow.
There is no support for marking. All methods are unsychronized.
As an additional feature, this class implements the
RepositionableStream and
MeasurableInputStream interfaces.
An instance of this class will try to cast
the underlying byte stream to a
RepositionableStream and to fetch by
reflection the
java.nio.channels.FileChannel underlying the given
output stream, in this order. If either reference can be successfully
fetched, you can use
FastBufferedInputStream.position(long) to reposition the stream.
Much in the same way, an instance of this class will try to cast the
the underlying byte stream to a
MeasurableInputStream , and if this
operation is successful, or if a
java.nio.channels.FileChannel can
be detected, then
FastBufferedInputStream.position() and
FastBufferedInputStream.length() will work as expected.
Due to erratic and unpredictable behaviour of
InputStream.skip(long) ,
which does not correspond to its specification and which Sun refuses to fix
(see bug 6222822;
don't be fooled by the “closed, fixed” label),
this class peeks at the underlying stream and if it is
System.in it uses
repeated reads instead of calling
InputStream.skip(long) on the underlying stream; moreover,
skips and reads are tried alternately, so to guarantee that skipping
less bytes than requested can be caused only by reaching the end of file.
This class keeps also track of the number of bytes read so far, so
to be able to implemented
MeasurableInputStream.position independently of underlying input stream.
This class has limited support for
(whatever that means) from the underlying input stream. You can choose the set of
that
delimit lines.
since: 4.4 |
Inner Class :public static enum LineTerminator | |
Method Summary | |
public int | available() | public void | close() | public void | flush() Resets the internal logic of this fast buffered input stream, clearing the buffer. | public long | length() Returns the length of the underlying input stream, if it is
. | protected boolean | noMoreCharacters() Checks whether no more bytes will be returned. | public void | position(long newPosition) | public long | position() | public int | read() | public int | read(byte b, int offset, int length) | public int | readLine(byte[] array) Reads a line into the given byte array using
.
Parameters: array - byte array where the next line will be stored. | public int | readLine(byte[] array, EnumSet<LineTerminator> terminators) Reads a line into the given byte array.
Parameters: array - byte array where the next line will be stored. Parameters: terminators - a set containing the line termination sequences that we wantto consider as valid. | public int | readLine(byte[] array, int off, int len) Reads a line into the given byte-array fragment using
.
Parameters: array - byte array where the next line will be stored. Parameters: off - the first byte to use in array . Parameters: len - the maximum number of bytes to read. | public int | readLine(byte[] array, int off, int len, EnumSet<LineTerminator> terminators) Reads a line into the given byte-array fragment.
Reading lines (i.e., characters) out of a byte stream is not always sensible
(methods available to that purpose in old versions of Java have been mercilessly deprecated).
Nonetheless, in several situations, such as when decoding network protocols or headers
known to be ASCII, it is very useful to be able to read a line from a byte stream.
This method will attempt to read the next line into array starting at off ,
reading at most len bytes. | public void | reset() Resets the internal logic of this fast buffered input stream. | public long | skip(long n) Skips over and discards the given number of bytes of data from this fast buffered input stream. |
ALL_TERMINATORS | final public static EnumSet<LineTerminator> ALL_TERMINATORS(Code) | | A set containing all available line terminators.
|
DEFAULT_BUFFER_SIZE | final public static int DEFAULT_BUFFER_SIZE(Code) | | The default size of the internal buffer in bytes (8Ki).
|
buffer | protected byte buffer(Code) | | The internal buffer.
|
pos | protected int pos(Code) | | The current position in the buffer.
|
readBytes | protected long readBytes(Code) | | The number of bytes ever read (reset upon a call to
FastBufferedInputStream.position(long) ).
In particular, this will always represent the index (in the underlying input stream)
of the first available byte in the buffer.
|
FastBufferedInputStream | public FastBufferedInputStream(InputStream is, int bufSize)(Code) | | Creates a new fast buffered input stream by wrapping a given input stream with a given buffer size.
Parameters: is - an input stream to wrap. Parameters: bufSize - the size in bytes of the internal buffer (greater than zero). |
flush | public void flush()(Code) | | Resets the internal logic of this fast buffered input stream, clearing the buffer.
All buffering information is discarded, and the number of bytes read so far
(and thus, also the
)
is adjusted to reflect this fact.
This method is mainly useful for re-reading
files that have been overwritten externally.
|
length | public long length() throws IOException(Code) | | Returns the length of the underlying input stream, if it is
.
the length of the underlying input stream. throws: UnsupportedOperationException - if the underlying input stream is not . |
noMoreCharacters | protected boolean noMoreCharacters() throws IOException(Code) | | Checks whether no more bytes will be returned.
This method will refill the internal buffer.
true if there are no characters in the internal buffer andthe underlying reader is exhausted. |
readLine | public int readLine(byte[] array, EnumSet<LineTerminator> terminators) throws IOException(Code) | | Reads a line into the given byte array.
Parameters: array - byte array where the next line will be stored. Parameters: terminators - a set containing the line termination sequences that we wantto consider as valid. the number of bytes actually placed in array , or -1 at end of file. See Also: FastBufferedInputStream.readLine(byte[],int,int,EnumSet) |
readLine | public int readLine(byte[] array, int off, int len) throws IOException(Code) | | Reads a line into the given byte-array fragment using
.
Parameters: array - byte array where the next line will be stored. Parameters: off - the first byte to use in array . Parameters: len - the maximum number of bytes to read. the number of bytes actually placed in array , or -1 at end of file. See Also: FastBufferedInputStream.readLine(byte[],int,int,EnumSet) |
readLine | public int readLine(byte[] array, int off, int len, EnumSet<LineTerminator> terminators) throws IOException(Code) | | Reads a line into the given byte-array fragment.
Reading lines (i.e., characters) out of a byte stream is not always sensible
(methods available to that purpose in old versions of Java have been mercilessly deprecated).
Nonetheless, in several situations, such as when decoding network protocols or headers
known to be ASCII, it is very useful to be able to read a line from a byte stream.
This method will attempt to read the next line into array starting at off ,
reading at most len bytes. The read, however, will be stopped by the end of file or
when meeting a
. Of course, for this operation
to be sensible the encoding of the text contained in the stream, if any, must not generate spurious
carriage returns or line feeds. Note that the termination detection uses a maximisation
criterion, so if you specify both
LineTerminator.CR and
LineTerminator.CR_LF meeting a pair CR/LF will consider the whole pair a terminator.
Terminators are not copied into array or included in the returned count. The
returned integer can be used to check whether the line is complete: if it is smaller than
len , then more bytes might be available, but note that this method (contrarily
to
FastBufferedInputStream.read(byte[],int,int) ) can legitimately return zero when len
is nonzero just because a terminator was found as the first character. Thus, the intended
usage of this method is to call it on a given array, check whether len bytes
have been read, and if so try again (possibly extending the array) until a number of read bytes
strictly smaller than len (possibly, -1) is returned.
If you need to guarantee that a full line is read, use the following idiom:
int start = off, len;
while( ( len = readLine( array, start, array.length - start, terminators ) ) == array.length - start ) {
start += len;
array = ByteArrays.grow( array, array.length + 1 );
};
At the end of the loop, the line will be placed in array starting at
off (inclusive) and ending at start + Math.max( len, 0 ) (exclusive).
Parameters: array - byte array where the next line will be stored. Parameters: off - the first byte to use in array . Parameters: len - the maximum number of bytes to read. Parameters: terminators - a set containing the line termination sequences that we wantto consider as valid. the number of bytes actually placed in array , or -1 at end of file.Note that the returned number will be len if no line termination sequence specified in terminators has been met before scanning len byte,and if also we did not meet the end of file. |
skip | public long skip(long n) throws IOException(Code) | | Skips over and discards the given number of bytes of data from this fast buffered input stream.
As explained in the
, the semantics
of
InputStream.skip(long) is fatally flawed. This method provides additional semantics as follows:
it will skip the provided number of bytes, unless the end of file has been reached.
Additionally, if the underlying input stream is
System.in this method will use
repeated reads instead of invoking
InputStream.skip(long) .
Parameters: n - the number of bytes to skip. the number of bytes actually skipped; it can be smaller than n only if the end of file has been reached. See Also: InputStream.skip(long) |
|
|