Class DataPacket

java.lang.Object
com.iizix.nio.DataPacket

public class DataPacket extends Object
This class implements the cryptographic interface for the client/server communication. The encryption algorithm is NOT intended for advanced encryption, rather to hide otherwise "visible" data such as user ID's and passwords.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The largest message, in bytes, that the WebSocket transports accept and that decompress(byte[]) will inflate a packet to: 10,000,000.
  • Constructor Summary

    Constructors
    Constructor
    Description
    The default cryptographic interface only implements the most simple way of garbling the byte data.
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    compress(byte[] data, int length)
    Tries to compress the input data to a smaller chunk.
    static byte[]
    compress2(byte[] data, int length)
    Always compresses the input data to perhaps a smaller chunk.
    static byte[]
    decompress(byte[] data)
    Decompress the data.
    static byte[]
    decompressLenient(byte[] data)
    Decompress data that compress2(byte[],int) may have written before its stream was always complete: as decompress(byte[]), except that a stream cut off after its last byte of data - its end-of-stream marker and Adler-32 trailer missing - is accepted, as it was before the decoder refused it.
    byte[]
    decrypt(byte[] indata, int offset, int len)
    Decrypts the data.
    byte[]
    encrypt(byte[] data, int length)
    Encrypts the data.
  • Field Details

    • MAX_MESSAGE_SIZE

      public static final int MAX_MESSAGE_SIZE
      The largest message, in bytes, that the WebSocket transports accept and that decompress(byte[]) will inflate a packet to: 10,000,000.

      The product has this one number for "how large may a message be": WSHandler (Jetty, the server side) caps a binary message at it and WebSocketCommNIO (the NIO side) caps a frame at it, both by reference to this constant. A compressed packet declares its inflated size in the 31 bits of its four-byte header, up to 2 GiB, and those bytes come from the peer: a message inflates to a message, so the declared size is held to the same limit as the message it arrived in, and refused before anything is allocated for it.

      See Also:
  • Constructor Details

    • DataPacket

      public DataPacket()
      The default cryptographic interface only implements the most simple way of garbling the byte data.

      In this implementation, nothing needs to be done in the constructor.

  • Method Details

    • encrypt

      public byte[] encrypt(byte[] data, int length)
      Encrypts the data. The decrypt method has to make sure that the same data that is input to the encrypt method is provided as a return to this method.

      Note: if any changes to the buffer is required and the buffer is of the same length, use the original buffer in order to spare the virtual machine of any memory freeing. In this case, the same buffer is returned as only the content have changed, not the size.

    • decrypt

      public byte[] decrypt(byte[] indata, int offset, int len)
      Decrypts the data.
      Parameters:
      indata - Input data.
      offset - Offset in input data.
      len - Length of input data.
    • compress

      public static byte[] compress(byte[] data, int length)
      Tries to compress the input data to a smaller chunk. If this chunk is not at least 2% smaller, the best thing is to use the data uncompressed and in this case the function will return null. If the input data size is less than 300 bytes, null is returned, thus no compression performed.
      Parameters:
      data - the input data to compress.
      Returns:
      The compressed data, or null if there is no need for compression.
    • compress2

      public static byte[] compress2(byte[] data, int length)
      Always compresses the input data to perhaps a smaller chunk.

      The stream is always complete, ending in its end-of-stream marker and Adler-32 trailer. Incompressible input deflates to more than its own size - deflate stores it in blocks of about 16 KB with a 5-byte header each - so the output buffer grows until the deflater has finished. A single deflate call into length+32 bytes used to cut the stream of incompressible input from about 82,000 bytes on, its trailer first and its data from about 98,000 bytes, and licence files were written so: decompressLenient(byte[]) reads those.

      Parameters:
      data - the input data to compress.
      Returns:
      The compressed data, or data if there is no need for compression.
    • decompress

      public static byte[] decompress(byte[] data) throws IOException
      Decompress the data.

      A compressed packet declares its inflated size in its four-byte header. A declared size above MAX_MESSAGE_SIZE is refused before anything is allocated for it, and a stream that does not inflate to exactly the declared size - short of it, beyond it, or cut off before its end - is refused after inflating at most the declared size.

      Returns:
      The uncompressed data.
      Throws:
      IOException - for uncompress errors, a declared size above MAX_MESSAGE_SIZE, or a stream that does not inflate to its declared size and end there.
    • decompressLenient

      public static byte[] decompressLenient(byte[] data) throws IOException
      Decompress data that compress2(byte[],int) may have written before its stream was always complete: as decompress(byte[]), except that a stream cut off after its last byte of data - its end-of-stream marker and Adler-32 trailer missing - is accepted, as it was before the decoder refused it. compress2 cut the stream of incompressible input from about 82,000 bytes on and wrote licence files with it: this reads installed licence files, whose own SHA-256 guards their bytes. Everything else reads with decompress.
      Parameters:
      data - the data, compressed or not.
      Returns:
      The uncompressed data.
      Throws:
      IOException - for uncompress errors, a declared size above MAX_MESSAGE_SIZE, or a stream that does not inflate to its declared size.