Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
org.apache.commons.codec.binary.Base64 Example
What is org.apache.commons.codec.binary.Base64 class? How to use org.apache.commons.codec.binary.Base64 class?
✍: FYIcenter.com
org.apache.commons.codec.binary.Base64 class is a Java class
offered in commons-codec.jar that
provides Base64 encoding and decoding as defined by RFC 2045.
Two commonly used static methods in org.apache.commons.codec.binary.Base64 class are:
1. The encoding method:
public static byte[] encodeBase64(byte[] binaryData)
Encodes binary data using the base64 algorithm but does not chunk the output.
Parameters:
binaryData - binary data to encode
Returns:
byte[] containing Base64 characters in their UTF-8 representation.
2. The decoding method:
public static byte[] decodeBase64(byte[] base64Data)
Decodes Base64 data into octets.
Note: this method seamlessly handles data encoded in URL-safe or normal mode.
Parameters:
base64Data - Byte array containing Base64 data
Returns:
Array containing decoded data.
Here is a simple example of org.apache.commons.codec.binary.Base64 class:
// Copyright (c) 2016 FYIcenter.com
import org.apache.commons.codec.binary.Base64;
// Example of using the Base64 class
public class Base64Example {
public static void main(String[] args) throws Exception {
System.out.println("encodeBase64() Example:");
String inputString = "Hello World";
byte[] inputBytes = inputString.getBytes("UTF8");
byte[] outputBytes = Base64.encodeBase64(inputBytes);
String outputString = new String(outputBytes,"UTF8");
String expectedString = "SGVsbG8gV29ybGQ=";
System.out.println(" Input string: "+inputString);
System.out.println(" Encoded string: "+outputString);
System.out.println(" Expected string: "+expectedString);
System.out.println("decodeBase64() Example:");
inputString = "SGVsbG8gV29ybGQ=";
inputBytes = inputString.getBytes("UTF8");
outputBytes = Base64.decodeBase64(inputBytes);
outputString = new String(outputBytes,"UTF8");
expectedString = "Hello World";
System.out.println(" Input string: "+inputString);
System.out.println(" Encoded string: "+outputString);
System.out.println(" Expected string: "+expectedString);
}
}
You can compile and run the above example in a command window as shown below:
C:\fyicenter>\local\jdk-1.8.0\bin\javac -cp C:\local\commons-codec-1.10\commons-codec-1.10.jar Base64Example.java C:\fyicenter>\local\jdk-1.8.0\bin\java -cp .;C:\local\commons-codec-1.10\commons-codec-1.10.jar Base64Example encodeBase64() Example: Input string: Hello World Encoded string: SGVsbG8gV29ybGQ= Expected string: SGVsbG8gV29ybGQ= decodeBase64() Example: Input string: SGVsbG8gV29ybGQ= Encoded string: Hello World Expected string: Hello World
⇒ org.apache.commons.codec.binary.BinaryCodec Example
⇐ Class Packages in Apache commons-codec-1.10.jar
2017-04-08, ∼3660🔥, 0💬
Popular Posts:
JDK 11 java.compiler.jmod is the JMOD file for JDK 11 Compiler module. JDK 11 Compiler module compil...
Jackson is "the Java JSON library" or "the best JSON parser for Java". Or simply as "JSON for Java"....
How to download and install ojdbc6.jar for Oracle 11g R2? ojdbc6.jar for Oracle 11g R2 is a Java 6, ...
JDK 11 jdk.internal.vm.ci.jmod is the JMOD file for JDK 11 Internal VM CI module. JDK 11 Internal VM...
JDK 11 jdk.jcmd.jmod is the JMOD file for JDK 11 JCmd tool, which can be invoked by the "jcmd" comma...