Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Apache Commons Codec features

Header Image

Ahoy there, ye landlubbers! Today, we’re going to set sail on an adventure and explore the different encoding and decoding techniques supported by Apache Commons Codec library. This be an essential tool for any buccaneer who wants to send messages without the fear of any landlubber decoding them!

Different encoding and decoding techniques supported

When it comes to encoding and decoding, Apache Commons Codec provides a vast array of options for us. Let’s take a look at some of them.

Base64 encoding and decoding

If ye ever find yourself stranded on a desert island and ye need to send a message to yer mateys, ye can use Base64 encoding. It converts binary data into a set of printable characters, making it easier to transmit the data over a network or through email. Apache Commons Codec provides an implementation of Base64 encoding and decoding that supports different variants of the encoding algorithm, including MIME and URL safe.

Arrr, let’s take a look at how it works in Java:

import org.apache.commons.codec.binary.Base64;

String message = "Ahoy matey!";
byte[] encodedBytes = Base64.encodeBase64(message.getBytes());
String encodedMessage = new String(encodedBytes);
System.out.println("Encoded message: " + encodedMessage);

byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
String decodedMessage = new String(decodedBytes);
System.out.println("Decoded message: " + decodedMessage);

Hexadecimal encoding and decoding

Sometimes, Base64 encoding is not enough to protect yer messages from those pesky eavesdroppers. That’s when ye need to use hexadecimal encoding. It converts binary data into a string of hexadecimal digits, which can only be decoded by someone who knows the algorithm.

Apache Commons Codec provides a class called Hex that supports hexadecimal encoding and decoding. Here’s an example of how to use it:

import org.apache.commons.codec.binary.Hex;

String message = "Shiver me timbers!";
byte[] encodedBytes = Hex.encodeHex(message.getBytes());
String encodedMessage = new String(encodedBytes);
System.out.println("Encoded message: " + encodedMessage);

char[] decodedChars = Hex.decodeHex(encodedMessage.toCharArray());
String decodedMessage = new String(decodedChars);
System.out.println("Decoded message: " + decodedMessage);

URL encoding and decoding

Sometimes, yer messages may contain characters that cannot be transmitted over the internet, such as spaces or special characters. That’s when ye need to use URL encoding. It replaces special characters with a percent sign (%) followed by two hexadecimal digits that represent the ASCII value of the character.

Apache Commons Codec provides a class called URLCodec that supports URL encoding and decoding. Here’s an example of how to use it:

import org.apache.commons.codec.net.URLCodec;

String message = "Avast ye!";
URLCodec urlCodec = new URLCodec();
String encodedMessage = urlCodec.encode(message);
System.out.println("Encoded message: " + encodedMessage);

String decodedMessage = urlCodec.decode(encodedMessage);
System.out.println("Decoded message: " + decodedMessage);

Quoted Printable encoding and decoding

Quoted Printable encoding is another method of encoding binary data into printable characters. It’s commonly used in email messages to ensure that the message is compatible with different email systems.

Apache Commons Codec provides a class called QuotedPrintableCodec that supports quoted printable encoding and decoding. Here’s an example of how to use it:

import org.apache.commons.codec.net.QuotedPrintableCodec;

String message = "All hands on deck!";
QuotedPrintableCodec quotedPrintableCodec = new QuotedPrintableCodec();
String encodedMessage = quotedPrintableCodec.encode(message); System.out.println("Encoded message: " + encodedMessage);

String decodedMessage = quotedPrintableCodec.decode(encodedMessage);
System.out.println("Decoded message: " + decodedMessage);

Soundex encoding

If ye be dealing with names or words and ye want to search for similar sounding words, ye can use Soundex encoding. It converts words into a four-character code that represents the way the word sounds.

Apache Commons Codec provides a class called Soundex that supports Soundex encoding. Here’s an example of how to use it:

import org.apache.commons.codec.language.Soundex;

String word = "Treasure";
Soundex soundex = new Soundex();
String encodedWord = soundex.encode(word);
System.out.println("Encoded word: " + encodedWord);

String[] words = {"Tresure", "Trasure", "Treasur", "Tresaur"};
for (String w : words) {
    if (soundex.encode(w).equals(encodedWord)) {
        System.out.println(w + " sounds like " + word);
    }
}

Metaphone and DoubleMetaphone encoding

Metaphone and DoubleMetaphone are algorithms that encode words based on their pronunciation. They’re useful for searching for similar-sounding words, especially when the spelling of the words may vary.

