public class ReaderInputStream extends InputStream
CharsetEncoder
that takes a sequence
of 16-bit Unicode characters and transform it into a sequence of bytes in UTF-8. Charset encoder uses two buffers:
charactersBuffer
for characters source - updated by wrapped reader, and bytesBuffer
where encoder
stores encoding result. All input stream read operations takes bytes from the bytes buffer; if bytes buffer is empty
delegates fillBytesBuffer()
.Modifier and Type | Field and Description |
---|---|
private static int |
BYTES_BUFFER_SIZE
Size of internal bytes buffer.
|
private ByteBuffer |
bytesBuffer
Bytes buffer used as output for the encoder.
|
private static int |
CHARACTERS_BUFFER_SIZE
Size of internal characters buffer.
|
private CharBuffer |
charactersBuffer
Characters buffer used as input for the encoder.
|
private CharsetEncoder |
encoder
Encoder for UTF-8 character set.
|
private boolean |
endOfInput
Flag true when end of file is detected by
fillBytesBuffer() . |
private static int |
EOF
Constant for end of file mark.
|
private CoderResult |
lastEncoderResult
Cache last encoder result.
|
private Reader |
reader
Source reader.
|
Constructor and Description |
---|
ReaderInputStream(Reader reader)
Construct a new input stream for source reader, using UTF-8 character encoding.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the stream.
|
private void |
fillBytesBuffer()
Encode characters from
charactersBuffer and store UTF-8 bytes into bytesBuffer . |
int |
read()
Reads the next byte of data from the input stream.
|
int |
read(byte[] b)
Reads some number of bytes from the input stream and stores them into the buffer array b.
|
int |
read(byte[] bytes,
int off,
int len)
Reads up to len bytes of data from the input stream into an array of bytes.
|
available, mark, markSupported, reset, skip
private static final int CHARACTERS_BUFFER_SIZE
charactersBuffer
.private static final int BYTES_BUFFER_SIZE
bytesBuffer
.private static final int EOF
private final CharsetEncoder encoder
charactersBuffer
and writes
UTF-8 encoded bytes into bytesBuffer
.private final CharBuffer charactersBuffer
bytesBuffer
.private final ByteBuffer bytesBuffer
private final Reader reader
private CoderResult lastEncoderResult
private boolean endOfInput
fillBytesBuffer()
.public ReaderInputStream(Reader reader)
reader
- source reader.public int read(byte[] bytes, int off, int len) throws IOException
This method simple copy bytesBuffer
into given bytes buffer. If stream buffer is empty delegates
fillBytesBuffer()
to fill it from underlying reader.
read
in class InputStream
bytes
- the bytes buffer to read into,off
- the offset to start reading bytes into,len
- the number of bytes to read.-1
if the end of the stream has been reached.IllegalArgumentException
- if bytes
argument is null.IndexOutOfBoundsException
- if offset or length is negative or sum of offset and length exceeds buffer size.IOException
- if read from underlying source fails.public int read(byte[] b) throws IOException
This method simple copy bytesBuffer
into given bytes buffer. If stream buffer is empty delegates
fillBytesBuffer()
to fill it from underlying reader.
read
in class InputStream
b
- the byte array to read into-1
if the end of the stream has been reached.IOException
- if read from underlying source fails.public int read() throws IOException
fillBytesBuffer()
to add more bytes, bytes that are in the end read and encoded
from source reader.read
in class InputStream
-1
if the end of the stream has been reached.IOException
- if read from underlying source reader fails.public void close() throws IOException
Reader
to be closed.close
in interface Closeable
close
in interface AutoCloseable
close
in class InputStream
IOException
- if underlying source reader closing fails.private void fillBytesBuffer() throws IOException
charactersBuffer
and store UTF-8 bytes into bytesBuffer
. If characters
buffer is underflow takes care to read more characters from underlying source reader.IOException
- if read from underlying source reader fails.charactersBuffer
,
bytesBuffer
Copyright © 2018. All rights reserved.