com.buck.common.codec
Class Codec

java.lang.Object
  extended by com.buck.common.codec.Codec
All Implemented Interfaces:
Comparable<Codec>
Direct Known Subclasses:
Base16, Base32, Base32Hex, Base64, Base64URL, PercentEncoded, QuotedPrintable, URLEncoded

public abstract class Codec
extends Object
implements Comparable<Codec>

A named mapping between encoded sequences of bytes and raw binary data. This class defines methods for creating decoders and encoders and for retrieving the various names associated with a codec. Instances of this class are immutable.

Codec names

Codecs are named by strings composed of the following characters:

Standard codecs

Codecs supported by the Codecs Library support the following standard codecs. Consult the release documentation for your implementation to see if any other codecs are supported. The behavior of such optional codecs may differ between implementations.

Codec

Description

Base16 Defined in RFC 4648, this codec, referred to as "base16" or "hex", is the standard case-insensitive hex encoding. Unlike base32 or base64, base16 requires no special padding since a full code word is always available.
Base32 Defined in RFC 4648, this codec, referred to as "base32", uses an alphabet that may be handled by humans; where the characters "0" and "O" are easily confused, as are "1", "l", and "I", the base32 alphabet omits 0 (zero) and 1 (one).
Base32 Extended Hex Alphabet Defined in RFC 4648, this codec, referred to as "base32hex", uses an alphabet that causes confusion by humans due to its use of 0 (zero) and 1 (one). However, one property with this alphabet, which the base64 and base32 alphabets lack, is that encoded data maintains its sort order when the encoded data is compared bit-wise.
Base64 Defined in RFC 4648, this codec, referred to as "base64", the encoding is designed to represent arbitrary sequences of octets in a form that allows the use of both upper- and lowercase letters but that need not be human readable.
Base64 URL Defined in RFC 4648, this codec, referred to as "base64url", is identical to base64, except that it uses an alphabet that is safe for use in URL and filenames.
Percent Encoded Defined in RFC 3986, this codec, referred to as "percent-encoded", is similar to URL Encoded, except that it uses an alphabet that is safe for use in URI, according to RFC 3986. Percent-encoding may only be applied to octets prior to producing a URI from its component parts. When encoding URI, percent encoding is preferable over URL encoded schemes.
Quoted Printable Defined in RFC 2045, this codec, referred to as "quoted-printable", is intended to represent data that largely consists of octets that correspond to printable characters in the US-ASCII character set. It encodes the data in such a way that the resulting octets are unlikely to be modified by mail transport. If the data being encoded are mostly US-ASCII text, the encoded form of the data remains largely recognizable by humans.
URL Encoded Defined in HTML 2.0 Forms, this codec, referred to as "x-www-form-urlencoded", is used primarily for HTML form submission.

Author:
Robert J. Buck

Constructor Summary
protected Codec(String canonicalName, String[] aliases)
          Initializes a new codec with the given canonical name and alias set.
 
Method Summary
 Set<String> aliases()
          Returns a set containing this codec's aliases.
static SortedMap<String,Codec> availableCodecs()
          Constructs a sorted map from canonical codec names to codec objects.
 int compareTo(Codec that)
          Compares this codec to another.
 boolean equals(Object ob)
          Tells whether or not this object is equal to another.
static Codec forName(String codecName)
          Returns a codec object for the named codec.
 int hashCode()
          Computes a hashcode for this codec.
static boolean isSupported(String codecName)
          Tells whether the named codec is supported.
 String name()
          Returns this codec's canonical name.
abstract  CodecDecoder newDecoder()
          Constructs a new decoder for this codec.
abstract  CodecEncoder newEncoder()
          Constructs a new encoder for this codec.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Codec

protected Codec(String canonicalName,
                String[] aliases)
Initializes a new codec with the given canonical name and alias set.

Parameters:
canonicalName - The canonical name of this codec
aliases - An array of this codec's aliases, or null if it has no aliases
Throws:
IllegalCodecNameException - If the canonical name or any of the aliases are illegal
Method Detail

forName

public static Codec forName(String codecName)
Returns a codec object for the named codec.

Parameters:
codecName - The name of the requested codec; may be either a canonical name or an alias
Returns:
A codec object for the named codec
Throws:
IllegalCodecNameException - If the given codec name is illegal
UnsupportedCodecException - If no support for the given codec is available in this instance of the virtual machine; the name is legal.

isSupported

public static boolean isSupported(String codecName)
Tells whether the named codec is supported.

Parameters:
codecName - The name of the requested codec; may be either a canonical name or an alias
Returns:
true if, and only if, support for the named codec is available in the current Java virtual machine
Throws:
IllegalCodecNameException - If the given codec name is illegal

aliases

public final Set<String> aliases()
Returns a set containing this codec's aliases.

Returns:
An immutable set of this codec's aliases

compareTo

public final int compareTo(Codec that)
Compares this codec to another.

Codecs are ordered by their canonical names, without regard to case.

Specified by:
compareTo in interface Comparable<Codec>
Parameters:
that - The codec to which this codec is to be compared
Returns:
A negative integer, zero, or a positive integer as this codec is less than, equal to, or greater than the specified codec

hashCode

public final int hashCode()
Computes a hashcode for this codec.

Overrides:
hashCode in class Object
Returns:
An integer hashcode

equals

public final boolean equals(Object ob)
Tells whether or not this object is equal to another.

Two codecs are equal if, and only if, they have the same canonical names. A codec is never equal to any other type of object.

Overrides:
equals in class Object
Returns:
true if, and only if, this codec is equal to the given object

name

public final String name()
Returns this codec's canonical name.

Returns:
The canonical name of this codec

availableCodecs

public static SortedMap<String,Codec> availableCodecs()
Constructs a sorted map from canonical codec names to codec objects.

The map returned by this method will have one entry for each codec for which support is available in the current Java virtual machine. If two or more supported codecs have the same canonical name then the resulting map will contain just one of them; which one it will contain is not specified.

The invocation of this method, and the subsequent use of the resulting map, may cause time-consuming disk or network I/O operations to occur. This method is provided for applications that need to enumerate all of the available codecs, for example to allow user codec selection. This method is not used by the forName method, which instead employs an efficient incremental lookup algorithm.

This method may return different results at different times if new codec providers are dynamically made available to the current Java virtual machine. In the absence of such changes, the codecs returned by this method are exactly those that can be retrieved via the forName method.

Returns:
An immutable, case-insensitive map from canonical codec names to codec objects

newDecoder

public abstract CodecDecoder newDecoder()
Constructs a new decoder for this codec.

Returns:
A new decoder for this codec

newEncoder

public abstract CodecEncoder newEncoder()
Constructs a new encoder for this codec.

Returns:
A new encoder for this codec


Copyright © 2013. All Rights Reserved.