Package com.clickhouse.data
Class ClickHouseInputStream
java.lang.Object
java.io.InputStream
com.clickhouse.data.ClickHouseInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable,Iterable<ClickHouseByteBuffer>
public abstract class ClickHouseInputStream
extends InputStream
implements Iterable<ClickHouseByteBuffer>
Extended input stream for read optimization. Methods like
readBuffer(int), readByte(), readBytes(int), and
readCustom(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
FieldsModifier and TypeFieldDescriptionprotected final ClickHouseByteBufferNon-null reusable byte buffer.protected booleanprotected OutputStreamprotected static final Stringprotected static final Stringprotected static final Stringprotected static final Stringprotected final RunnableOptional post close action.protected final ClickHousePassThruStreamUnderlying pass-thru stream.static final StringUser data shared between multiple calls. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedClickHouseInputStream(ClickHousePassThruStream stream, OutputStream copyTo, Runnable postCloseAction) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()protected voidCloses the input stream quietly.static ClickHouseInputStreamempty()Gets an empty input stream that produces nothing and cannot be closed.protected voidChecks whether the input stream has been closed and throw an exception if it is.protected abstract ClickHouseByteBufferGets reference to current byte buffer.Gets underlying file.Gets underlying stream.final ObjectgetUserData(String key) Gets user data associated with this input stream.final <T> TgetUserData(String key, T defaultValue) Gets user data associated with this input stream.booleanChecks if there's underlying input stream.booleanisClosed()Checks if the input stream has been closed or not.iterator()protected abstract ClickHouseByteBufferGets reference to next byte buffer available for read.static ClickHouseInputStreamof(byte[]... bytes) Wraps the given byte arrays.static ClickHouseInputStreamof(ClickHouseDataConfig config, ClickHouseWriter writer) Creates an input stream using the given customer writer.static ClickHouseInputStreamof(ClickHouseDeferredValue<InputStream> deferredInput, int bufferSize, Runnable postCloseAction) Wraps the deferred input stream.static ClickHouseInputStreamof(ClickHousePassThruStream stream, int bufferSize, Runnable postCloseAction) Wraps the given pass-thru stream as input stream.static ClickHouseInputStreamof(ClickHouseWriter writer) Creates an input stream using the given customer writer.static ClickHouseInputStreamWraps the given files.static ClickHouseInputStreamof(InputStream input) Wraps the given input stream.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, int compressionLevel, Runnable postCloseAction) Wraps the given input stream.static ClickHouseInputStreamof(InputStream input, int bufferSize, Runnable postCloseAction) Wraps the given input stream.static ClickHouseInputStreamof(InputStream input, ClickHouseCompression compression) Wraps the given input stream.static <T> ClickHouseInputStreamof(Iterable<T> source, Class<T> clazz, Function<T, byte[]> converter, int bufferSize, Runnable postCloseAction) Wraps the given iterable objects as byte array based binary input stream.static <T> ClickHouseInputStreamWraps the given iterable objects as byte array based binary input stream.static ClickHouseInputStreamWraps the given (UTF-8)strings.static ClickHouseInputStreamWraps 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> ClickHouseInputStreamWraps the given array of object as byte array based binary input stream.abstract intpeek()Peeks one byte.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.Reads ascii string from input stream.readAsciiString(int byteLength) Reads ascii string from input stream.booleanReads a byte as boolean.readBuffer(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.readString(int byteLength, Charset charset) Reads binary string from the input stream.readString(Charset charset) Reads binary string from the input stream.Reads unicode string from input stream.readUnicodeString(int byteLength) Reads unicode string from input stream.shortReads an unsigned byte from the input stream.intReads a varint from input stream.longReads 64-bit varint as long from input stream.final ObjectremoveUserData(String key) Removes user data.static Filesave(ClickHouseDataConfig config, InputStream input) Saves data from the given input stream to a temporary file, which will be deleted after JVM exited.static Filesave(ClickHouseDataConfig config, InputStream input, File file) Saves data from the given input stream to the specified file.static Filesave(File file, InputStream in, int bufferSize, int timeout, boolean deleteOnExit) Deprecated.static Filesave(InputStream input) Saves data from the given input stream to a temporary file, which will be deleted after JVM exited.static Filesave(InputStream in, int bufferSize, int timeout) Deprecated.will be dropped in 0.5, please usesave(InputStream)insteadstatic Filesave(InputStream input, File file) Saves data from the given input stream to the specified file.final voidSets target output stream to copy bytes to.final <T> TsetUserData(String key, T value) Sets user data.static ClickHouseInputStreamwrap(ClickHousePassThruStream stream, InputStream input, int bufferSize, ClickHouseCompression compression, int compressionLevel, Runnable postCloseAction) Wraps the given input stream.static ClickHouseInputStreamwrap(InputStream input, int bufferSize, long length, Runnable postCloseAction) Wraps the given input stream with length limitation.Methods inherited from class java.io.InputStream
available, mark, markSupported, nullInputStream, read, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferToMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
ERROR_INCOMPLETE_READ
- See Also:
-
ERROR_NULL_BYTES
- See Also:
-
ERROR_REUSE_BUFFER
- See Also:
-
ERROR_STREAM_CLOSED
- See Also:
-
TYPE_NAME
- See Also:
-
byteBuffer
Non-null reusable byte buffer. -
stream
Underlying pass-thru stream. -
postCloseAction
Optional post close action. -
userData
User data shared between multiple calls. -
closed
protected volatile boolean closed -
copyTo
-
-
Constructor Details
-
ClickHouseInputStream
protected ClickHouseInputStream(ClickHousePassThruStream stream, OutputStream copyTo, Runnable postCloseAction)
-
-
Method Details
-
wrap
public static ClickHouseInputStream wrap(ClickHousePassThruStream stream, InputStream input, int bufferSize, ClickHouseCompression compression, int compressionLevel, Runnable postCloseAction) Wraps the given input stream.- Parameters:
stream- pass-thru stream, could be nullinput- non-null input streambufferSize- buffer sizecompression- compression algorithmcompressionLevel- compression levelpostCloseAction- custom action will be performed right after closing the wrapped input stream- Returns:
- non-null wrapped input stream
-
wrap
public static ClickHouseInputStream wrap(InputStream input, int bufferSize, long length, Runnable postCloseAction) Wraps the given input stream with length limitation. Please pay attention that calling close() method of the wrapper will never close the inner input stream.- Parameters:
input- non-null input streambufferSize- buffer sizelength- maximum bytes can be read from the inputpostCloseAction- custom action will be performed right after closing the wrapped input stream- Returns:
- non-null wrapped input stream
-
empty
Gets an empty input stream that produces nothing and cannot be closed.- Returns:
- empty input stream
-
of
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 4096 or larger)postCloseAction- custom action will be performed right after closing the input stream- Returns:
- wrapped input
-
of
public static ClickHouseInputStream of(ClickHousePassThruStream stream, int bufferSize, Runnable postCloseAction) Wraps the given pass-thru stream as input stream.- Parameters:
stream- non-null pass-thru streambufferSize- buffer size which is always greater than zero(usually 4096 or larger)postCloseAction- custom action will be performed right after closing the input stream- Returns:
- wrapped input
-
of
Creates an input stream using the given customer writer. Behind the scene, a piped stream will be created, writer will be called in a separate worker thread for writing.- Parameters:
writer- non-null customer writer- Returns:
- wrapped input
-
of
Creates an input stream using the given customer writer. Behind the scene, a piped stream will be created, writer will be called in a separate worker thread for writing.- Parameters:
config- configuration, could be nullwriter- non-null customer writer- Returns:
- wrapped input
-
of
Wraps the given input stream.- Parameters:
input- input stream- Returns:
- wrapped input, or the same input if it's instance of
ClickHouseInputStream
-
of
Wraps the given input stream.- Parameters:
input- 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
Wraps the given input stream.- Parameters:
input- input streambufferSize- buffer size which is always greater than zero(usually 4096 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
Wraps the given input stream.- Parameters:
input- input streamcompression- 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, int compressionLevel, Runnable postCloseAction) Wraps the given input stream.- Parameters:
input- input streambufferSize- buffer size which is always greater than zero(usually 4096 or larger)compression- compression algorithm, null orClickHouseCompression.NONEmeans no compressioncompressionLevel- compression levelpostCloseAction- 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
Wraps the given byte arrays.- Parameters:
bytes- array of byte array- Returns:
- non-null input stream
- See Also:
-
of
Wraps the given byte buffers.- Parameters:
buffers- array of byte buffer- Returns:
- non-null input stream
- See Also:
-
of
Wraps the given files.- Parameters:
files- array of file- Returns:
- non-null input stream
- See Also:
-
of
Wraps the given input streams.- Parameters:
inputs- array of input stream- Returns:
- non-null input stream
- See Also:
-
of
Wraps the given (UTF-8)strings.- Parameters:
strings- array of string- Returns:
- non-null input stream
- See Also:
-
of
Wraps the given URLs.- Parameters:
urls- array of URL- Returns:
- non-null input stream
- See Also:
-
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
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
-
of
public static <T> ClickHouseInputStream of(Iterable<T> source, Class<T> clazz, Function<T, byte[]> converter, int bufferSize, 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 arraybufferSize- read buffer sizepostCloseAction- custom action will be performed right after closing the input stream- Returns:
- non-null input stream
-
pipe
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 asClickHouseDataConfig.DEFAULT_BUFFER_SIZE- Returns:
- written bytes
- Throws:
IOException- when error occured reading from input stream or writing data to output stream
-
pipe
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
Saves data from the given input stream to a temporary file, which will be deleted after JVM exited.- Parameters:
input- non-null input stream- Returns:
- non-null temporary file
-
save
Saves data from the given input stream to the specified file.- Parameters:
input- non-null input streamfile- target file, could be null- Returns:
- non-null file
-
save
Saves data from the given input stream to a temporary file, which will be deleted after JVM exited.- Parameters:
config- config, could be nullinput- non-null input stream- Returns:
- non-null temporary file
-
save
Saves data from the given input stream to the specified file.- Parameters:
config- config, could be nullinput- non-null input streamfile- target file, could be null- Returns:
- non-null file
-
save
Deprecated.will be dropped in 0.5, please usesave(InputStream)insteadSaves data from the given input stream to a temporary file.- Parameters:
in- non-null input streambufferSize- buffer sizetimeout- timeout in milliseconds- Returns:
- non-null temporary file
-
save
@Deprecated public static File save(File file, InputStream in, int bufferSize, int timeout, boolean deleteOnExit) Deprecated.will be dropped in 0.5, please usesave(InputStream, File)insteadSaves data from the given input stream to the specified file.- Parameters:
file- target file, could be nullin- non-null input streambufferSize- buffer sizetimeout- timeout in millisecondsdeleteOnExit- whether the file should be deleted after JVM exit- Returns:
- non-null file
-
closeQuietly
protected void closeQuietly()Closes the input stream quietly. -
ensureOpen
Checks whether the input stream has been closed and throw an exception if it is.- Throws:
IOException- when the input stream has been closed
-
getBuffer
Gets reference to current byte buffer.- Returns:
- non-null byte buffer
-
nextBuffer
Gets reference to next byte buffer available for read. An empty byte buffer will be returned (nextBuffer().isEmpty() == true), when it reaches end of the input stream.- Returns:
- non-null byte buffer
- Throws:
IOException
-
getUnderlyingFile
Gets underlying file. Same asClickHouseFile.of(getUnderlyingStream()).- Returns:
- non-null underlying file
-
getUnderlyingStream
Gets underlying stream.- Returns:
- non-null underlying stream
-
hasUnderlyingStream
public boolean hasUnderlyingStream()Checks if there's underlying input stream. Same asgetUnderlyingStream().hasInput().- Returns:
- true if there's underlying input stream; false otherwise
-
getUserData
Gets user data associated with this input stream.- Parameters:
key- key- Returns:
- value, could be null
-
getUserData
Gets user data associated with this input stream.- Type Parameters:
T- type of the value- Parameters:
key- keydefaultValue- default value- Returns:
- value, could be null
-
removeUserData
Removes user data.- Parameters:
key- key- Returns:
- removed user data, could be null
-
setUserData
Sets user data.- Parameters:
key- keyvalue- value- Returns:
- overidded value, could be null
-
peek
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
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
Reads 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
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
-
readBoolean
Reads a byte as boolean. The byte value can be either 0 (false) or 1 (true).- Returns:
- boolean value
- Throws:
IOException- when failed to read boolean value from input stream or reached end of the stream
-
readByte
Reads 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
Readslengthbytes 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
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
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
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
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
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
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
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
Reads a varint from input stream.- Returns:
- varint
- Throws:
IOException- when failed to read value from input stream or reached end of the stream
-
readVarLong
Reads 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
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
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
iterator
- Specified by:
iteratorin interfaceIterable<ClickHouseByteBuffer>
-
save(InputStream, File)instead