Package com.clickhouse.data
Class ClickHouseOutputStream
java.lang.Object
java.io.OutputStream
com.clickhouse.data.ClickHouseOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
ClickHousePipedOutputStream
Extended output stream for write optimization. It also acts as a factory
class providing static methods for creating output stream as needed.
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedClickHouseOutputStream(ClickHousePassThruStream stream, Runnable postCloseAction) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()static ClickHouseOutputStreamempty()Gets an empty output stream that consumes nothing and cannot be closed.protected voidGets underlying file.Gets underlying stream.booleanChecks if there's underlying output stream.booleanisClosed()Checks if the output stream has been closed or not.static ClickHouseOutputStreamof(ClickHouseDeferredValue<OutputStream> deferredOutput, int bufferSize, Runnable postCloseAction) Wraps the deferred output stream.static ClickHouseOutputStreamof(ClickHousePassThruStream stream, int bufferSize, Runnable postCloseAction) Wraps the given pass-thru stream as output stream.static ClickHouseOutputStreamof(OutputStream output) Wraps the given output stream.static ClickHouseOutputStreamof(OutputStream output, int bufferSize) Wraps the given output stream.static ClickHouseOutputStreamof(OutputStream output, int bufferSize, ClickHouseCompression compression, int level, Runnable postCloseAction) Wraps the given output stream.final ClickHouseOutputStreamtransferBytes(byte[] bytes) Deprecated.abstract ClickHouseOutputStreamtransferBytes(byte[] bytes, int offset, int length) Deprecated.will be dropped in 0.5, please usewriteBuffer(ClickHouseByteBuffer)insteadtransferBytes(ByteBuffer buffer, int length) Deprecated.will be dropped in 0.5, please usewriteBuffer(ClickHouseByteBuffer)insteadfinal voidwrite(byte[] b) final voidwrite(byte[] b, int off, int len) final voidwrite(int b) writeAsciiString(String value) Writes ascii string into output stream.writeBoolean(boolean b) Writes a byte (true=1, false=0) into output stream.writeBuffer(ClickHouseByteBuffer buffer) Writes bytes into output stream.abstract ClickHouseOutputStreamwriteByte(byte b) Writes a single byte into output stream.final ClickHouseOutputStreamwriteBytes(byte[] bytes) Writes bytes into output stream.abstract ClickHouseOutputStreamwriteBytes(byte[] bytes, int offset, int length) Writes copy of given bytes into output stream.writeBytes(ByteBuffer buffer, int length) Writes bytes into output stream.abstract ClickHouseOutputStreamwriteCustom(ClickHouseDataUpdater writer) Writes bytes using custom writer.writeString(String value, Charset charset) Writes string into the output stream.writeUnicodeString(String value) Writes unicode string into output stream.writeUnsignedVarInt(long value) Writes varint into output stream.writeVarInt(int value) Writes varint into output stream.Methods inherited from class java.io.OutputStream
flush, nullOutputStream
-
Field Details
-
ERROR_NULL_BYTES
- See Also:
-
ERROR_REUSE_BUFFER
- See Also:
-
ERROR_STREAM_CLOSED
- See Also:
-
TYPE_NAME
- See Also:
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE- See Also:
-
stream
-
postCloseAction
-
closed
protected volatile boolean closed
-
-
Constructor Details
-
ClickHouseOutputStream
-
-
Method Details
-
empty
Gets an empty output stream that consumes nothing and cannot be closed.- Returns:
- empty output stream
-
of
public static ClickHouseOutputStream of(ClickHouseDeferredValue<OutputStream> deferredOutput, int bufferSize, Runnable postCloseAction) Wraps the deferred output stream.- Parameters:
deferredOutput- non-null deferred output 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 output
-
of
public static ClickHouseOutputStream of(ClickHousePassThruStream stream, int bufferSize, Runnable postCloseAction) Wraps the given pass-thru stream as output stream.- Parameters:
stream- non-null pass-thru streampostCloseAction- custom action will be performed right after closing the output streambufferSize- buffer size which is always greater than zero(usually 4096 or larger)- Returns:
- wrapped output
-
of
Wraps the given output stream.- Parameters:
output- output stream- Returns:
- wrapped output, or the same output if it's instance of
ClickHouseOutputStream
-
of
Wraps the given output stream.- Parameters:
output- output streambufferSize- buffer size which is always greater than zero(usually 4096 or larger)- Returns:
- wrapped output, or the same output if it's instance of
ClickHouseOutputStream
-
of
public static ClickHouseOutputStream of(OutputStream output, int bufferSize, ClickHouseCompression compression, int level, Runnable postCloseAction) Wraps the given output stream.- Parameters:
output- output streambufferSize- buffer size which is always greater than zero(usually 4096 or larger)compression- compression algorithm, null orClickHouseCompression.NONEmeans no compressionlevel- compression levelpostCloseAction- custom action will be performed right after closing the output stream- Returns:
- wrapped output, or the same output if it's instance of
ClickHouseOutputStream
-
ensureOpen
- Throws:
IOException
-
getUnderlyingFile
Gets underlying file.- Returns:
- non-null underlying file
-
getUnderlyingStream
Gets underlying stream.- Returns:
- non-null underlying stream
-
hasUnderlyingStream
public boolean hasUnderlyingStream()Checks if there's underlying output stream. Same asgetUnderlyingStream().hasOutput().- Returns:
- true if there's underlying output stream; false otherwise
-
transferBytes
Deprecated.will be dropped in 0.5, please usewriteBuffer(ClickHouseByteBuffer)insteadTransfers bytes into output stream without creating a copy.- Parameters:
bytes- non-null byte array- Returns:
- current output stream
- Throws:
IOException- when failed to write value into output stream, not able to sent all bytes, or operate on a closed stream
-
transferBytes
@Deprecated public ClickHouseOutputStream transferBytes(ByteBuffer buffer, int length) throws IOException Deprecated.will be dropped in 0.5, please usewriteBuffer(ClickHouseByteBuffer)insteadTransfer bytes into output stream without creating a copy.- Parameters:
buffer- non-null byte bufferlength- bytes to write- Returns:
- current output stream
- Throws:
IOException- when failed to write value into output stream, not able to sent all bytes, or operate on a closed stream
-
transferBytes
@Deprecated public abstract ClickHouseOutputStream transferBytes(byte[] bytes, int offset, int length) throws IOException Deprecated.will be dropped in 0.5, please usewriteBuffer(ClickHouseByteBuffer)insteadTransfers the given bytes into output stream, without creating a copy as inwriteBytes(byte[], int, int).- Parameters:
bytes- non-null byte arrayoffset- offset of the byte arraylength- bytes to write- Returns:
- current output stream
- Throws:
IOException- when failed to write value into output stream, not able to sent all bytes, or operate on a closed stream
-
write
- Specified by:
writein classOutputStream- Throws:
IOException
-
write
- Overrides:
writein classOutputStream- Throws:
IOException
-
write
- Overrides:
writein classOutputStream- Throws:
IOException
-
isClosed
public boolean isClosed()Checks if the output stream has been closed or not.- Returns:
- true if the output stream has been closed; false otherwise
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
writeBoolean
Writes a byte (true=1, false=0) into output stream.- Parameters:
b- boolean value to write- Returns:
- current output stream
- Throws:
IOException- when failed to write value into output stream, not able to sent all bytes, or operate on a closed stream
-
writeByte
Writes a single byte into output stream.- Parameters:
b- byte to write- Returns:
- current output stream
- Throws:
IOException- when failed to write value into output stream, not able to sent all bytes, or operate on a closed stream
-
writeBytes
Writes bytes into output stream.- Parameters:
buffer- non-null byte bufferlength- bytes to write- Returns:
- current output stream
- Throws:
IOException- when failed to write value into output stream, not able to sent all bytes, or operate on a closed stream
-
writeBytes
Writes bytes into output stream.- Parameters:
bytes- non-null byte array- Returns:
- current output stream
- Throws:
IOException- when failed to write value into output stream, not able to sent all bytes, or operate on a closed stream
-
writeBytes
public abstract ClickHouseOutputStream writeBytes(byte[] bytes, int offset, int length) throws IOException Writes copy of given bytes into output stream.- Parameters:
bytes- non-null byte arrayoffset- offset of the byte arraylength- bytes to write- Returns:
- current output stream
- Throws:
IOException- when failed to write value into output stream, not able to sent all bytes, or operate on a closed stream
-
writeBuffer
Writes bytes into output stream. UnlikewriteBytes(byte[], int, int), which always writes bytes into a fixed-size buffer first, this may skip the internal buffer for less memory footprint and better performance.- Parameters:
buffer- wrapped byte array with offset and limit- Returns:
- current output stream
- Throws:
IOException- when failed to write value into output stream, not able to sent all bytes, or operate on a closed stream
-
writeCustom
Writes bytes using custom writer.- Parameters:
writer- non-null data writer- Returns:
- current output stream
- Throws:
IOException- when failed to write value into output stream, not able to sent all bytes, or operate on a closed stream
-
writeString
Writes string into the output stream. Nothing will happen whenvalueis null or empty.- Parameters:
value- string to writecharset- charset, null is treated asStandardCharsets.UTF_8- Returns:
- this output stream
- Throws:
IOException- when failed to write value into output stream, not able to sent all bytes, or operate on a closed stream
-
writeAsciiString
Writes ascii string into output stream.writeVarInt(int)will be called automatically before writing the string.- Parameters:
value- ascii string to write- Returns:
- this output stream
- Throws:
IOException- when failed to write value into output stream, not able to sent all bytes, or operate on a closed stream
-
writeUnicodeString
Writes unicode string into output stream.writeVarInt(int)will be called automatically before writing the string.- Parameters:
value- unicode string to write- Returns:
- this output stream
- Throws:
IOException- when failed to write value into output stream, not able to sent all bytes, or operate on a closed stream
-
writeVarInt
Writes varint into output stream.- Parameters:
value- varint- Returns:
- this output stream
- Throws:
IOException- when failed to write value into output stream, not able to sent all bytes, or operate on a closed stream
-
writeUnsignedVarInt
Writes varint into output stream.- Parameters:
value- varint- Returns:
- this output stream
- Throws:
IOException- when failed to write value into output stream, not able to sent all bytes, or operate on a closed stream
-
writeBuffer(ClickHouseByteBuffer)instead