Package com.sun.ts.lib.util

# Class CharacterEncoder

java.lang.Object
com.sun.ts.lib.util.CharacterEncoder

Direct Known Subclasses:
  `BASE64Encoder`

public abstract class CharacterEncoder extends Object
This class defines the encoding half of character encoders. A character encoder is an algorithim for transforming 8 bit binary data into text (generally 7 bit ASCII or 8 bit ISO-Latin-1 text) for transmition over text channels such as e-mail and network news. The character encoders have been structured around a central theme that, in general, the encoded text has the form:

            [Buffer Prefix]
            [Line Prefix][encoded data atoms][Line Suffix]
            [Buffer Suffix]

In the CharacterEncoder and CharacterDecoder classes, one complete chunk of data is referred to as a buffer. Encoded buffers are all text, and decoded buffers (sometimes just referred to as buffers) are binary octets. To create a custom encoder, you must, at a minimum, overide three abstract methods in this class.

  bytesPerAtom which tells the encoder how many bytes to send to encodeAtom
  encodeAtom which encodes the bytes sent to it as text.
  bytesPerLine which tells the encoder the maximum number of bytes per line.

Several useful encoders have already been written and are referenced in the See Also list below.

Version:
  1.38, 11/17/05
Author:
  Chuck McManis
See Also:
  - `CharacterDecoder`
  - `BASE64Encoder`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected PrintStream`
  `pStream`
  Stream that understands "printing"

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `CharacterEncoder()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected abstract int`
  `bytesPerAtom()`
  Return the number of bytes per atom of encoding
  `protected abstract int`
  `bytesPerLine()`
  Return the number of bytes that can be encoded per line
  `String`
  `encode(byte[] aBuffer)`
  A 'streamless' version of encode that simply takes a buffer of bytes and returns a string containing the encoded buffer.
  `void`
  `encode(byte[] aBuffer, OutputStream aStream)`
  Encode the buffer in aBuffer and write the encoded result to the OutputStream aStream.
  `void`
  `encode(InputStream inStream, OutputStream outStream)`
  Encode bytes from the input stream, and write them as text characters to the output stream.
  `String`
  `encode(ByteBuffer aBuffer)`
  A 'streamless' version of encode that simply takes a ByteBuffer and returns a string containing the encoded buffer.
  `void`
  `encode(ByteBuffer aBuffer, OutputStream aStream)`
  Encode the aBuffer ByteBuffer and write the encoded result to the OutputStream aStream.
  `protected abstract void`
  `encodeAtom(OutputStream aStream, byte[] someBytes, int anOffset, int aLength)`
  Encode one "atom" of information into characters.
  `String`
  `encodeBuffer(byte[] aBuffer)`
  A 'streamless' version of encode that simply takes a buffer of bytes and returns a string containing the encoded buffer.
  `void`
  `encodeBuffer(byte[] aBuffer, OutputStream aStream)`
  Encode the buffer in aBuffer and write the encoded result to the OutputStream aStream.
  `void`
  `encodeBuffer(InputStream inStream, OutputStream outStream)`
  Encode bytes from the input stream, and write them as text characters to the output stream.
  `String`
  `encodeBuffer(ByteBuffer aBuffer)`
  A 'streamless' version of encode that simply takes a ByteBuffer and returns a string containing the encoded buffer.
  `void`
  `encodeBuffer(ByteBuffer aBuffer, OutputStream aStream)`
  Encode the aBuffer ByteBuffer and write the encoded result to the OutputStream aStream.
  `protected void`
  `encodeBufferPrefix(OutputStream aStream)`
  Encode the prefix for the entire buffer.
  `protected void`
  `encodeBufferSuffix(OutputStream aStream)`
  Encode the suffix for the entire buffer.
  `protected void`
  `encodeLinePrefix(OutputStream aStream, int aLength)`
  Encode the prefix that starts every output line.
  `protected void`
  `encodeLineSuffix(OutputStream aStream)`
  Encode the suffix that ends every output line.
  `protected int`
  `readFully(InputStream in, byte[] buffer)`
  This method works around the bizarre semantics of BufferedInputStream's read method.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### pStream

    protected PrintStream pStream
    Stream that understands "printing"

- ## Constructor Details

  - ### CharacterEncoder

    public CharacterEncoder()

- ## Method Details

  - ### bytesPerAtom

    protected abstract int bytesPerAtom()
    Return the number of bytes per atom of encoding

  - ### bytesPerLine

    protected abstract int bytesPerLine()
    Return the number of bytes that can be encoded per line

  - ### encodeBufferPrefix

    protected void encodeBufferPrefix(OutputStream aStream) throws IOException
    Encode the prefix for the entire buffer. By default is simply opens the PrintStream for use by the other functions.
    Throws:
      `IOException`

  - ### encodeBufferSuffix

    protected void encodeBufferSuffix(OutputStream aStream) throws IOException
    Encode the suffix for the entire buffer.
    Throws:
      `IOException`

  - ### encodeLinePrefix

    protected void encodeLinePrefix(OutputStream aStream, int aLength) throws IOException
    Encode the prefix that starts every output line.
    Throws:
      `IOException`

  - ### encodeLineSuffix

    protected void encodeLineSuffix(OutputStream aStream) throws IOException
    Encode the suffix that ends every output line. By default this method just prints a into the output stream.
    Throws:
      `IOException`

  - ### encodeAtom

    protected abstract void encodeAtom(OutputStream aStream, byte[] someBytes, int anOffset, int aLength) throws IOException
    Encode one "atom" of information into characters.
    Throws:
      `IOException`

  - ### readFully

    protected int readFully(InputStream in, byte[] buffer) throws IOException
    This method works around the bizarre semantics of BufferedInputStream's read method.
    Throws:
      `IOException`

  - ### encode

    public void encode(InputStream inStream, OutputStream outStream) throws IOException
    Encode bytes from the input stream, and write them as text characters to the output stream. This method will run until it exhausts the input stream, but does not print the line suffix for a final line that is shorter than bytesPerLine().
    Throws:
      `IOException`

  - ### encode

    public void encode(byte[] aBuffer, OutputStream aStream) throws IOException
    Encode the buffer in aBuffer and write the encoded result to the OutputStream aStream.
    Throws:
      `IOException`

  - ### encode

    public String encode(byte[] aBuffer)
    A 'streamless' version of encode that simply takes a buffer of bytes and returns a string containing the encoded buffer.

  - ### encode

    public void encode(ByteBuffer aBuffer, OutputStream aStream) throws IOException
    Encode the aBuffer ByteBuffer and write the encoded result to the OutputStream aStream. The ByteBuffer's position will be advanced to ByteBuffer's limit.
    Throws:
      `IOException`

  - ### encode

    public String encode(ByteBuffer aBuffer)
    A 'streamless' version of encode that simply takes a ByteBuffer and returns a string containing the encoded buffer. The ByteBuffer's position will be advanced to ByteBuffer's limit.

  - ### encodeBuffer

    public void encodeBuffer(InputStream inStream, OutputStream outStream) throws IOException
    Encode bytes from the input stream, and write them as text characters to the output stream. This method will run until it exhausts the input stream. It differs from encode in that it will add the line at the end of a final line that is shorter than bytesPerLine().
    Throws:
      `IOException`

  - ### encodeBuffer

    public void encodeBuffer(byte[] aBuffer, OutputStream aStream) throws IOException
    Encode the buffer in aBuffer and write the encoded result to the OutputStream aStream.
    Throws:
      `IOException`

  - ### encodeBuffer

    public String encodeBuffer(byte[] aBuffer)
    A 'streamless' version of encode that simply takes a buffer of bytes and returns a string containing the encoded buffer.

  - ### encodeBuffer

    public void encodeBuffer(ByteBuffer aBuffer, OutputStream aStream) throws IOException
    Encode the aBuffer ByteBuffer and write the encoded result to the OutputStream aStream. The ByteBuffer's position will be advanced to ByteBuffer's limit.
    Throws:
      `IOException`

  - ### encodeBuffer

    public String encodeBuffer(ByteBuffer aBuffer)
    A 'streamless' version of encode that simply takes a ByteBuffer and returns a string containing the encoded buffer. The ByteBuffer's position will be advanced to ByteBuffer's limit.