com.sun.sgs.impl.service.data.store
Class DataEncoding

java.lang.Object
  extended by com.sun.sgs.impl.service.data.store.DataEncoding

public final class DataEncoding
extends Object

Provides methods for encoding and decoding data stored in the database.


Method Summary
static int decodeInt(byte[] bytes)
          Decodes a four byte array into an int.
static int decodeInt(byte[] bytes, int offset)
          Decodes a four byte subsequence of an array into an int.
static long decodeLong(byte[] bytes)
          Decodes an eight byte array into a long.
static short decodeShort(byte[] bytes)
          Decodes a two byte array into a short.
static String decodeString(byte[] bytes)
          Decodes an array of bytes encoded as modified UTF-8, but with the two-byte size value omitted from the start and with a null termination, into a String.
static byte[] encodeInt(int n)
          Encodes an int into a four byte array.
static void encodeInt(int n, byte[] bytes, int offset)
          Encodes an int into a four byte subsequence of an array.
static byte[] encodeLong(long n)
          Encodes a long into an eight byte array.
static byte[] encodeShort(short n)
          Encodes a short into a two byte array.
static byte[] encodeString(String string)
          Encodes a String into an array of bytes encoded in modified UTF-8 format, as documented by DataInput, but with the two byte size value omitted from the start of the encoding, and with a terminating null byte.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

encodeShort

public static byte[] encodeShort(short n)
Encodes a short into a two byte array. The higher order bits of the value are stored in the first byte. The high order bit of the first byte is inverted to insure that negative values sort first.

Parameters:
n - the number to encode
Returns:
the encoded byte array

decodeShort

public static short decodeShort(byte[] bytes)
Decodes a two byte array into a short. The higher order bits of the value are read from the first byte. The high order bit of the first byte is inverted.

Parameters:
bytes - the byte array to decode
Returns:
the decoded number
Throws:
IndexOutOfBoundsException - if bytes is less than two bytes in length
IllegalArgumentException - if bytes is more than two bytes in length

encodeInt

public static byte[] encodeInt(int n)
Encodes an int into a four byte array. The higher order bits of the value are stored in the lower numbered bytes. The high order bit of the first byte is inverted to insure that negative values sort first.

Parameters:
n - the number to encode
Returns:
the encoded byte array

encodeInt

public static void encodeInt(int n,
                             byte[] bytes,
                             int offset)
Encodes an int into a four byte subsequence of an array. The higher order bits of the value are stored in the lower numbered bytes. The high order bit of the first byte is inverted to insure that negative values sort first.

Parameters:
n - the number to encode
bytes - the array in which to store the encoded bytes
offset - the array index at which to store the first byte
Throws:
IndexOutOfBoundsException - if bytes is not long enough to contain the encoded bytes

decodeInt

public static int decodeInt(byte[] bytes)
Decodes a four byte array into an int. The higher order bits of the value are read from the lower numbered bytes. The high order bit of the first byte is inverted.

Parameters:
bytes - the byte array to decode
Returns:
the decoded number
Throws:
IndexOutOfBoundsException - if bytes is less than four bytes in length
IllegalArgumentException - if bytes is more than four bytes in length

decodeInt

public static int decodeInt(byte[] bytes,
                            int offset)
Decodes a four byte subsequence of an array into an int. The higher order bits of the value are read from the lower numbered bytes. The high order bit of the first byte is inverted.

Parameters:
bytes - the byte array to decode
offset - the array index of the first byte to decode
Returns:
the decoded number
Throws:
IndexOutOfBoundsException - if bytes is not long enough to contain the encoded bytes

encodeLong

public static byte[] encodeLong(long n)
Encodes a long into an eight byte array. The higher order bits of the value are stored in the lower numbered bytes. The high order bit of the first byte is inverted to insure that negative values sort first.

Parameters:
n - the number to encode
Returns:
the encoded byte array

decodeLong

public static long decodeLong(byte[] bytes)
Decodes an eight byte array into a long. The higher order bits of the value are read from the lower numbered bytes. The high order bit of the first byte is inverted.

Parameters:
bytes - the byte array to decode
Returns:
the decoded number
Throws:
IndexOutOfBoundsException - if bytes is less than eight bytes in length
IllegalArgumentException - if bytes is more than eight bytes in length

encodeString

public static byte[] encodeString(String string)
Encodes a String into an array of bytes encoded in modified UTF-8 format, as documented by DataInput, but with the two byte size value omitted from the start of the encoding, and with a terminating null byte. Removing the size from the start of the encoding means that the strings will sort properly when used as keys, and adding a null termination removes the need to depend on the length of the array to find the end of the data, which turns out to be useful for Berkeley DB.

Parameters:
string - the string to encode
Returns:
the encoded byte array
Throws:
IllegalArgumentException - if the UTF-8 encoding of the string contains more than 65535 characters

decodeString

public static String decodeString(byte[] bytes)
Decodes an array of bytes encoded as modified UTF-8, but with the two-byte size value omitted from the start and with a null termination, into a String.

Parameters:
bytes - the byte array to decode
Returns:
the decoded string
Throws:
IllegalArgumentException - if the format of the bytes is not valid UTF-8 data, if it is not null terminated, or if it represents more than 65535 bytes

RedDwarf, Version 0.10.1
2010-03-14 10:56:12

Copyright © 2010 The RedDwarf Authors. All rights reserved
Copyright © 2007-2010 Sun Microsystems, Inc. All rights reserved