Apache Commons Codec provides classes called Metaphone and DoubleMetaphone that support Metaphone and DoubleMetaphone encoding. Here’s an example of how to use them:

import org.apache.commons.codec.language.DoubleMetaphone;
import org.apache.commons.codec.language.Metaphone;

String word = "Sea monster";
Metaphone metaphone = new Metaphone();
DoubleMetaphone doubleMetaphone = new DoubleMetaphone();
String encodedWord1 = metaphone.encode(word);
String encodedWord2 = doubleMetaphone.encode(word);
System.out.println("Metaphone encoded word: " + encodedWord1);
System.out.println("DoubleMetaphone encoded word: " + encodedWord2);

Compression and decompression of data

Ahoy there, me hearties! When it comes to transmitting data over the high seas, sometimes ye need to make it smaller to save space and speed up the transmission. That’s where compression comes in!

Zlib compression and decompression

Zlib is a popular compression algorithm used in many applications, including HTTP and SSL/TLS. Apache Commons Codec provides a class called ZlibCodec that supports zlib compression and decompression. Here’s an example of how to use it:

import org.apache.commons.codec.digest.ZlibCodec;

String message = "Hoist the Jolly Roger!";
ZlibCodec zlibCodec = new ZlibCodec();
byte[] compressedBytes = zlibCodec.compress(message.getBytes());
System.out.println("Compressed message: " + new String(compressedBytes));

byte[] decompressedBytes = zlibCodec.decompress(compressedBytes);
System.out.println("Decompressed message: " + new String(decompressedBytes));

Gzip compression and decompression

Gzip is another popular compression algorithm used in many applications, including HTTP and email. Apache Commons Codec provides a class called GzipCodec that supports gzip compression and decompression. Here’s an example of how to use it:

import org.apache.commons.codec.digest.GzipCodec;

String message = "Ahoy there, me hearties!";
GzipCodec gzipCodec = new GzipCodec();
byte[] compressedBytes = gzipCodec.compress(message.getBytes());
System.out.println("Compressed message: " + new String(compressedBytes));

byte[] decompressedBytes = gzipCodec.decompress(compressedBytes);
System.out.println("Decompressed message: " + new String(decompressedBytes));

Encryption and decryption of data

Avast ye, ye scallywags! Sometimes ye need to keep yer messages secret, so that no one but the intended recipient can read them. That’s when encryption comes in handy!

Message Digests (MD5, SHA-1, SHA-256, etc.)

Message Digests are a type of one-way hash function that take a message and return a fixed-length string of characters. The hash function is designed to be irreversible, meaning that it’s difficult (if not impossible) to reconstruct the original message from the hash.

Apache Commons Codec provides classes for generating message digests using popular algorithms such as MD5, SHA-1, SHA-256, and more. Here’s an example of how to use the DigestUtils class to generate an MD5 hash of a message:

import org.apache.commons.codec.digest.DigestUtils;

String message = "Dead men tell no tales";
String md5Hash = DigestUtils.md5Hex(message);
System.out.println("MD5 hash of message: " + md5Hash);

Symmetric encryption and decryption (AES, Blowfish, DES, etc.)

Symmetric encryption is a method of encryption where the same key is used for both encryption and decryption. Apache Commons Codec provides classes for symmetric encryption using popular algorithms such as AES, Blowfish, and DES.

Here’s an example of how to use the Crypt class to encrypt and decrypt a message using AES:

import org.apache.commons.codec.digest.Crypt;

String message = "X marks the spot";
String key = "treasure";
String encryptedMessage = Crypt.crypt(message.getBytes(), key);
System.out.println("Encrypted message: " + encryptedMessage);

String decryptedMessage = Crypt.crypt(encryptedMessage, key);
System.out.println("Decrypted message: " + decryptedMessage);

Generating random numbers

Ahoy there, me hearties! Sometimes ye need to generate random numbers for yer adventures on the high seas. That’s when theSecureRandom class comes in handy!

Creating random numbers using SecureRandom class

The SecureRandom class provides a secure way to generate random numbers, which are essential in cryptography and other security-related applications.

Here’s an example of how to use the SecureRandom class to generate a random number:

import java.security.SecureRandom;

SecureRandom random = new SecureRandom();
int randomNumber = random.nextInt(1000);
System.out.println("Random number: " + randomNumber);

Other miscellaneous features

Arrr, me hearties! Apache Commons Codec also provides some other features that can be useful for yer adventures on the high seas. Let’s take a look at them.

DigestUtils

