Chapter 12. Codec


The term codec originates from the words compression/decompression (amusingly, the abbreviation is in a sense a form of compression). Popular codecs perform such functions as compressing and decompressing arbitrary data (e.g., ZIP, RAR), image-specific data (e.g., GIF, JPEG), or audio data (e.g., MP3, AIFF). Over time, the term codec has grown to include a wide variety of data translations, not all of which are useful for compression and decompression but instead serve other functions such as security, data transmission, or even spell-checking. While the JDK offers a variety of built-in handlers for dealing with popular compression formats such as ZIP, GIF, and JPG, the Apache Commons Codec project fills in other gaps in functionality.

As of this writing, the Codec includes utilities in the following areas:

Data transmission (Base64 and Hex encoding/decoding)

Hash encoding (MD5 and SHA, useful for passwords and file signatures)

Phonetic encoding (useful for features such as spell-checking)

In this chapter, we will look at this functionality in a series of methods in a single class. Listing 12-1 shows the header information for the classnote the imports from the Codec package.

Listing 12-1. Imports and Class Header
 package com.cascadetg.ch12; import java.io.FileInputStream; import java.io.UnsupportedEncodingException; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.language.DoubleMetaphone; import org.apache.commons.codec.language.Metaphone; import org.apache.commons.codec.language.RefinedSoundex; import org.apache.commons.codec.language.Soundex; import org.apache.commons.codec.net.URLCodec; // To change the default encoding, use the command // java -Dfile.encoding=UTF8 EncodingDemo // when launching. Keep in mind that if you change the encoding, // you should also change the encoding of the terminal to // match (an option not available on all systems). public class EncodingDemo {     public static void printHeader(String title)     {         System.out.println();         System.out.println("================================");         System.out.println(title);         System.out.println("================================");     }     public static void main(String[] args)     {         fileEncodingDemo();         encodingDemo();         phoneticDemo();         hashEncodingDemo();         formURLEncodingDemo();     } ... // Rest of code follows. 

The imports refer to the various classes of the Codec package, as shown in Figure 12-1. The MD5 and SHA classes aren't part of the rest of the hierarchy but are instead broken out into a utility class. All of the interfaces provided are intended for use with either String or byte[] parameters, not the more sophisticated stream interfaces present in the JDK. This is acceptable for most uses, but it does require the entire data set to be loaded into memory before use.

Figure 12-1. Apache Commons Codec classes.




    Apache Jakarta Commons(c) Reusable Java Components
    Real World Web Services
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 137
    Authors: Will Iverson

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net