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 static ByteBufferEMPTY_BUFFEREmpty and read-only byte buffer.static byte[]EMPTY_BYTESEmpty byte array.
-
Constructor Summary
Constructors Constructor Description ClickHouseInputStream()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract booleanisClosed()Checks if the input stream has been closed or not.static ClickHouseInputStreamof(InputStream input)Wraps the given input stream.static ClickHouseInputStreamof(InputStream input, int bufferSize)Wraps the given input stream.static ClickHouseInputStreamof(BlockingQueue<ByteBuffer> queue, int timeout)Wraps the given blocking queue.StringreadAsciiString()Reads ascii string from input stream.StringreadAsciiString(int byteLength)Reads ascii string from input stream.abstract bytereadByte()Reads one single byte from the input stream.byte[]readBytes(int length)Readslengthbytes from the input stream.StringreadString(int byteLength, Charset charset)Reads string from the input stream.StringreadString(Charset charset)Reads string from the input stream.StringreadUnicodeString()Reads unicode string from input stream.StringreadUnicodeString(int byteLength)Reads unicode string from input stream.intreadUnsignedByte()Reads an unsigned byte from the input stream.intreadVarInt()Read varint from input stream.-
Methods inherited from class java.io.InputStream
available, close, mark, markSupported, nullInputStream, read, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
-
-
-
-
Field Detail
-
EMPTY_BYTES
public static final byte[] EMPTY_BYTES
Empty byte array.
-
EMPTY_BUFFER
public static final ByteBuffer EMPTY_BUFFER
Empty and read-only byte buffer.
-
-
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(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
-
readUnsignedByte
public int readUnsignedByte() throws IOExceptionReads an unsigned byte from the input stream. UnlikeInputStream.read(), it will throwIOExceptionif 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 IOExceptionReads one single byte from the input stream. UnlikeInputStream.read(), it will throwIOExceptionif 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 IOExceptionReadslengthbytes from the input stream. It behaves in the same way asDataInput.readFully(byte[]), and it will throwIOExceptionwhen 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
-
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. WhenbyteLengthis 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 IOExceptionRead varint from input stream.- Returns:
- varint
- Throws:
IOException- when failed to read value from input stream or reached end of the stream
-
isClosed
public abstract boolean isClosed()
Checks if the input stream has been closed or not.- Returns:
- true if the input stream has been closed; false otherwise
-
-