uk.org.blankaspect.nlf
Class Chunk.Base64Encoder

java.lang.Object
  extended by uk.org.blankaspect.nlf.Chunk.Base64Encoder
All Implemented Interfaces:
Chunk.Encoder
Enclosing class:
Chunk

public static class Chunk.Base64Encoder
extends java.lang.Object
implements Chunk.Encoder

This singleton class implements a Base64 encoder for chunk data. It is set as the default encoder on all general chunks (ie, chunks that do not have a reserved identifier). The length of a line of output from the encoder and the string that terminates each line can both be set on the single instance of the class. The default line length of zero means that no line separators will be appended to the encoded output.

A chunk's encoder is used by the toXml(org.w3c.dom.Document) method to encode the chunk data as the character data of an XML element.

Since:
1.0
See Also:
Chunk.Encoder

Method Summary
 java.lang.String encode(byte[] data, int offset, int length, boolean endOfInput)
          Encodes the specified byte data as a Base64 string.
 int getInputLength(long size)
          Returns the length of a block of input data that the encode(byte[], int, int, boolean) method expects to receive.
static Chunk.Base64Encoder getInstance()
          Returns the single instance of Base64Encoder.
 int getLineLength()
          Returns the length of a line of encoded output.
 java.lang.String getLineSeparator()
          Returns the string that is appended to each line of encoded output.
 void reset()
          Resets the chunk encoder before any encoding is performed: does nothing.
 void setLineLength(int lineLength)
          Sets the length of a Base64-encoded line.
 void setLineSeparator(java.lang.String lineSeparator)
          Sets the string that will be appended to each line of encoded output.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static Chunk.Base64Encoder getInstance()
Returns the single instance of Base64Encoder.

Returns:
the single instance of Base64Encoder.
Since:
1.0

reset

public void reset()
Resets the chunk encoder before any encoding is performed: does nothing.

Specified by:
reset in interface Chunk.Encoder
Since:
1.0

getInputLength

public int getInputLength(long size)
Returns the length of a block of input data that the encode(byte[], int, int, boolean) method expects to receive.

Specified by:
getInputLength in interface Chunk.Encoder
Parameters:
size - the size of the chunk data.
Returns:
the length of a block of input data that the encode(byte[], int, int, boolean) method expects to receive.
Since:
1.0

encode

public java.lang.String encode(byte[] data,
                               int offset,
                               int length,
                               boolean endOfInput)
                        throws java.lang.IllegalArgumentException
Encodes the specified byte data as a Base64 string. The line length and line separator that are used in the encoding can be set with setLineLength(int) and setLineSeparator(java.lang.String) respectively.

Specified by:
encode in interface Chunk.Encoder
Parameters:
data - the data that are to be encoded.
offset - the offset to data at which the input data begin.
length - the length of the input data.
endOfInput - true if the input data are the last that are to be encoded; false otherwise.
Returns:
the string that results from encoding the input data.
Throws:
java.lang.IllegalArgumentException - if
  • data is null, or
  • (offset < 0) or (offset > data.length), or
  • (length < 0) or (length > data.length - offset).
Since:
1.0

getLineLength

public int getLineLength()
Returns the length of a line of encoded output.

Returns:
the length of a line of encoded output.
Since:
1.0
See Also:
setLineLength(int)

getLineSeparator

public java.lang.String getLineSeparator()
Returns the string that is appended to each line of encoded output.

Returns:
the string that is appended to each line of encoded output.
Since:
1.0
See Also:
setLineSeparator(String)

setLineLength

public void setLineLength(int lineLength)
                   throws java.lang.IllegalArgumentException
Sets the length of a Base64-encoded line. If the length is set to zero, no line separators will be appended to the encoded output. The length must be a multiple of 4.

Parameters:
lineLength - the length of a Base64-encoded line, or 0 if no line separators are to be appended to the encoded output. The length must be a multiple of 4.
Throws:
java.lang.IllegalArgumentException - if lineLength is negative or is not a multiple of 4.
Since:
1.0
See Also:
getLineLength()

setLineSeparator

public void setLineSeparator(java.lang.String lineSeparator)
Sets the string that will be appended to each line of encoded output.

Parameters:
lineSeparator - the string that is to be appended to each line of encoded output. If lineSeparator is null, the line separator is set to the value of the system property line.separator.
Since:
1.0
See Also:
getLineSeparator()