Enum Class StreamOutputHelper

java.lang.Object
java.lang.Enum<StreamOutputHelper>
org.elasticsearch.common.io.stream.StreamOutputHelper
All Implemented Interfaces:
Serializable, Comparable<StreamOutputHelper>, Constable

public enum StreamOutputHelper extends Enum<StreamOutputHelper>
Utilities for implementing a StreamOutput, for cases where performance is not a concern.
  • Method Details

    • values

      public static StreamOutputHelper[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static StreamOutputHelper valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null
    • getThreadLocalScratchBuffer

      public static byte[] getThreadLocalScratchBuffer()
      Returns:
      a thread-local byte[] that can be used to collect some data which is then passed to StreamOutput.write(int).

      This is almost certainly more efficient than calling StreamOutput.writeByte(byte) repeatedly, but accessing a thread-local is not super-cheap so it is typically less efficient than writing the bytes directly into the buffer underneath the StreamOutput if it has such a buffer.

    • writeString

      public static int writeString(String str, OutputStream outputStream) throws IOException
      Write string using the default thread-local scratch buffer, as described in StreamOutput.writeString(java.lang.String).

      This is almost certainly more efficient than calling StreamOutput.writeByte(byte) repeatedly, but accessing a thread-local is not super-cheap so it is typically less efficient than writing the bytes directly into the buffer underneath the StreamOutput if it has such a buffer.

      Parameters:
      str - string to write
      outputStream - the stream to which to write the data after buffering.
      Returns:
      number of bytes written.
      Throws:
      IOException - on failure
    • writeString

      public static int writeString(String str, byte[] buffer, int prefixLength, OutputStream outputStream) throws IOException
      Write string prefixed by some number of bytes (possibly zero) from the beginning of the given buffer. The given buffer will also be used when encoding the given string. This is almost certainly more efficient than calling StreamOutput.writeByte(byte) repeatedly, but less efficient than writing the bytes directly into the buffer underneath the StreamOutput if it has such a buffer.
      Parameters:
      str - string to write
      buffer - buffer that may hold some bytes to write
      prefixLength - how many bytes in {code buffer} to write
      outputStream - the stream to which to write the data after buffering.
      Returns:
      number of bytes written.
      Throws:
      IOException - on failure
    • writeOptionalString

      public static int writeOptionalString(@Nullable String str, OutputStream outputStream) throws IOException
      Write possibly-null string using the default thread-local scratch buffer, as described in StreamOutput.writeOptionalString(java.lang.String).

      This is almost certainly more efficient than calling StreamOutput.writeByte(byte) repeatedly, but accessing a thread-local is not super-cheap so it is typically less efficient than writing the bytes directly into the buffer underneath the StreamOutput if it has such a buffer.

      Parameters:
      str - string to write
      outputStream - the stream to which to write the data after buffering.
      Returns:
      number of bytes written.
      Throws:
      IOException - on failure
    • writeGenericString

      public static int writeGenericString(String value, OutputStream outputStream) throws IOException
      Write generic string using the default thread-local scratch buffer, as described in StreamOutput.writeGenericString(java.lang.String).

      This is almost certainly more efficient than calling StreamOutput.writeByte(byte) repeatedly, but accessing a thread-local is not super-cheap so it is typically less efficient than writing the bytes directly into the buffer underneath the StreamOutput if it has such a buffer.

      Parameters:
      value - string to write
      outputStream - the stream to which to write the data after buffering.
      Returns:
      number of bytes written.
      Throws:
      IOException - on failure
    • putVInt

      public static int putVInt(byte[] buffer, int i, int offset)
      Put the integer i into the given buffer starting at the given offset, formatted as per StreamOutput.writeVInt(int). Performs no bounds checks: callers must verify that there is enough space in buffer first.
      Returns:
      updated offset (original offset plus number of bytes written)
    • putMultiByteVInt

      public static int putMultiByteVInt(byte[] buffer, int i, int offset)
      Put the integer i into the given buffer starting at the given offset, formatted as per StreamOutput.writeVInt(int). Performs no bounds checks: callers must verify that there is enough space in buffer first.
      Returns:
      updated offset (original offset plus number of bytes written)