org.clazzes.optional.io
Class DataCodingHelper

java.lang.Object
  extended by org.clazzes.optional.io.DataCodingHelper

public abstract class DataCodingHelper
extends java.lang.Object

Static helper methods, which complement the functionality of DataInput and DataOutput.


Constructor Summary
DataCodingHelper()
           
 
Method Summary
static int[] readDynamicIntArray(java.io.DataInput in, int maxSize)
          Read a dynamic integer array from the stream.
static byte[] readDynamicOpaque(java.io.DataInput in, int maxSize)
          Read a dynamic opaque byte array from the stream.
static int readUInt3(java.io.DataInput in)
          Read a three-byte unsigned integer from a stream.
static long readUInt4(java.io.DataInput in)
          Read a four-byte unsigned integer from a stream.
static void writeDynamicIntArray(java.io.DataOutput out, int maxSize, int[] arr)
          Write a dynamic integer array to the stream.
static void writeDynamicOpaque(java.io.DataOutput out, int maxSize, byte[] opaque)
          Write a dynamic opaque byte array to the stream.
static void writeUInt3(java.io.DataOutput out, int x)
          Write a three-byte unsigned integer value to the stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DataCodingHelper

public DataCodingHelper()
Method Detail

writeUInt3

public static void writeUInt3(java.io.DataOutput out,
                              int x)
                       throws java.io.IOException
Write a three-byte unsigned integer value to the stream.

Parameters:
out - The stream to write to.
x - The value, of which the 24 low-order bits are written.
Throws:
java.io.IOException - Upon I/O errors.

readUInt3

public static int readUInt3(java.io.DataInput in)
                     throws java.io.IOException
Read a three-byte unsigned integer from a stream.

Parameters:
in - The stream to be read.
Returns:
A value between 0 and 16777215.
Throws:
java.io.IOException - Upon errors or premature end of stream.

readUInt4

public static long readUInt4(java.io.DataInput in)
                      throws java.io.IOException
Read a four-byte unsigned integer from a stream.

Parameters:
in - The stream to be read.
Returns:
An unsigned integer value represented as a long value.
Throws:
java.io.IOException - Upon errors or premature end of stream.

readDynamicOpaque

public static byte[] readDynamicOpaque(java.io.DataInput in,
                                       int maxSize)
                                throws java.io.IOException
Read a dynamic opaque byte array from the stream. The number of bytes read as length prefix is determined from the given maximal size. Maximal sizes from 0 to 255 read one byte of length information, maximal sizes from 256 to 65535 read two bytes of length information, maximal sizes from 65536 to 16777215 read three bytes of length information, maximal sizes greater or equal to 16777216 or negative maximal sizes read four bytes of length information.

Parameters:
in - The stream to read from.
maxSize - The maximal permissible size or a negative number for no size restrictions.
Returns:
A newly allocated byte array with a size as specified in the length prefix inside the stream. If the length in the stream is 0, null is returned.
Throws:
java.io.IOException - Upon read errors.

writeDynamicOpaque

public static void writeDynamicOpaque(java.io.DataOutput out,
                                      int maxSize,
                                      byte[] opaque)
                               throws java.io.IOException
Write a dynamic opaque byte array to the stream. The number of bytes written as length prefix is determined from the given maximal size. Maximal sizes from 0 to 255 write one byte of length information, maximal sizes from 256 to 65535 write two bytes of length information, maximal sizes from 65536 to 16777215 write three bytes of length information, maximal sizes greater or equal to 16777216 or negative maximal sizes write four bytes of length information.

Parameters:
out - The stream to write to.
maxSize - The maximal permissible size or a negative number for no size restrictions.
opaque - The byte array to write. If null is passed in, an array of length 0 written.
Throws:
java.io.IOException - Upon write errors.

readDynamicIntArray

public static int[] readDynamicIntArray(java.io.DataInput in,
                                        int maxSize)
                                 throws java.io.IOException
Read a dynamic integer array from the stream. The number of bytes read as length prefix is determined from the given maximal size. Maximal sizes from 0 to 255 read one byte of length information, maximal sizes from 256 to 65535 read two bytes of length information, maximal sizes from 65536 to 16777215 read three bytes of length information, maximal sizes greater or equal to 16777216 or negative maximal sizes read four bytes of length information.

Parameters:
in - The stream to read from.
maxSize - The maximal permissible size or a negative number for no size restrictions.
Returns:
A newly allocated int[] with a size as specified in the length prefix inside the stream. If the length in the stream is 0, null is returned.
Throws:
java.io.IOException - Upon read errors.

writeDynamicIntArray

public static void writeDynamicIntArray(java.io.DataOutput out,
                                        int maxSize,
                                        int[] arr)
                                 throws java.io.IOException
Write a dynamic integer array to the stream. The number of bytes written as length prefix is determined from the given maximal size. Maximal sizes from 0 to 255 write one byte of length information, maximal sizes from 256 to 65535 write two bytes of length information, maximal sizes from 65536 to 16777215 write three bytes of length information, maximal sizes greater or equal to 16777216 or negative maximal sizes write four bytes of length information.

Parameters:
out - The stream to write to.
maxSize - The maximal permissible size or a negative number for no size restrictions.
arr - The int[] to write. If null is passed in, an array of length 0 written.
Throws:
java.io.IOException - Upon write errors.


Copyright © 2010. All Rights Reserved.