Java/Apache Common/Code

Материал из Java эксперт
Версия от 07:23, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Codec Digest

import org.apache.rumons.codec.digest.*;
public class DigestUsage{
  public static void main(String args[]){
    DigestUsage codec = new DigestUsage();
    try{
      codec.start();
    }catch(Exception e){
      System.err.println(e);
    }
  }
  public void start(){
    String hashData = "Hello World!!";
    System.err.println("Hello World!! as MD5 16 element hash: "
      + new String(DigestUtils.md5(hashData)));
    System.err.println("Hello World!! as MD5 Hex hash: "
      + DigestUtils.md5Hex(hashData));
    System.err.println("Hello World!! as SHA byte array hash: "
      + new String(DigestUtils.sha(hashData)));
    System.err.println("Hello World!! as SHA Hex hash: "
      + DigestUtils.shaHex(hashData));
  }
}





Codec Language

import org.apache.rumons.codec.*;
import org.apache.rumons.codec.language.*;
public class LanguageUsage{
  public static void main(String args[]){
    LanguageUsage codec = new LanguageUsage();
    try{
      codec.start();
    }catch(Exception e){
      System.err.println(e);
    }
  }
  public void start() throws EncoderException, DecoderException{
    String word1 = "Wilson";
    String word2 = "Wylson";
    String foreignWord1 = "Otto";
    String foreignWord2 = "Auto";
    Soundex sndx = new Soundex();
    DoubleMetaphone doubleMetaphone = new DoubleMetaphone();
    System.err.println("Soundex Code for Wilson is: " + sndx.encode("Wilson"));
    System.err.println("Soundex Code for Wylson is: " + sndx.encode("Wylson"));
    // Use the StringEncoderComparator to compare these two Strings
    StringEncoderComparator comparator1 = new StringEncoderComparator(sndx);
    System.err.println("Are Wilson and Wylson same based on Soundex? "
      + comparator1.rupare("Wilson", "Wylson"));
    System.err.println("Are Auto and Otto same based on Soundex? "
      + comparator1.rupare("Auto", "Otto"));
    StringEncoderComparator comparator2 =
      new StringEncoderComparator(doubleMetaphone);
    System.err.println("Are Auto and Otto same based on DoubleMetaphone? "
      + comparator2.rupare("Auto", "Otto"));
    System.err.println("Double Metaphone primary code for Schmidt: " +
      doubleMetaphone.doubleMetaphone("Schmidt"));
    System.err.println("Double Metaphone secondary code for Schmidt: " +
      doubleMetaphone.doubleMetaphone("Schmidt", true));
  }
}





Codec Net: URL encode and decode

import org.apache.rumons.codec.*;
import org.apache.rumons.codec.net.*;
public class NetUsage{
  public static void main(String args[]){
    NetUsage codec = new NetUsage();
    try{
      codec.start();
    }catch(Exception e){
      System.err.println(e);
    }
  }
  public void start() throws EncoderException, DecoderException{
    String urlData1 = "This#is^a&String with reserved @/characters";
    URLCodec encoder = new URLCodec();
    String result = encoder.encode(urlData1);
    System.err.println("URL Encoding result: " + result);
    System.err.println("URL Decoding result: " + encoder.decode(result));
  }
}