com.wm.util.coder
Class Coder

java.lang.Object
  extended by com.wm.util.coder.Coder
Direct Known Subclasses:
Reporter, XMLCoder

public abstract class Coder
extends java.lang.Object

This class is an abstract class that contains methods for transforming an encoded set of data to a Values object. You can extend this class to define a derived class that specifies the format and the content type to decode a file, byte array or an InputStream into a Values object and to encode a Values object into a file, byte array or OutputStream.

This class is used to store and fetch Values objects to and from disk into a format specified by the derived class.

Note: If you extend this class, you must implement the following methods: decode, encode and getContentType.

See Also:
Codable, IDataCodable, StringCodable, ValuesCodable

Constructor Summary
Coder()
           
 
Method Summary
abstract  Values decode(java.io.InputStream is)
          Decodes the data in the specified InputStream to a Values object.
 Values decodeFromBytes(byte[] b)
          Decodes the data in the specified byte [] to a Values object.
abstract  void encode(java.io.OutputStream os, Values data)
          Encodes the contents of a specified Values object and writes the encoded data to an OutputStream.
 byte[] encodeToBytes(Values data)
          Encodes the specified Values object and writes the encoded data to a byte [].
abstract  java.lang.String getContentType()
          Returns a String containing the name of the content type that is in use by this instance of Coder.
 Values readFromFile(java.io.File f)
          Decodes the contents of the specified File and puts it in a Values object.
static byte[] readFully(java.io.InputStream is)
          Decodes an InputStream into a byte [] using the format defined by the derived class.
 void writeToFile(java.io.File f, Values v)
          Encodes the specified Values object and writes the encoded data to a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Coder

public Coder()
Method Detail

decode

public abstract Values decode(java.io.InputStream is)
                       throws java.io.IOException,
                              InvalidDatatypeException
Decodes the data in the specified InputStream to a Values object. This abstract method lets you to build customized processes for converting encoded data (such as an XML document) to Values objects.

Note: If you extend this class, you must implement this method.

Parameters:
is - The InputStream that this method will decode. The data in this object must be encoded in the way that your implementation of decode expects.
Returns:
The Values object that is produced by decoding the InputStream in is.
Throws:
java.io.IOException - If an error occurs while reading the InputStream or writing to the Values object.
InvalidDatatypeException - If the content of InputStream cannot an be properly parsed and decoded.
See Also:
decodeFromBytes(byte[]), readFromFile(java.io.File)

decodeFromBytes

public Values decodeFromBytes(byte[] b)
                       throws java.io.IOException
Decodes the data in the specified byte [] to a Values object.

Parameters:
b - The byte [] that contains that data that this method will decode.
Returns:
The Values object produced by decoding the data in b.
Throws:
java.io.IOException - If an error occurs while reading the byte [], parsing its contents, or writing to the Values object.
See Also:
decode(java.io.InputStream), readFromFile(java.io.File)

encode

public abstract void encode(java.io.OutputStream os,
                            Values data)
                     throws java.io.IOException,
                            InvalidDatatypeException
Encodes the contents of a specified Values object and writes the encoded data to an OutputStream. This abstract method allows you to build additional, customized implementations to accommodate other encoding schemes.

Parameters:
os - The OutputStream to which you want the encoded data written.
data - The Values object whose data you want to encode.
Throws:
java.io.IOException - If an error occurs while reading data from the Values object or writing data to the OutputStream.
InvalidDatatypeException - If the data in the Values object cannot be encoded (i.e., the Values object contains an object type that is not supported by the specific implementation of encode that you are using.)
See Also:
encodeToBytes(com.wm.util.Values), writeToFile(java.io.File, com.wm.util.Values)

encodeToBytes

public byte[] encodeToBytes(Values data)
                     throws java.io.IOException
Encodes the specified Values object and writes the encoded data to a byte [].

Parameters:
data - The Values object whose data you want to encode.
Returns:
A byte [] containing the encoded data.
Throws:
java.io.IOException - If an error occurs while reading data from the Values object or writing data to the byte [].
See Also:
encode(java.io.OutputStream, com.wm.util.Values), writeToFile(java.io.File, com.wm.util.Values)

getContentType

public abstract java.lang.String getContentType()
Returns a String containing the name of the content type that is in use by this instance of Coder. Classes that extend the Coder class support one or more content types.

Returns:
A String specifying the current content type.

readFromFile

public Values readFromFile(java.io.File f)
                    throws java.io.IOException
Decodes the contents of the specified File and puts it in a Values object. This method allows you to build a customized process for converting an encoded data file (such as an XML document) to a Values object.

Parameters:
f - A File representing the file that you want to decode. (The data in the file must be encoded in the way that the specific implementation of decode that you are using expects.)
Returns:
The Values object produced by decoding the file specified in f.
Throws:
java.io.IOException - If an error occurs while reading data from the file, parsing its contents, or writing it to the Values object.
See Also:
decode(java.io.InputStream), decodeFromBytes(byte[]), writeToFile(java.io.File, com.wm.util.Values)

readFully

public static byte[] readFully(java.io.InputStream is)
                        throws java.io.IOException
Decodes an InputStream into a byte [] using the format defined by the derived class.

Parameters:
is - The InputStream that you want to decode.
Returns:
A byte [] containing the decoded data.
Throws:
java.io.IOException - If an error occurs while reading data from the InputStream or writing data to the byte [].

writeToFile

public void writeToFile(java.io.File f,
                        Values v)
                 throws java.io.IOException
Encodes the specified Values object and writes the encoded data to a file.

Parameters:
f - A File representing the file to which you want the encoded data written. If this file does not exist, it will be created. If the file already exists, it will be overwritten.
v - The Values object whose data you want to encode.
Throws:
java.io.IOException - If an error occurs while reading data from the Values object or writing data to the file.
See Also:
encode(java.io.OutputStream, com.wm.util.Values), encodeToBytes(com.wm.util.Values), readFromFile(java.io.File)