Module org.firebirdsql.jaybird
Package org.firebirdsql.gds.impl.wire

# Class XdrOutputStream

java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.io.BufferedOutputStream
org.firebirdsql.gds.impl.wire.XdrOutputStream

All Implemented Interfaces:
  `Closeable`, `Flushable`, `AutoCloseable`

public final class XdrOutputStream extends BufferedOutputStream
An `XdrOutputStream` writes data in XDR format to an underlying `java.io.OutputStream`.

This class is not thread-safe.

Author:
  Alejandro Alberola, David Jencks, Mark Rotteveel

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final int`
  `NULL_BYTE`
  `static final int`
  `SPACE_BYTE`

  ### Fields inherited from class java.io.BufferedOutputStream

  `buf, count`

  ### Fields inherited from class java.io.FilterOutputStream

  `out`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `XdrOutputStream(OutputStream out)`
  Create a new instance of `XdrOutputStream` with default buffer size.
  `XdrOutputStream(OutputStream out, int size)`
  Create a new instance of `XdrOutputStream` with the specified buffer size.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `close()`
  Close this stream and the underlying output stream.
  `void`
  `enableCompression()`
  Wraps the underlying stream for zlib compression.
  `void`
  `setCipher(Cipher cipher)`
  `void`
  `write(byte[] b, int off, int len)`
  Writes `len` bytes from the specified byte array starting at offset `off` to this output stream as defined by `OutputStream.write(byte[], int, int)`.
  `void`
  `write(byte[] b, int off, int len, int pad)`
  Write byte buffer `b` to the underlying output stream in XDR format.
  `void`
  `writeAlignment(int len)`
  Writes the `0x00` alignment for the specified length.
  `void`
  `writeBuffer(byte[] buf)`
  Write byte buffer `buf` to the underlying output stream in XDR format.
  `void`
  `writeBuffer(byte[] buf, int off, int len)`
  Write byte buffer `buf` from offset `off` for `len` bytes to the underlying output stream in XDR format.
  `void`
  `writeDirect(byte[] data)`
  Writes directly to the `OutputStream` of the underlying socket.
  `void`
  `writeInt(int v)`
  Write an `int` value to the underlying stream in XDR format.
  `void`
  `writeLong(long v)`
  Write a `long` value to the underlying stream in XDR format.
  `void`
  `writePadding(int len, int padByte)`
  Writes padding for the specified length of the specified padding byte.
  `void`
  `writeSpacePadding(int len)`
  Writes space (`0x20`) padding of the specified length
  `void`
  `writeString(String s, Encoding encoding)`
  Write content of the specified string using the specified encoding.
  `void`
  `writeTyped(int type, Xdrable item)`
  Write an `Xdrable` to this output stream.
  `void`
  `writeTyped(ParameterBuffer parameterBuffer)`
  `void`
  `writeZeroPadding(int len)`
  Writes zero padding of the specified length

  ### Methods inherited from class java.io.BufferedOutputStream

  `flush, write`

  ### Methods inherited from class java.io.FilterOutputStream

  `write`

  ### Methods inherited from class java.io.OutputStream

  `nullOutputStream`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### SPACE_BYTE

    public static final int SPACE_BYTE
    See Also:
      - Constant Field Values

  - ### NULL_BYTE

    public static final int NULL_BYTE
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### XdrOutputStream

    public XdrOutputStream(OutputStream out)
    Create a new instance of `XdrOutputStream` with default buffer size.
    Parameters:
      `out` - underlying `OutputStream` to write to

  - ### XdrOutputStream

    public XdrOutputStream(OutputStream out, int size)
    Create a new instance of `XdrOutputStream` with the specified buffer size.
    Parameters:
      `out` - underlying `OutputStream` to write to
      `size` - size of the buffer (implementation uses a minimum of 8 bytes)

- ## Method Details

  - ### writeAlignment

    public void writeAlignment(int len) throws IOException
    Writes the `0x00` alignment for the specified length. This padding is calculated as `(4 - len) & 3`.
    Parameters:
      `len` - length of the previously written buffer to pad
    Throws:
      `IOException` - if an error occurs while writing to the underlying output stream

  - ### writeZeroPadding

    public void writeZeroPadding(int len) throws IOException
    Writes zero padding of the specified length
    Parameters:
      `len` - length to write
    Throws:
      `IOException` - if an error occurs while writing to the underlying output stream
    See Also:
      - `writePadding(int, int)`

  - ### writeSpacePadding

    public void writeSpacePadding(int len) throws IOException
    Writes space (`0x20`) padding of the specified length
    Parameters:
      `len` - length to write
    Throws:
      `IOException` - if an error occurs while writing to the underlying output stream
    See Also:
      - `writePadding(int, int)`

  - ### writePadding

    public void writePadding(int len, int padByte) throws IOException
    Writes padding for the specified length of the specified padding byte.

    Prefer using the more specific `writeZeroPadding(int)` and `writeZeroPadding(int)`.

    Parameters:
      `len` - length of padding to write
      `padByte` - padding byte to use
    Throws:
      `IOException` - if an error occurs while writing to the underlying output stream
    See Also:
      - `writeSpacePadding(int)`
      - `writeZeroPadding(int)`

  - ### writeBuffer

    public void writeBuffer(byte[] buf) throws IOException
    Write byte buffer `buf` to the underlying output stream in XDR format.
    Parameters:
      `buf` - byte buffer to be written
    Throws:
      `IOException` - if an error occurs while writing to the underlying output stream

  - ### writeBuffer

    public void writeBuffer(byte[] buf, int off, int len) throws IOException
    Write byte buffer `buf` from offset `off` for `len` bytes to the underlying output stream in XDR format.
    Parameters:
      `buf` - byte buffer to be written
      `off` - offset to start
      `len` - length to write
    Throws:
      `IOException` - if an error occurs while writing to the underlying output stream
      `IndexOutOfBoundsException` - If `off` is negative, `len` is negative, or `len` is greater than `buff.length - off`

  - ### writeString

    public void writeString(String s, Encoding encoding) throws IOException
    Write content of the specified string using the specified encoding.
    Throws:
      `IOException`

  - ### writeTyped

    public void writeTyped(int type, Xdrable item) throws IOException
    Write an `Xdrable` to this output stream.
    Parameters:
      `type` - type of the `Xdrable` to be written, e.g. `ISCConstants.isc_tpb_version3`
      `item` - object to be written
    Throws:
      `IOException` - if an error occurs while writing to the underlying output stream

  - ### writeTyped

    public void writeTyped(ParameterBuffer parameterBuffer) throws IOException
    Throws:
      `IOException`

  - ### writeLong

    public void writeLong(long v) throws IOException
    Write a `long` value to the underlying stream in XDR format.
    Parameters:
      `v` - `long` value to be written
    Throws:
      `IOException` - if an error occurs while writing to the underlying output stream

  - ### writeInt

    public void writeInt(int v) throws IOException
    Write an `int` value to the underlying stream in XDR format.
    Parameters:
      `v` - `int` value to be written
    Throws:
      `IOException` - if an error occurs while writing to the underlying output stream

  - ### write

    public void write(byte[] b, int off, int len, int pad) throws IOException
    Write byte buffer `b` to the underlying output stream in XDR format.
    Parameters:
      `b` - byte buffer to be written
      `off` - start offset in the buffer
      `len` - number of bytes to write
      `pad` - number of (blank) padding bytes to write
    Throws:
      `IOException` - if an error occurs while writing to the underlying output stream

  - ### write

    public void write(byte[] b, int off, int len) throws IOException
    Writes `len` bytes from the specified byte array starting at offset `off` to this output stream as defined by `OutputStream.write(byte[], int, int)`.

    Important: do not confuse this method with `write(byte[], int, int, int)` which originally had the signature of this method.

    Overrides:
      `write` in class `BufferedOutputStream`
    Parameters:
      `b` - data
      `off` - start offset in the data
      `len` - number of bytes to write
    Throws:
      `IOException` - if an error occurs while writing to the underlying output stream

  - ### close

    public void close() throws IOException
    Close this stream and the underlying output stream.
    Specified by:
      `close` in interface `AutoCloseable`
    Specified by:
      `close` in interface `Closeable`
    Overrides:
      `close` in class `FilterOutputStream`
    Throws:
      `IOException` - if an error occurs while closing the underlying stream

  - ### enableCompression

    @InternalApi public void enableCompression() throws IOException
    Wraps the underlying stream for zlib compression.
    Throws:
      `IOException` - If the underlying stream is already set up for compression

  - ### setCipher

    public void setCipher(Cipher cipher) throws IOException
    Throws:
      `IOException`

  - ### writeDirect

    public void writeDirect(byte[] data) throws IOException
    Writes directly to the `OutputStream` of the underlying socket.
    Parameters:
      `data` - data to write
    Throws:
      `IOException` - for errors writing to the socket