public abstract class CharToByteConverter extends Object
This class defines an interface to allow conversion of characters
to bytes for a particular encoding scheme. Encoding converters
should reside in the com.integpg.io
package.
Many encoding schemes need to take state into account in the
conversion process. That is, the conversion to a byte
might depend on
the character sequence converted before it. To accommodate this, the
CharToByteConverter
has the ability to remember state between conversions
(between calls to convert()
. Therefore, the caller
should call the flush()
method to finalize the conversion
and reset the converter's internal state.
Subclasses of this abstract
class need to implement
getMaxByteCount()
, convert()
,
flush()
, and getName()
.
Programs should not call into a converter directly. A
better method of executing character conversions is to use the
java.lang.String.getBytes(String)
method.
...
String str = new String("this is a test");
byte[] convertedString = str.getBytes("UTF8");
...
This will convert the String
"this is a test" into a byte
array according to the UTF8 encoding scheme.
ByteToCharConverter
Constructor and Description |
---|
CharToByteConverter() |
Modifier and Type | Method and Description |
---|---|
abstract int |
convert(char[] src,
int srcStart,
int srcEnd,
byte[] dst,
int dstStart,
int dstEnd)
Converts the specified
char array into a byte array based on
this CharToByteConverter 's encoding scheme. |
abstract int |
flush(byte[] buff,
int start,
int end)
Tells the
CharToByteConverter to convert
any unconverted data it has internally stored. |
static CharToByteConverter |
getConverter(String name)
Dynamically loads a
CharToByteConverter for the specified encoding scheme. |
static CharToByteConverter |
getDefaultConverter()
Returns the default
CharToByteConverter for the system. |
abstract int |
getMaxByteCount(char[] forThis,
int start,
int end)
Returns the number of bytes that the specified character
sequence will require for encoding.
|
abstract String |
getName()
Returns the name of this encoding scheme.
|
public static CharToByteConverter getConverter(String name)
CharToByteConverter
for the specified encoding scheme.
All converters should be placed in the com.integpg.io package
, and have class
name CharToByteNAME, where NAME is the encoding scheme. For example, the UTF8
CharToByteConverter
is called com.integpg.io.CharToByteUTF8
.name
- the name of the encoding scheme, such as "UTF8"null
if none could be foundpublic static CharToByteConverter getDefaultConverter()
CharToByteConverter
for the system. The name
of the default encoding scheme is stored in the system property
"file.encoding". This method finds the name of the default
encoding scheme, and calls getConverter()
with that name
as its argument.null
if the converter could
not be foundgetConverter(java.lang.String)
public abstract int getMaxByteCount(char[] forThis, int start, int end)
convert()
method. The value returned may not be the actual
number of converted bytes that will be produced due to
conversion errors, but it will be the maximum that will be
produced.forThis
- contains the character sequence that will be encoded
to determine the number of bytes requiredstart
- offset into the character array to begin processingend
- ending offset in the character array to end processing.
The number of processed characters will then be (end-start)
.convert(char[],int,int,byte[],int,int)
public abstract int convert(char[] src, int srcStart, int srcEnd, byte[] dst, int dstStart, int dstEnd) throws CharConversionException
char
array into a byte
array based on
this CharToByteConverter
's encoding scheme. getMaxByteCount()
should
always be called first to find out how much room is required in the
destination byte array.src
- the same character array passed to getMaxByteCount()
srcStart
- the same starting offset as passed to getMaxByteCount()
srcEnd
- the same ending offset as passed to getMaxByteCount()
dst
- the destination byte
arraydstStart
- offset to begin storing converted characters in the
destination arraydstEnd
- the ending location for storing converted characters into the
destination array. This argument may usually be ignored, as
the algorithm may choose to continue converting characters until
finished.CharConversionException
- If an illegal character is encountered that cannot be convertedgetMaxByteCount(char[],int,int)
,
flush(byte[],int,int)
public abstract int flush(byte[] buff, int start, int end) throws CharConversionException
CharToByteConverter
to convert
any unconverted data it has internally stored.
Some CharToByteConverter
's will store state between
calls to convert()
. Since the converter may be left in
an unknown state, the converter should be flushed to
notify it that no more input will be received. The converter
can handle any unfinished conversions before its output is
used.buff
- the destination byte arraystart
- the next available offset into the destination arrayend
- offset in the destination array to stop placing data
(may be ignored by some algorithms)CharConversionException
- if an illegal character is encountered that cannot be converted.convert(char[],int,int,byte[],int,int)
public abstract String getName()