org.fudgemsg
Class UTF8

java.lang.Object
  extended by org.fudgemsg.UTF8

public class UTF8
extends Object

Utility to manage UTF-8 encoding.

This includes support for normal UTF-8 instead of the modified UTF-8 encoding used in the earlier versions of the specification. Implemented from information at en.wikipedia.org/wiki/UTF-8, and other methods from ModifiedUTF8Util.

This class is a static utility with no shared state.


Method Summary
static String decode(byte[] arr)
          Decodes a string from a byte array.
static String decode(byte[] arr, int start, int length)
          Decodes a string from part of a byte array.
static byte[] encode(String str)
          Encodes a string into an array.
static int encode(String str, byte[] arr)
          Encodes a string into a supplied array.
static int getLengthBytes(char[] str)
          Calculate the length in bytes of a string.
static int getLengthBytes(String str)
          Calculate the length in bytes of a string.
static String readString(DataInput is, int utfLen)
          Decodes a string from a DataInput source.
static int writeString(DataOutput os, String str)
          Encodes a string to a DataOutput target.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getLengthBytes

public static int getLengthBytes(String str)
Calculate the length in bytes of a string.

Parameters:
str - the string to find the length of, not null
Returns:
number of bytes

getLengthBytes

public static int getLengthBytes(char[] str)
Calculate the length in bytes of a string.

Parameters:
str - the string to find the length of, not null
Returns:
number of bytes

encode

public static int encode(String str,
                         byte[] arr)
Encodes a string into a supplied array. The array must be at least getLengthBytes(String) long for this to succeed.

Parameters:
str - the string to encode, not null
arr - the array to encode into
Returns:
number of bytes written to array
Throws:
ArrayIndexOutOfBoundsException - if the target array is not big enough

encode

public static byte[] encode(String str)
Encodes a string into an array.

Parameters:
str - the string to encode, not null
Returns:
byte the encoding of the string

decode

public static String decode(byte[] arr)
                     throws UTFDataFormatException
Decodes a string from a byte array.

Parameters:
arr - the byte encoding of a string to convert, not null
Returns:
the decoded string, not null
Throws:
UTFDataFormatException - if the source does not contain valid UTF-8

decode

public static String decode(byte[] arr,
                            int start,
                            int length)
                     throws UTFDataFormatException
Decodes a string from part of a byte array.

Parameters:
arr - the byte encoding of a string to convert, not null
start - the start index of the UTF-8 string encoding
length - the number of bytes of UTF-8 data
Returns:
the decoded string, not null
Throws:
UTFDataFormatException - if the array fragment does not contain valid UTF-8

readString

public static String readString(DataInput is,
                                int utfLen)
                         throws IOException
Decodes a string from a DataInput source. Note that the methods within DataInput are designed for modified UTF-8 so can't be used directly with Fudge.

Parameters:
is - the data source
utfLen - the number of bytes of data to read
Returns:
the decoded string, not null
Throws:
IOException - if the underlying source raises one or the data is malformed

writeString

public static int writeString(DataOutput os,
                              String str)
                       throws IOException
Encodes a string to a DataOutput target. Note that the methods within DataOutput are designed for modified UTF-8 so can't be used directly with Fudge.

Parameters:
os - the data target
str - the string to encode
Returns:
number of bytes written
Throws:
IOException - if the target raises one


Copyright 2009-Present by OpenGamma Inc. and individual contributors
Released under the Apache License, Version 2.0