Class ClickHouseOutputStream

java.lang.Object
java.io.OutputStream
com.clickhouse.data.ClickHouseOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
Direct Known Subclasses:
ClickHousePipedOutputStream

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 Details

  • Constructor Details

  • Method Details

    • 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(ClickHouseDeferredValue<OutputStream> deferredOutput, int bufferSize, Runnable postCloseAction)
      Wraps the deferred output stream.
      Parameters:
      deferredOutput - non-null deferred output stream
      bufferSize - 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 stream
      postCloseAction - custom action will be performed right after closing the output stream
      bufferSize - buffer size which is always greater than zero(usually 4096 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 stream
      bufferSize - 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 stream
      bufferSize - buffer size which is always greater than zero(usually 4096 or larger)
      compression - compression algorithm, null or ClickHouseCompression.NONE means no compression
      level - compression level
      postCloseAction - 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
    • getUnderlyingStream

      public ClickHousePassThruStream getUnderlyingStream()
      Gets underlying stream.
      Returns:
      non-null underlying stream
    • hasUnderlyingStream

      public boolean hasUnderlyingStream()
      Checks if there's underlying output stream. Same as getUnderlyingStream().hasOutput().
      Returns:
      true if there's underlying output stream; false otherwise
    • transferBytes

      @Deprecated public final ClickHouseOutputStream transferBytes(byte[] bytes) throws IOException
      Deprecated.
      will be dropped in 0.5, please use writeBuffer(ClickHouseByteBuffer) instead
      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 operate on a closed stream
    • transferBytes

      @Deprecated public ClickHouseOutputStream transferBytes(ByteBuffer buffer, int length) throws IOException
      Deprecated.
      will be dropped in 0.5, please use writeBuffer(ClickHouseByteBuffer) instead
      Transfer bytes into output stream without creating a copy.
      Parameters:
      buffer - non-null byte buffer
      length - 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 use writeBuffer(ClickHouseByteBuffer) instead
      Transfers the given bytes into output stream, without creating a copy as in writeBytes(byte[], int, int).
      Parameters:
      bytes - non-null byte array
      offset - offset of the byte array
      length - 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

      public final void write(int b) throws IOException
      Specified by:
      write in class OutputStream
      Throws:
      IOException
    • write

      public final void write(byte[] b) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • write

      public final void write(byte[] b, int off, int len) throws IOException
      Overrides:
      write in class OutputStream
      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:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException
    • writeBoolean

      public ClickHouseOutputStream writeBoolean(boolean b) throws IOException
      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

      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 operate on a closed stream
    • writeBytes

      public ClickHouseOutputStream writeBytes(ByteBuffer buffer, int length) throws IOException
      Writes bytes into output stream.
      Parameters:
      buffer - non-null byte buffer
      length - 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

      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 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 array
      offset - offset of the byte array
      length - 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

      public ClickHouseOutputStream writeBuffer(ClickHouseByteBuffer buffer) throws IOException
      Writes bytes into output stream. Unlike writeBytes(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

      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 operate on a closed stream
    • writeString

      public ClickHouseOutputStream writeString(String value, Charset charset) throws IOException
      Writes string into the output stream. Nothing will happen when value is null or empty.
      Parameters:
      value - string to write
      charset - charset, null is treated as StandardCharsets.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

      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 operate 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 operate 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 operate 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 operate on a closed stream