org.planx.xmlstore.io
Class Streamers

java.lang.Object
  extended by org.planx.xmlstore.io.Streamers

public final class Streamers
extends Object

Tools for Streamers and writing to DataOutput and reading from DataInput.

Author:
Thomas Ambus

Method Summary
static
<E> E
fromByteArray(byte[] b, Streamer<E> streamer)
           
static
<E> PolymorphicStreamer<E>
getPolymorphicStreamer(Class<E> cls)
          Returns a PolymorphicStreamer for the specified class.
static
<E> List<E>
readList(DataInput in, Streamer<E> s)
          Reads an immutable List of objects, where the specified Streamer is capable of reading each object in the list.
static int[] readPosArray(DataInput in)
          Reads data written by writePosArray(java.io.DataOutput, int[]).
static int readShortInt(DataInput in)
          Reads an int written by the writeShortInt method.
static String readUTF(DataInput in)
          Reads a String written by writeUTF(DataOutput,String).
static String readUTF(DataInput in, int utflen)
          Reads a String written by writeUTF(DataOutput,String,int).
static int shortIntSize(int i)
          Returns the number of bytes that will be written by the writeShortInt(java.io.DataOutput, int) method for the specified int.
static Streamer<String> stringStreamer()
           
static
<E> byte[]
toByteArray(E obj, Streamer<E> streamer)
           
static boolean unwrapNull(DataInput in)
          Reads a truth value written by wrapNull(java.io.DataOutput, java.lang.Object) and returns it.
static int utfSize(String s)
          Return the number of bytes that will be written by the writeUTF(java.io.DataOutput, java.lang.String) method for the specified String.
static boolean wrapNull(DataOutput out, Object o)
          Writes false to the output if the object was null and true if the object was non-null.
static
<E> void
writeList(DataOutput out, List<E> l, Streamer<E> s)
          Writes a list of objects where the specified Streamer is capable of writing each object contained in the List.
static void writePosArray(DataOutput out, int[] arr)
          The ints in the array must be positive, and if an int is smaller than 128, only a single byte is written to represent it.
static void writeShortInt(DataOutput out, int i)
          Writes an int.
static void writeUTF(DataOutput out, String s)
          Writes a String of any size in Java modified UTF format.
static void writeUTF(DataOutput out, String s, int utflen)
          Writes a String of any size in Java modified UTF format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getPolymorphicStreamer

public static <E> PolymorphicStreamer<E> getPolymorphicStreamer(Class<E> cls)
Returns a PolymorphicStreamer for the specified class. This is useful for maintaining the ability to stream the subtypes of various implementations of classes. Each implementation should make sure to register its sub-classes with the PolymorphicStreamer of the appropriate super class before any other part of the system needs to stream its sub-classes.


stringStreamer

public static Streamer<String> stringStreamer()

utfSize

public static int utfSize(String s)
Return the number of bytes that will be written by the writeUTF(java.io.DataOutput, java.lang.String) method for the specified String.


writeUTF

public static void writeUTF(DataOutput out,
                            String s)
                     throws IOException
Writes a String of any size in Java modified UTF format. Note, that DataOutput.writeUTF(java.lang.String) has a maximum UTF size of 65535 bytes.

Throws:
IOException

writeUTF

public static void writeUTF(DataOutput out,
                            String s,
                            int utflen)
                     throws IOException
Writes a String of any size in Java modified UTF format. Note, that DataOutput.writeUTF(java.lang.String) has a maximum UTF size of 65535 bytes.

Throws:
IOException

readUTF

public static String readUTF(DataInput in)
                      throws IOException
Reads a String written by writeUTF(DataOutput,String).

Throws:
IOException

readUTF

public static String readUTF(DataInput in,
                             int utflen)
                      throws IOException
Reads a String written by writeUTF(DataOutput,String,int).

Throws:
IOException

toByteArray

public static <E> byte[] toByteArray(E obj,
                                     Streamer<E> streamer)
                          throws IOException
Throws:
IOException

fromByteArray

public static <E> E fromByteArray(byte[] b,
                                  Streamer<E> streamer)
                       throws IOException
Throws:
IOException

shortIntSize

public static int shortIntSize(int i)
Returns the number of bytes that will be written by the writeShortInt(java.io.DataOutput, int) method for the specified int.


writeShortInt

public static void writeShortInt(DataOutput out,
                                 int i)
                          throws IOException
Writes an int. If 0 <= i < 128 then only a single byte is written. If i >= 128 4 bytes are written. If i < 0 an IllegalArgumentException is thrown.

Throws:
IOException

readShortInt

public static int readShortInt(DataInput in)
                        throws IOException
Reads an int written by the writeShortInt method.

Throws:
IOException

writePosArray

public static void writePosArray(DataOutput out,
                                 int[] arr)
                          throws IOException
The ints in the array must be positive, and if an int is smaller than 128, only a single byte is written to represent it.

Throws:
IOException

readPosArray

public static int[] readPosArray(DataInput in)
                          throws IOException
Reads data written by writePosArray(java.io.DataOutput, int[]).

Throws:
IOException

writeList

public static <E> void writeList(DataOutput out,
                                 List<E> l,
                                 Streamer<E> s)
                      throws IOException
Writes a list of objects where the specified Streamer is capable of writing each object contained in the List. A NullPointerException is thrown if the list is null.

Throws:
IOException

readList

public static <E> List<E> readList(DataInput in,
                                   Streamer<E> s)
                        throws IOException
Reads an immutable List of objects, where the specified Streamer is capable of reading each object in the list.

Throws:
IOException

wrapNull

public static boolean wrapNull(DataOutput out,
                               Object o)
                        throws IOException
Writes false to the output if the object was null and true if the object was non-null.

Returns:
The truth value written
Throws:
IOException

unwrapNull

public static boolean unwrapNull(DataInput in)
                          throws IOException
Reads a truth value written by wrapNull(java.io.DataOutput, java.lang.Object) and returns it. If false is returned the object was null and should therefore not be read from the input.

Throws:
IOException


Copyright © 2010. All Rights Reserved.