org.planx.io
Class Streamers

java.lang.Object
  extended by org.planx.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 Streamer<byte[]> byteArrayStreamer()
           
static int compactIntSize(int i)
          Returns the number of bytes that will be written by the writeCompactInt(java.io.DataOutput, int) method for the specified int.
static int compactLongSize(long i)
          Returns the number of bytes that will be written by the writeCompactLong(java.io.DataOutput, long) method for the specified long.
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 Streamer<Long> longStreamer()
           
static
<K,V,M extends Map<K,V>>
SizeStreamer<Map<K,V>>
mapStreamer(SizeStreamer<K> keyStreamer, SizeStreamer<V> valueStreamer, Class<M> mapCls)
           
static int readCompactInt(DataInput in)
          Reads a variable sized int written by the writeCompactInt(java.io.DataOutput, int) method.
static long readCompactLong(DataInput in)
          Reads a variable sized long written by the writeCompactLong(java.io.DataOutput, long) method.
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 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 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 void writeCompactInt(DataOutput out, int i)
          Writes a variable sized int.
static void writeCompactLong(DataOutput out, long i)
          Writes a variable sized long.
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 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
equals, 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()

longStreamer

public static Streamer<Long> longStreamer()

byteArrayStreamer

public static Streamer<byte[]> byteArrayStreamer()

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

compactIntSize

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


writeCompactInt

public static void writeCompactInt(DataOutput out,
                                   int i)
                            throws IOException
Writes a variable sized 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

readCompactInt

public static int readCompactInt(DataInput in)
                          throws IOException
Reads a variable sized int written by the writeCompactInt(java.io.DataOutput, int) method.

Throws:
IOException

compactLongSize

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


writeCompactLong

public static void writeCompactLong(DataOutput out,
                                    long i)
                             throws IOException
Writes a variable sized long. If 0 <= i < 128 then only a single byte is written. If i >= 128 8 bytes are written. If i < 0 an IllegalArgumentException is thrown.

Throws:
IOException

readCompactLong

public static long readCompactLong(DataInput in)
                            throws IOException
Reads a variable sized long written by the writeCompactLong(java.io.DataOutput, long) 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

mapStreamer

public static <K,V,M extends Map<K,V>> SizeStreamer<Map<K,V>> mapStreamer(SizeStreamer<K> keyStreamer,
                                                                          SizeStreamer<V> valueStreamer,
                                                                          Class<M> mapCls)

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 © 2004-2005 Plan-X. All Rights Reserved.