Package com.clickhouse.client
Class ClickHouseInputStream
- java.lang.Object
-
- java.io.InputStream
-
- com.clickhouse.client.ClickHouseInputStream
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
ClickHouseLZ4InputStream
public abstract class ClickHouseInputStream extends InputStream
Extended input stream for read optimization.
-
-
Field Summary
Fields Modifier and Type Field Description protected Runnable
afterClose
protected ClickHouseByteBuffer
byteBuffer
protected boolean
closed
static ByteBuffer
EMPTY_BUFFER
Deprecated.static byte[]
EMPTY_BYTES
Deprecated.
-
Constructor Summary
Constructors Modifier Constructor Description protected
ClickHouseInputStream(Runnable afterClose)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
close()
protected void
closeQuietly()
boolean
isClosed()
Checks if the input stream has been closed or not.static ClickHouseInputStream
of(InputStream input)
Wraps the given input stream.static ClickHouseInputStream
of(InputStream input, int bufferSize)
Wraps the given input stream.static ClickHouseInputStream
of(InputStream input, int bufferSize, Runnable afterClose)
Wraps the given input stream.static ClickHouseInputStream
of(BlockingQueue<ByteBuffer> queue, int timeout)
Wraps the given blocking queue.static ClickHouseInputStream
of(BlockingQueue<ByteBuffer> queue, int timeout, Runnable afterClose)
Wraps the given blocking queue.abstract int
peek()
Peeks one byte.abstract long
pipe(ClickHouseOutputStream output)
Reads all bytes and write into given output stream.ClickHouseByteBuffer
read(int len)
String
readAsciiString()
Reads ascii string from input stream.String
readAsciiString(int byteLength)
Reads ascii string from input stream.abstract byte
readByte()
Reads one single byte from the input stream.byte[]
readBytes(int length)
Readslength
bytes from the input stream.String
readString(int byteLength, Charset charset)
Reads string from the input stream.String
readString(Charset charset)
Reads string from the input stream.String
readUnicodeString()
Reads unicode string from input stream.String
readUnicodeString(int byteLength)
Reads unicode string from input stream.int
readUnsignedByte()
Reads an unsigned byte from the input stream.int
readVarInt()
Read varint from input stream.-
Methods inherited from class java.io.InputStream
available, mark, markSupported, nullInputStream, read, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
-
-
-
-
Field Detail
-
EMPTY_BYTES
@Deprecated public static final byte[] EMPTY_BYTES
Deprecated.Empty byte array.
-
EMPTY_BUFFER
@Deprecated public static final ByteBuffer EMPTY_BUFFER
Deprecated.Empty and read-only byte buffer.
-
afterClose
protected final Runnable afterClose
-
byteBuffer
protected final ClickHouseByteBuffer byteBuffer
-
closed
protected boolean closed
-
-
Constructor Detail
-
ClickHouseInputStream
protected ClickHouseInputStream(Runnable afterClose)
-
-
Method Detail
-
of
public static ClickHouseInputStream of(BlockingQueue<ByteBuffer> queue, int timeout)
Wraps the given blocking queue.- Parameters:
queue
- non-null blocking queuetimeout
- read timeout in milliseconds- Returns:
- wrapped input
-
of
public static ClickHouseInputStream of(BlockingQueue<ByteBuffer> queue, int timeout, Runnable afterClose)
Wraps the given blocking queue.- Parameters:
queue
- non-null blocking queuetimeout
- read timeout in millisecondsafterClose
- custom handler will be invoked right after closing the input stream- Returns:
- wrapped input
-
of
public static ClickHouseInputStream of(InputStream input)
Wraps the given input stream.- Parameters:
input
- non-null input stream- Returns:
- wrapped input, or the same input if it's instance of
ClickHouseInputStream
-
of
public static ClickHouseInputStream of(InputStream input, int bufferSize)
Wraps the given input stream.- Parameters:
input
- non-null input streambufferSize
- buffer size which is always greater than zero(usually 4096 or larger)- Returns:
- wrapped input, or the same input if it's instance of
ClickHouseInputStream
-
of
public static ClickHouseInputStream of(InputStream input, int bufferSize, Runnable afterClose)
Wraps the given input stream.- Parameters:
input
- non-null input streambufferSize
- buffer size which is always greater than zero(usually 4096 or larger)afterClose
- custom handler will be invoked right after closing the input stream- Returns:
- wrapped input, or the same input if it's instance of
ClickHouseInputStream
-
closeQuietly
protected void closeQuietly()
-
peek
public abstract int peek() throws IOException
Peeks one byte. It's similar asInputStream.read()
except it never changes cursor.- Returns:
- the next byte of data, or -1 if the end of the stream is reached
- Throws:
IOException
- when failed to read value from input stream or reached end of the stream
-
pipe
public abstract long pipe(ClickHouseOutputStream output) throws IOException
Reads all bytes and write into given output stream.- Parameters:
output
- non-null output stream- Returns:
- bytes being written into output stream
- Throws:
IOException
- when failed to read value from input stream or reached end of the stream
-
readUnsignedByte
public int readUnsignedByte() throws IOException
Reads an unsigned byte from the input stream. UnlikeInputStream.read()
, it will throwIOException
if the input stream has been closed.- Returns:
- unsigned byte
- Throws:
IOException
- when failed to read value from input stream or reached end of the stream
-
readByte
public abstract byte readByte() throws IOException
Reads one single byte from the input stream. UnlikeInputStream.read()
, it will throwIOException
if the input stream has been closed. In general, this method should be faster thanInputStream.read()
, especially when it's an input stream backed by byte[] orByteBuffer
.- Returns:
- byte value if present
- Throws:
IOException
- when failed to read value from input stream or reached end of the stream
-
readBytes
public byte[] readBytes(int length) throws IOException
Readslength
bytes from the input stream. It behaves in the same way asDataInput.readFully(byte[])
, except it will throwIOException
when the input stream has been closed.- Parameters:
length
- number of bytes to read- Returns:
- byte array and its length should be
length
- Throws:
IOException
- when failed to read value from input stream, not able to retrieve all bytes, or reached end of the stream
-
read
public ClickHouseByteBuffer read(int len) throws IOException
- Throws:
IOException
-
readString
public String readString(Charset charset) throws IOException
Reads string from the input stream.readVarInt()
will be called automatically to understand byte length of the string.- Parameters:
charset
- charset, null is treated asStandardCharsets.UTF_8
- Returns:
- non-null string
- Throws:
IOException
- when failed to read value from input stream, not able to retrieve all bytes, or reached end of the stream
-
readString
public String readString(int byteLength, Charset charset) throws IOException
Reads string from the input stream. WhenbyteLength
is zero or negative number, this method will always return empty string.- Parameters:
byteLength
- length in bytecharset
- charset, null is treated asStandardCharsets.UTF_8
- Returns:
- non-null string
- Throws:
IOException
- when failed to read value from input stream, not able to retrieve all bytes, or reached end of the stream
-
readAsciiString
public String readAsciiString() throws IOException
Reads ascii string from input stream.readVarInt()
will be called automatically to understand byte length of the string.- Returns:
- non-null ascii string
- Throws:
IOException
- when failed to read value from input stream, not able to retrieve all bytes, or reached end of the stream
-
readAsciiString
public String readAsciiString(int byteLength) throws IOException
Reads ascii string from input stream. Similar asreadString(byteLength, StandardCharsets.US_ASCII)
.- Parameters:
byteLength
- length in byte- Returns:
- non-null ascii string
- Throws:
IOException
- when failed to read value from input stream, not able to retrieve all bytes, or reached end of the stream
-
readUnicodeString
public String readUnicodeString() throws IOException
Reads unicode string from input stream.- Returns:
- non-null unicode string
- Throws:
IOException
- when failed to read value from input stream, not able to retrieve all bytes, or reached end of the stream
-
readUnicodeString
public String readUnicodeString(int byteLength) throws IOException
Reads unicode string from input stream. Similar asreadString(byteLength, null)
.- Parameters:
byteLength
- length in byte- Returns:
- non-null unicode string
- Throws:
IOException
- when failed to read value from input stream, not able to retrieve all bytes, or reached end of the stream
-
readVarInt
public int readVarInt() throws IOException
Read varint from input stream.- Returns:
- varint
- Throws:
IOException
- when failed to read value from input stream or reached end of the stream
-
isClosed
public boolean isClosed()
Checks if the input stream has been closed or not.- Returns:
- true if the input stream has been closed; false otherwise
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
-