DigestUtils is a utility class that provides methods for generating message digests using popular algorithms such as MD5, SHA-1, SHA-256, and more. We have already used this class to generate an MD5 hash of a message in the Encryption and Decryption section.

StringUtils

StringUtils is a utility class that provides methods for working with strings, such as trimming, concatenating, and padding. It also provides methods for converting strings to different case styles and removing whitespace. Here’s an example of how to use the StringUtils class to pad a string with a certain character:

import org.apache.commons.lang3.StringUtils;

String message = "Pieces of eight";
String paddedMessage = StringUtils.leftPad(message, 20, '*');
System.out.println("Padded message: " + paddedMessage);

CharSetUtils

CharSetUtils is a utility class that provides methods for working with character sets, such as removing characters from a string, translating characters from one set to another, and checking if a string contains only certain characters. Here’s an example of how to use the CharSetUtils class to remove all digits from a string:

import org.apache.commons.lang3.CharSetUtils;

String message = "Shiver me 123 timbers!";
String cleanedMessage = CharSetUtils.delete(message, "0123456789");
System.out.println("Cleaned message: " + cleanedMessage);

ClassUtils

ClassUtils is a utility class that provides methods for working with classes, such as getting the package name of a class, checking if a class is assignable from another class, and getting the class name without the package name. Here’s an example of how to use the ClassUtils class to get the package name of a class:

import org.apache.commons.lang3.ClassUtils;

String packageName = ClassUtils.getPackageName(StringUtils.class);
System.out.println("Package name: " + packageName);

Conclusion

Avast ye, ye scallywags! That be all for our adventure into the world of Apache Commons Codec. We’ve learned about the different encoding and decoding techniques supported by the library, as well as some other miscellaneous features that can be useful for yer adventures on the high seas. Now, go forth and use this knowledge to keep yer messages safe and secure from any landlubber who dares to cross yer path!

SecureRandom class comes in handy!

Generating random numbers using SecureRandom class

The SecureRandom class provides a secure way to generate random numbers that cannot be predicted or manipulated. Apache Commons Codec provides a class called RandomGenerator that uses SecureRandom to generate random numbers.

Here’s an example of how to use RandomGenerator to generate a random number between 0 and 100:

import org.apache.commons.codec.digest.RandomGenerator;

RandomGenerator randomGenerator = new RandomGenerator();
int randomNumber = randomGenerator.nextInt(101);
System.out.println("Random number: " + randomNumber);

Other miscellaneous features

Aside from encoding, decoding, compression, decompression, encryption, decryption, and random number generation, Apache Commons Codec also provides some other miscellaneous features that ye may find useful on yer adventures on the high seas.

String escaping

Sometimes ye may need to escape special characters in a string to make it safe for transmission over a network or through a database. Apache Commons Codec provides a class called StringEscapeUtils that supports escaping and unescaping of strings.

Here’s an example of how to use StringEscapeUtils to escape a string:

import org.apache.commons.codec.StringEscapeUtils;

String message = "Avast, ye scurvy dogs!";
String escapedMessage = StringEscapeUtils.escapeJava(message);
System.out.println("Escaped message: " + escapedMessage);

Hexadecimal and binary conversions

Sometimes ye may need to convert a hexadecimal string into a binary string, or vice versa. Apache Commons Codec provides a class called BinaryCodec that supports binary-to-hexadecimal and hexadecimal-to-binary conversions.

Here’s an example of how to use BinaryCodec to convert a hexadecimal string to a binary string:

import org.apache.commons.codec.binary.BinaryCodec;

String hexadecimalString = "48656c6c6f20576f726c64";
BinaryCodec binaryCodec = new BinaryCodec();
byte[] binaryBytes = binaryCodec.decode(hexadecimalString);
String binaryString = new String(binaryBytes);
System.out.println("Binary string: " + binaryString);

Character encoding detection

Sometimes ye may receive a message or a file with an unknown character encoding. Apache Commons Codec provides a class called CharsetDetector that can detect the character encoding of a message or a file.

Here’s an example of how to use CharsetDetector to detect the character encoding of a string:

import org.apache.commons.codec.CharsetDetector;

String message = "Ahoy, matey!";
CharsetDetector charsetDetector = new CharsetDetector();
charsetDetector.setText(message.getBytes());
String detectedCharset = charsetDetector.detect().getName();
System.out.println("Detected character encoding: " + detectedCharset);

That be all for now, me hearties! We’ve explored the different encoding and decoding techniques, compression and decompression of data, encryption and decryption of data, and random number generation, and other miscellaneous features supported by Apache Commons Codec. Ye can use these tools to keep yer messages and data safe on yer adventures on the high seas. Keep on sailin’!

