Package com.clickhouse.client
Class ClickHouseInputStream
- java.lang.Object
-
- java.io.InputStream
-
- com.clickhouse.client.ClickHouseInputStream
-
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
AbstractByteArrayInputStream,AbstractByteBufferInputStream,EmptyInputStream,NonBlockingInputStream
public abstract class ClickHouseInputStream extends InputStream
Extended input stream for read optimization. Methods likereadBuffer(int),readByte(),readBytes(int), andreadCustom(ClickHouseDataUpdater)are added to reduce object creation as well as closing the stream when it reaches end of stream. This class is also responsible for creating various input stream as needed.
-
-
Field Summary
Fields Modifier and Type Field Description protected ClickHouseByteBufferbyteBufferNon-null reusable byte buffer.protected booleanclosedprotected OutputStreamcopyTostatic ByteBufferEMPTY_BUFFERDeprecated.will be removed in v0.3.3, please useClickHouseByteBuffer.EMPTY_BUFFERinsteadstatic byte[]EMPTY_BYTESDeprecated.will be removed in v0.3.3, please useClickHouseByteBuffer.EMPTY_BYTESinsteadprotected static StringERROR_INCOMPLETE_READprotected static StringERROR_NULL_BYTESprotected static StringERROR_REUSE_BUFFERprotected static StringERROR_STREAM_CLOSEDprotected ClickHouseFilefileUnderlying file.protected RunnablepostCloseActionOptional post close action.
-
Constructor Summary
Constructors Modifier Constructor Description protectedClickHouseInputStream(ClickHouseFile file, OutputStream copyTo, Runnable postCloseAction)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclose()protected voidcloseQuietly()Closes the input stream quietly.static ClickHouseInputStreamempty()Gets an empty input stream that produces nothing and cannot be closed.protected voidensureOpen()Checks whether the input stream has been closed and throw an exception if it is.ClickHouseFilegetUnderlyingFile()Gets underlying file.booleanisClosed()Checks if the input stream has been closed or not.static ClickHouseInputStreamof(byte[]... bytes)Wraps the given byte arrays.static ClickHouseInputStreamof(ClickHouseDeferredValue<InputStream> deferredInput, int bufferSize, Runnable postCloseAction)Wraps the deferred input stream.static ClickHouseInputStreamof(ClickHouseFile file, int bufferSize, Runnable postCloseAction)Wraps the given file as input stream.static ClickHouseInputStreamof(File... files)Wraps the given files.static ClickHouseInputStreamof(InputStream... inputs)Wraps the given input streams.static ClickHouseInputStreamof(InputStream input, int bufferSize)Wraps the given input stream.static ClickHouseInputStreamof(InputStream input, int bufferSize, ClickHouseCompression compression)Wraps the given input stream.static ClickHouseInputStreamof(InputStream input, int bufferSize, ClickHouseCompression compression, Runnable postCloseAction)Wraps the given input stream.static ClickHouseInputStreamof(InputStream input, int bufferSize, Runnable postCloseAction)Wraps the given input stream.static <T> ClickHouseInputStreamof(Iterable<T> source, Class<T> clazz, Function<T,byte[]> converter, Runnable postCloseAction)Wraps the given iterable objects as byte array based binary input stream.static ClickHouseInputStreamof(String... strings)Wraps the given (UTF-8)strings.static ClickHouseInputStreamof(URL... urls)Wraps the given URLs.static ClickHouseInputStreamof(ByteBuffer... buffers)Wraps the given byte buffers.static ClickHouseInputStreamof(BlockingQueue<ByteBuffer> queue, int timeout)Wraps the given blocking queue.static ClickHouseInputStreamof(BlockingQueue<ByteBuffer> queue, int timeout, Runnable postCloseAction)Wraps the given blocking queue.static <T> ClickHouseInputStreamof(T[] source, Class<T> clazz, Function<T,byte[]> converter, Runnable postCloseAction)Wraps the given array of object as byte array based binary input stream.abstract intpeek()Peeks one byte.abstract longpipe(ClickHouseOutputStream output)Reads all remaining bytes and write into given output stream.static longpipe(InputStream input, OutputStream output, byte[] buffer)Transfers data from input stream to output stream.static longpipe(InputStream input, OutputStream output, int bufferSize)Transfers data from input stream to output stream.StringreadAsciiString()Reads ascii string from input stream.StringreadAsciiString(int byteLength)Reads ascii string from input stream.ClickHouseByteBufferreadBuffer(int length)Reads byte buffer from the input stream.abstract bytereadByte()Reads one single byte from the input stream.byte[]readBytes(int length)Readslengthbytes from the input stream.abstract ClickHouseByteBufferreadCustom(ClickHouseDataUpdater reader)Reads bytes using custom reader.StringreadString(int byteLength, Charset charset)Reads binary string from the input stream.StringreadString(Charset charset)Reads binary 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()Reads a varint from input stream.longreadVarLong()Reads 64-bit varint as long from input stream.static Filesave(File file, InputStream in, int bufferSize, int timeout, boolean deleteOnExit)static Filesave(InputStream in, int bufferSize, int timeout)voidsetCopyToTarget(OutputStream out)Sets target output stream to copy bytes to.static ClickHouseInputStreamwrap(ClickHouseFile file, InputStream input, int bufferSize, Runnable postCloseAction, ClickHouseCompression compression, int compressionLevel)Wraps the given 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.will be removed in v0.3.3, please useClickHouseByteBuffer.EMPTY_BYTESinsteadEmpty byte array.
-
EMPTY_BUFFER
@Deprecated public static final ByteBuffer EMPTY_BUFFER
Deprecated.will be removed in v0.3.3, please useClickHouseByteBuffer.EMPTY_BUFFERinsteadEmpty and read-only byte buffer.
-
ERROR_INCOMPLETE_READ
protected static final String ERROR_INCOMPLETE_READ
- See Also:
- Constant Field Values
-
ERROR_NULL_BYTES
protected static final String ERROR_NULL_BYTES
- See Also:
- Constant Field Values
-
ERROR_REUSE_BUFFER
protected static final String ERROR_REUSE_BUFFER
- See Also:
- Constant Field Values
-
ERROR_STREAM_CLOSED
protected static final String ERROR_STREAM_CLOSED
- See Also:
- Constant Field Values
-
byteBuffer
protected final ClickHouseByteBuffer byteBuffer
Non-null reusable byte buffer.
-
file
protected final ClickHouseFile file
Underlying file.
-
postCloseAction
protected final Runnable postCloseAction
Optional post close action.
-
closed
protected boolean closed
-
copyTo
protected OutputStream copyTo
-
-
Constructor Detail
-
ClickHouseInputStream
protected ClickHouseInputStream(ClickHouseFile file, OutputStream copyTo, Runnable postCloseAction)
-
-
Method Detail
-
wrap
public static ClickHouseInputStream wrap(ClickHouseFile file, InputStream input, int bufferSize, Runnable postCloseAction, ClickHouseCompression compression, int compressionLevel)
Wraps the given input stream.- Parameters:
file- wrapped file, could be nullinput- non-null input streambufferSize- buffer sizepostCloseAction- custom action will be performed right after closing the wrapped input streamcompression- compression algorithmcompressionLevel- compression level- Returns:
- non-null wrapped input stream
-
empty
public static ClickHouseInputStream empty()
Gets an empty input stream that produces nothing and cannot be closed.- Returns:
- empty input stream
-
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 postCloseAction)
Wraps the given blocking queue.- Parameters:
queue- non-null blocking queuetimeout- read timeout in millisecondspostCloseAction- custom action will be performed right after closing the input stream- Returns:
- wrapped input
-
of
public static ClickHouseInputStream of(ClickHouseDeferredValue<InputStream> deferredInput, int bufferSize, Runnable postCloseAction)
Wraps the deferred input stream.- Parameters:
deferredInput- non-null deferred input streambufferSize- buffer size which is always greater than zero(usually 8192 or larger)postCloseAction- custom action will be performed right after closing the input stream- Returns:
- wrapped input
-
of
public static ClickHouseInputStream of(ClickHouseFile file, int bufferSize, Runnable postCloseAction)
Wraps the given file as input stream.- Parameters:
file- non-null filebufferSize- buffer size which is always greater than zero(usually 8192 or larger)postCloseAction- custom action will be performed right after closing the input stream- Returns:
- wrapped input
-
of
public static ClickHouseInputStream of(InputStream input, int bufferSize)
Wraps the given input stream.- Parameters:
input- input streambufferSize- buffer size which is always greater than zero(usually 8192 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 postCloseAction)
Wraps the given input stream.- Parameters:
input- input streambufferSize- buffer size which is always greater than zero(usually 8192 or larger)postCloseAction- custom action will be performed right after closing the input stream- Returns:
- wrapped input, or the same input if it's instance of
ClickHouseInputStream
-
of
public static ClickHouseInputStream of(InputStream input, int bufferSize, ClickHouseCompression compression)
Wraps the given input stream.- Parameters:
input- input streambufferSize- buffer size which is always greater than zero(usually 8192 or larger)compression- compression algorithm, null orClickHouseCompression.NONEmeans no compression- Returns:
- wrapped input, or the same input if it's instance of
ClickHouseInputStream
-
of
public static ClickHouseInputStream of(InputStream input, int bufferSize, ClickHouseCompression compression, Runnable postCloseAction)
Wraps the given input stream.- Parameters:
input- input streambufferSize- buffer size which is always greater than zero(usually 8192 or larger)compression- compression algorithm, null orClickHouseCompression.NONEmeans no compressionpostCloseAction- custom action will be performed right after closing the input stream- Returns:
- wrapped input, or the same input if it's instance of
ClickHouseInputStream
-
of
public static ClickHouseInputStream of(byte[]... bytes)
Wraps the given byte arrays.- Parameters:
bytes- array of byte array- Returns:
- non-null input stream
- See Also:
of(Iterable, Class, Function, Runnable)
-
of
public static ClickHouseInputStream of(ByteBuffer... buffers)
Wraps the given byte buffers.- Parameters:
buffers- array of byte buffer- Returns:
- non-null input stream
- See Also:
of(Iterable, Class, Function, Runnable)
-
of
public static ClickHouseInputStream of(File... files)
Wraps the given files.- Parameters:
files- array of file- Returns:
- non-null input stream
- See Also:
of(Iterable, Class, Function, Runnable)
-
of
public static ClickHouseInputStream of(InputStream... inputs)
Wraps the given input streams.- Parameters:
inputs- array of input stream- Returns:
- non-null input stream
- See Also:
of(Iterable, Class, Function, Runnable)
-
of
public static ClickHouseInputStream of(String... strings)
Wraps the given (UTF-8)strings.- Parameters:
strings- array of string- Returns:
- non-null input stream
- See Also:
of(Iterable, Class, Function, Runnable)
-
of
public static ClickHouseInputStream of(URL... urls)
Wraps the given URLs.- Parameters:
urls- array of URL- Returns:
- non-null input stream
- See Also:
of(Iterable, Class, Function, Runnable)
-
of
public static <T> ClickHouseInputStream of(T[] source, Class<T> clazz, Function<T,byte[]> converter, Runnable postCloseAction)
Wraps the given array of object as byte array based binary input stream.- Type Parameters:
T- type of the object- Parameters:
source- array of objectclazz- class of the objectconverter- optional transformer to convert each object into byte arraypostCloseAction- custom action will be performed right after closing the input stream- Returns:
- non-null input stream
- See Also:
of(Iterable, Class, Function, Runnable)
-
of
public static <T> ClickHouseInputStream of(Iterable<T> source, Class<T> clazz, Function<T,byte[]> converter, Runnable postCloseAction)
Wraps the given iterable objects as byte array based binary input stream.byte[],ByteBuffer,InputStream,File,String, andURLare all supported by default.- Type Parameters:
T- type of the object- Parameters:
source- iterable objects(e.g. byte[], ByteBuffer, and String etc.)clazz- class of the objectconverter- optional transformer to convert each object into byte arraypostCloseAction- custom action will be performed right after closing the input stream- Returns:
- non-null input stream
-
pipe
public static long pipe(InputStream input, OutputStream output, int bufferSize) throws IOException
Transfers data from input stream to output stream. Input stream will be closed but output stream will remain open. Please pay attention that you need to explictly calloutput.flush()before closing output stream.- Parameters:
input- non-null input stream, which will be closedoutput- non-null output stream, which will remain openbufferSize- buffer size, zero or negative number will be treated asClickHouseClientOption.BUFFER_SIZE- Returns:
- written bytes
- Throws:
IOException- when error occured reading from input stream or writing data to output stream
-
pipe
public static long pipe(InputStream input, OutputStream output, byte[] buffer) throws IOException
Transfers data from input stream to output stream. Input stream will be closed but output stream will remain open. Please pay attention that you need to explictly calloutput.flush()before closing output stream.- Parameters:
input- non-null input stream, which will be closedoutput- non-null output stream, which will remain openbuffer- non-empty buffer- Returns:
- written bytes
- Throws:
IOException- when error occured reading from input stream or writing data to output stream
-
save
public static File save(InputStream in, int bufferSize, int timeout)
-
save
public static File save(File file, InputStream in, int bufferSize, int timeout, boolean deleteOnExit)
-
closeQuietly
protected void closeQuietly()
Closes the input stream quietly.
-
ensureOpen
protected void ensureOpen() throws IOExceptionChecks whether the input stream has been closed and throw an exception if it is.- Throws:
IOException- when the input stream has been closed
-
getUnderlyingFile
public ClickHouseFile getUnderlyingFile()
Gets underlying file.- Returns:
- non-null underlying file
-
peek
public abstract int peek() throws IOExceptionPeeks 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 remaining bytes and write into given output stream. Current input stream will be closed automatically at the end of writing, butoutputwill remain open.- 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 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
-
readBuffer
public ClickHouseByteBuffer readBuffer(int length) throws IOException
Reads byte buffer from the input stream.- Parameters:
length- byte length- Returns:
- non-null byte buffer
- Throws:
IOException- when failed to read bytes from input stream, not able to retrieve all bytes, 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[]), except 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
-
readCustom
public abstract ClickHouseByteBuffer readCustom(ClickHouseDataUpdater reader) throws IOException
Reads bytes using custom reader. Stream will be closed automatically when it reached end of stream. However, unlikereadBuffer(int), this method will never throwEOFException.- Parameters:
reader- non-null data reader- Returns:
- non-null byte buffer
- Throws:
IOException- when failed to read bytes from input stream, not able to retrieve all bytes, or reached end of the stream
-
readString
public String readString(Charset charset) throws IOException
Reads binary string from the input stream.readVarInt()will be first 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 binary 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 first 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 IOExceptionReads a varint from input stream.- Returns:
- varint
- Throws:
IOException- when failed to read value from input stream or reached end of the stream
-
readVarLong
public long readVarLong() throws IOExceptionReads 64-bit varint as long from input stream.- Returns:
- 64-bit varint
- Throws:
IOException- when failed to read value from input stream or reached end of the stream
-
setCopyToTarget
public final void setCopyToTarget(OutputStream out) throws IOException
Sets target output stream to copy bytes to. This is mainly used for testing, for example: dump input into a file while reading.- Parameters:
out- the output stream to write bytes to- Throws:
IOException- when failed to flush previous target or not able to write remaining bytes in buffer to the given output 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
-