Package com.clickhouse.client
Class ClickHouseOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- com.clickhouse.client.ClickHouseOutputStream
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
AbstractByteArrayOutputStream,ClickHousePipedOutputStream,EmptyOutputStream
public abstract class ClickHouseOutputStream extends OutputStream
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 Modifier and Type Field Description protected booleanclosedprotected static StringERROR_INCOMPLETE_READprotected static StringERROR_NULL_BYTESprotected static StringERROR_REUSE_BUFFERprotected static StringERROR_STREAM_CLOSEDprotected ClickHouseFilefileprotected RunnablepostCloseAction
-
Constructor Summary
Constructors Modifier Constructor Description protectedClickHouseOutputStream(ClickHouseFile file, Runnable postCloseAction)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclose()static ClickHouseOutputStreamempty()Gets an empty output stream that consumes nothing and cannot be closed.protected voidensureOpen()ClickHouseFilegetUnderlyingFile()Gets underlying file.booleanisClosed()Checks if the output stream has been closed or not.static ClickHouseOutputStreamof(ClickHouseFile file, int bufferSize, Runnable postCloseAction)Wraps the given file 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, Runnable postCloseAction)Wraps the given output stream.ClickHouseOutputStreamtransferBytes(byte[] bytes)Transfers bytes into output stream without creating a copy.abstract ClickHouseOutputStreamtransferBytes(byte[] bytes, int offset, int length)Transfers the given bytes into output stream, without creating a copy as inwriteBytes(byte[], int, int).ClickHouseOutputStreamtransferBytes(ByteBuffer buffer, int length)Transfer bytes into output stream without creating a copy.voidwrite(byte[] b)voidwrite(byte[] b, int off, int len)voidwrite(int b)ClickHouseOutputStreamwriteAsciiString(String value)Writes ascii string into output stream.ClickHouseOutputStreamwriteBuffer(ClickHouseByteBuffer buffer)Writes bytes into output stream.abstract ClickHouseOutputStreamwriteByte(byte b)Writes a single byte into output stream.ClickHouseOutputStreamwriteBytes(byte[] bytes)Writes bytes into output stream.abstract ClickHouseOutputStreamwriteBytes(byte[] bytes, int offset, int length)Writes copy of given bytes into output stream.ClickHouseOutputStreamwriteBytes(ByteBuffer buffer, int length)Writes bytes into output stream.abstract ClickHouseOutputStreamwriteCustom(ClickHouseDataUpdater writer)Writes bytes using custom writer.ClickHouseOutputStreamwriteString(String value, Charset charset)Writes string into the output stream.ClickHouseOutputStreamwriteUnicodeString(String value)Writes unicode string into output stream.ClickHouseOutputStreamwriteUnsignedVarInt(long value)Writes varint into output stream.ClickHouseOutputStreamwriteVarInt(int value)Writes varint into output stream.-
Methods inherited from class java.io.OutputStream
flush, nullOutputStream
-
-
-
-
Field Detail
-
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
-
file
protected final ClickHouseFile file
-
postCloseAction
protected final Runnable postCloseAction
-
closed
protected boolean closed
-
-
Constructor Detail
-
ClickHouseOutputStream
protected ClickHouseOutputStream(ClickHouseFile file, Runnable postCloseAction)
-
-
Method Detail
-
empty
public static ClickHouseOutputStream empty()
Gets an empty output stream that consumes nothing and cannot be closed.- Returns:
- empty output stream
-
of
public static ClickHouseOutputStream of(ClickHouseFile file, int bufferSize, Runnable postCloseAction)
Wraps the given file as output stream.- Parameters:
file- non-null filepostCloseAction- custom action will be performed right after closing the output streambufferSize- buffer size which is always greater than zero(usually 8192 or larger)- Returns:
- wrapped output
-
of
public static ClickHouseOutputStream of(OutputStream output)
Wraps the given output stream.- Parameters:
output- output stream- Returns:
- wrapped output, or the same output if it's instance of
ClickHouseOutputStream
-
of
public static ClickHouseOutputStream of(OutputStream output, int bufferSize)
Wraps the given output stream.- Parameters:
output- output streambufferSize- buffer size which is always greater than zero(usually 8192 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, Runnable postCloseAction)
Wraps the given output stream.- Parameters:
output- output 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 output stream- Returns:
- wrapped output, or the same output if it's instance of
ClickHouseOutputStream
-
ensureOpen
protected void ensureOpen() throws IOException- Throws:
IOException
-
getUnderlyingFile
public ClickHouseFile getUnderlyingFile()
Gets underlying file.- Returns:
- non-null underlying file
-
transferBytes
public final ClickHouseOutputStream transferBytes(byte[] bytes) throws IOException
Transfers 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 opereate on a closed stream
-
transferBytes
public ClickHouseOutputStream transferBytes(ByteBuffer buffer, int length) throws IOException
Transfer 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 opereate on a closed stream
-
transferBytes
public abstract ClickHouseOutputStream transferBytes(byte[] bytes, int offset, int length) throws IOException
Transfers 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 opereate on a closed stream
-
write
public final void write(int b) throws IOException- Specified by:
writein classOutputStream- Throws:
IOException
-
write
public final void write(byte[] b) throws IOException- Overrides:
writein classOutputStream- Throws:
IOException
-
write
public final void write(byte[] b, int off, int len) throws IOException- 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
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
writeByte
public abstract ClickHouseOutputStream writeByte(byte b) throws IOException
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 opereate on a closed stream
-
writeBytes
public ClickHouseOutputStream writeBytes(ByteBuffer buffer, int length) throws IOException
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 opereate on a closed stream
-
writeBytes
public final ClickHouseOutputStream writeBytes(byte[] bytes) throws IOException
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 opereate 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 opereate on a closed stream
-
writeBuffer
public ClickHouseOutputStream writeBuffer(ClickHouseByteBuffer buffer) throws IOException
Writes bytes into output stream.- 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 opereate on a closed stream
-
writeCustom
public abstract ClickHouseOutputStream writeCustom(ClickHouseDataUpdater writer) throws IOException
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 opereate on a closed stream
-
writeString
public ClickHouseOutputStream writeString(String value, Charset charset) throws IOException
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 opereate on a closed stream
-
writeAsciiString
public ClickHouseOutputStream writeAsciiString(String value) throws IOException
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 opereate on a closed stream
-
writeUnicodeString
public ClickHouseOutputStream writeUnicodeString(String value) throws IOException
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 opereate on a closed stream
-
writeVarInt
public ClickHouseOutputStream writeVarInt(int value) throws IOException
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 opereate on a closed stream
-
writeUnsignedVarInt
public ClickHouseOutputStream writeUnsignedVarInt(long value) throws IOException
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 opereate on a closed stream
-
-