SecureRandom class comes in handy!

Generating random numbers using SecureRandom class

The SecureRandom class provides a secure way to generate random numbers that cannot be predicted or manipulated. Apache Commons Codec provides a class called RandomGenerator that uses SecureRandom to generate random numbers.

Here’s an example of how to use RandomGenerator to generate a random number between 0 and 100:

import org.apache.commons.codec.digest.RandomGenerator;

RandomGenerator randomGenerator = new RandomGenerator();
int randomNumber = randomGenerator.nextInt(101);
System.out.println("Random number: " + randomNumber);

Other miscellaneous features

Aside from encoding, decoding, compression, decompression, encryption, decryption, and random number generation, Apache Commons Codec also provides some other miscellaneous features that ye may find useful on yer adventures on the high seas.

String escaping

Sometimes ye may need to escape special characters in a string to make it safe for transmission over a network or through a database. Apache Commons Codec provides a class called StringEscapeUtils that supports escaping and unescaping of strings.

Here’s an example of how to use StringEscapeUtils to escape a string:

import org.apache.commons.codec.StringEscapeUtils;

String message = "Avast, ye scurvy dogs!";
String escapedMessage = StringEscapeUtils.escapeJava(message);
System.out.println("Escaped message: " + escapedMessage);

Hexadecimal and binary conversions

Sometimes ye may need to convert a hexadecimal string into a binary string, or vice versa. Apache Commons Codec provides a class called BinaryCodec that supports binary-to-hexadecimal and hexadecimal-to-binary conversions.

Here’s an example of how to use BinaryCodec to convert a hexadecimal string to a binary string:

import org.apache.commons.codec.binary.BinaryCodec;

String hexadecimalString = "48656c6c6f20576f726c64";
BinaryCodec binaryCodec = new BinaryCodec();
byte[] binaryBytes = binaryCodec.decode(hexadecimalString);
String binaryString = new String(binaryBytes);
System.out.println("Binary string: " + binaryString);

Character encoding detection

Sometimes ye may receive a message or a file with an unknown character encoding. Apache Commons Codec provides a class called CharsetDetector that can detect the character encoding of a message or a file.

Here’s an example of how to use CharsetDetector to detect the character encoding of a string:

import org.apache.commons.codec.CharsetDetector;

String message = "Ahoy, matey!";
CharsetDetector charsetDetector = new CharsetDetector();
charsetDetector.setText(message.getBytes());
String detectedCharset = charsetDetector.detect().getName();
System.out.println("Detected character encoding: " + detectedCharset);

That be all for now, me hearties! We’ve explored the different encoding and decoding techniques, compression and decompression of data, encryption and decryption of data, and random number generation, and other miscellaneous features supported by Apache Commons Codec. Ye can use these tools to keep yer messages and data safe on yer adventures on the high seas. Keep on sailin’!

HTML escaping

In addition to string escaping, Apache Commons Codec also provides a class called StringEscapeUtils that supports HTML escaping and unescaping of strings. This can be useful for preventing cross-site scripting (XSS) attacks in web applications.

Here’s an example of how to use StringEscapeUtils to escape an HTML string:

import org.apache.commons.codec.StringEscapeUtils;

String html = "<h1>Welcome aboard!</h1>";
String escapedHtml = StringEscapeUtils.escapeHtml(html);
System.out.println("Escaped HTML: " + escapedHtml);

Preventing cross-site scripting (XSS) attacks

Cross-site scripting (XSS) is a type of security vulnerability in web applications that allows attackers to inject malicious code into a web page viewed by other users. Apache Commons Codec provides a class called StringEscapeUtils that can help prevent XSS attacks by escaping special characters in user input.

Here’s an example of how to use StringEscapeUtils to escape user input in a web form:

import org.apache.commons.codec.StringEscapeUtils;

String userInput = "<script>alert('Ahoy, matey!');</script>";
String escapedUserInput = StringEscapeUtils.escapeHtml(userInput);
System.out.println("Escaped user input: " + escapedUserInput);

That be all for now, me hearties! We’ve explored the different encoding and decoding techniques, compression and decompression of data, encryption and decryption of data, and random number generation, and other miscellaneous features supported by Apache Commons Codec. Ye can use these tools to keep yer messages and data safe on yer adventures on the high seas. Remember to use them wisely, and always keep yer wits about ye! Until next time, me hearties, keep on sailin’!