com.h3c.util.net

## Class ByteMessageTransfer

- java.lang.Object
- - com.h3c.util.net.ByteMessageTransfer

-     public class ByteMessageTransfer
      extends Object

  Utility for writing and consuming length-prefixed messages from the specified output or input stream. The length is specified as the leading two bytes immediately after the two start-of-frame bytes.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `DEFAULT_MAX_BUFFER_SIZE`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ByteMessageTransfer()` Create a new transfer setup to deal with messages of default maximum size, which is 1024 bytes.
    `ByteMessageTransfer(int maxBufferSize)` Create a new transfer setup to deal with messages of specified maximum size.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `getMaxBufferSize()` Get the current maximum buffer size supported by this byte transfer.
    `byte[]` | `read(InputStream input)` Read length-prefixed data from the specified input stream and return a newly allocated byte buffer that holds the data.
    `int` | `readSOF(InputStream input)` Read and validate the length-prefixed start of frame from the specified input stream and return the decoded length of the expected message.
    `void` | `setMaxBufferSize(int maxBufferSize)` Set the current maximum buffer size supported by this byte transfer.
    `void` | `write(OutputStream output, byte[] data)` Write the given byte buffer to the specified output stream using the length-prefixed transfer.
    `void` | `write(OutputStream output, byte[] data, int offset, int length)` Write the given section of a byte buffer to the specified output stream using the length-prefixed transfer.
    `void` | `write(OutputStream output, Writable w)` Write the given writable to the specified output stream using the length-prefixed transfer.
    `int` | `writeSOF(OutputStream output, int length)` Write a start of frame of the length-prefixed message.

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

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

- - ### Field Detail

    - #### DEFAULT_MAX_BUFFER_SIZE

          public static final int DEFAULT_MAX_BUFFER_SIZE

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### ByteMessageTransfer

          public ByteMessageTransfer()

      Create a new transfer setup to deal with messages of default maximum size, which is 1024 bytes.

    - #### ByteMessageTransfer

          public ByteMessageTransfer(int maxBufferSize)

      Create a new transfer setup to deal with messages of specified maximum size.
      Parameters:
        `maxBufferSize` - maximum size of messages processed by this transfer

  - ### Method Detail

    - #### getMaxBufferSize

          public int getMaxBufferSize()

      Get the current maximum buffer size supported by this byte transfer.
      Returns:
        maximum number of bytes allowed in a single transfer

    - #### setMaxBufferSize

          public void setMaxBufferSize(int maxBufferSize)

      Set the current maximum buffer size supported by this byte transfer.
      Parameters:
        `maxBufferSize` - maximum number of bytes allowed in a single transfer

    - #### write

          public void write(OutputStream output,
                   byte[] data)
                     throws IOException

      Write the given byte buffer to the specified output stream using the length-prefixed transfer.
      Parameters:
        `output` - output stream to write to
        `data` - byte buffer to write
      Throws:
        `IOException` - if issues encountered writing the message

    - #### writeSOF

          public int writeSOF(OutputStream output,
                     int length)
                       throws IOException

      Write a start of frame of the length-prefixed message.
      Parameters:
        `output` - output stream to write to
        `length` - size of the message being written
      Returns:
        number of bytes to be written after the header
      Throws:
        `IOException` - if issues encountered writing the header

    - #### write

          public void write(OutputStream output,
                   byte[] data,
                   int offset,
                   int length)
                     throws IOException

      Write the given section of a byte buffer to the specified output stream using the length-prefixed transfer.
      Parameters:
        `output` - output stream to write to
        `data` - byte buffer to write
        `offset` - offset at which to start the transfer
        `length` - number of bytes to transfer
      Throws:
        `IOException` - if issues encountered writing the data

    - #### write

          public void write(OutputStream output,
                   Writable w)
                     throws IOException

      Write the given writable to the specified output stream using the length-prefixed transfer.
      Parameters:
        `output` - output stream to write to
        `w` - writable object to write
      Throws:
        `IOException` - if issues encountered writing the message

    - #### readSOF

          public int readSOF(InputStream input)
                      throws IOException

      Read and validate the length-prefixed start of frame from the specified input stream and return the decoded length of the expected message.
      Parameters:
        `input` - input stream from which to read
      Returns:
        length of the expected message
      Throws:
        `IOException` - if issues encountered reading the header

    - #### read

          public byte[] read(InputStream input)
                      throws IOException

      Read length-prefixed data from the specified input stream and return a newly allocated byte buffer that holds the data.
      Parameters:
        `input` - input stream from which to read
      Returns:
        byte buffer with read data
      Throws:
        `IOException` - if issues encountered reading the message

Copyright © 2014. All Rights Reserved.