Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ziqueiros/8872680 to your computer and use it in GitHub Desktop.
Save ziqueiros/8872680 to your computer and use it in GitHub Desktop.
base64 study
// Copyright 2003-2010 Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland
// www.source-code.biz, www.inventec.ch/chdh
//
// This module is multi-licensed and may be used under the terms
// of any of the following licenses:
//
// EPL, Eclipse Public License, V1.0 or later, http://www.eclipse.org/legal
// LGPL, GNU Lesser General Public License, V2.1 or later, http://www.gnu.org/licenses/lgpl.html
// GPL, GNU General Public License, V2 or later, http://www.gnu.org/licenses/gpl.html
// AL, Apache License, V2.0 or later, http://www.apache.org/licenses
// BSD, BSD License, http://www.opensource.org/licenses/bsd-license.php
// MIT, MIT License, http://www.opensource.org/licenses/MIT
//
// Please contact the author if you need another license.
// This module is provided "as is", without warranties of any kind.
package biz.source_code.base64Coder;
/**
* A Base64 encoder/decoder.
*
* <p>
* This class is used to encode and decode data in Base64 format as described in
* RFC 1521.
*
* <p>
* Project home page: <a
* href="http://www.source-code.biz/base64coder/java/">www.
* source-code.biz/base64coder/java</a><br>
* Author: Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland<br>
* Multi-licensed: EPL / LGPL / GPL / AL / BSD / MIT.
*/
public class Base64Coder {
// The line separator string of the operating system.
private static final String systemLineSeparator = System
.getProperty("line.separator");
// Mapping table from 6-bit nibbles to Base64 characters.
private static final char[] map1 = new char[64];
static {
int i = 0;
for (char c = 'A'; c <= 'Z'; c++)
map1[i++] = c;
for (char c = 'a'; c <= 'z'; c++)
map1[i++] = c;
for (char c = '0'; c <= '9'; c++)
map1[i++] = c;
map1[i++] = '+';
map1[i++] = '/';
}
// Mapping table from Base64 characters to 6-bit nibbles.
private static final byte[] map2 = new byte[128];
static {
for (int i = 0; i < map2.length; i++)
map2[i] = -1;
for (int i = 0; i < 64; i++)
map2[map1[i]] = (byte) i;
}
/**
* Encodes a string into Base64 format. No blanks or line breaks are
* inserted.
*
* @param s
* A String to be encoded.
* @return A String containing the Base64 encoded data.
*/
public static String encodeString(String s) {
return new String(encode(s.getBytes()));
}
/**
* Encodes a byte array into Base 64 format and breaks the output into lines
* of 76 characters. This method is compatible with
* <code>sun.misc.BASE64Encoder.encodeBuffer(byte[])</code>.
*
* @param in
* An array containing the data bytes to be encoded.
* @return A String containing the Base64 encoded data, broken into lines.
*/
public static String encodeLines(byte[] in) {
return encodeLines(in, 0, in.length, 76, systemLineSeparator);
}
/**
* Encodes a byte array into Base 64 format and breaks the output into
* lines.
*
* @param in
* An array containing the data bytes to be encoded.
* @param iOff
* Offset of the first byte in <code>in</code> to be processed.
* @param iLen
* Number of bytes to be processed in <code>in</code>, starting
* at <code>iOff</code>.
* @param lineLen
* Line length for the output data. Should be a multiple of 4.
* @param lineSeparator
* The line separator to be used to separate the output lines.
* @return A String containing the Base64 encoded data, broken into lines.
*/
public static String encodeLines(byte[] in, int iOff, int iLen,
int lineLen, String lineSeparator) {
int blockLen = (lineLen * 3) / 4;
if (blockLen <= 0)
throw new IllegalArgumentException();
int lines = (iLen + blockLen - 1) / blockLen;
int bufLen = ((iLen + 2) / 3) * 4 + lines * lineSeparator.length();
StringBuilder buf = new StringBuilder(bufLen);
int ip = 0;
while (ip < iLen) {
int l = Math.min(iLen - ip, blockLen);
buf.append(encode(in, iOff + ip, l));
buf.append(lineSeparator);
ip += l;
}
return buf.toString();
}
/**
* Encodes a byte array into Base64 format. No blanks or line breaks are
* inserted in the output.
*
* @param in
* An array containing the data bytes to be encoded.
* @return A character array containing the Base64 encoded data.
*/
public static char[] encode(byte[] in) {
return encode(in, 0, in.length);
}
/**
* Encodes a byte array into Base64 format. No blanks or line breaks are
* inserted in the output.
*
* @param in
* An array containing the data bytes to be encoded.
* @param iLen
* Number of bytes to process in <code>in</code>.
* @return A character array containing the Base64 encoded data.
*/
public static char[] encode(byte[] in, int iLen) {
return encode(in, 0, iLen);
}
/**
* Encodes a byte array into Base64 format. No blanks or line breaks are
* inserted in the output.
*
* @param in
* An array containing the data bytes to be encoded.
* @param iOff
* Offset of the first byte in <code>in</code> to be processed.
* @param iLen
* Number of bytes to process in <code>in</code>, starting at
* <code>iOff</code>.
* @return A character array containing the Base64 encoded data.
*/
public static char[] encode(byte[] in, int iOff, int iLen) {
int oDataLen = (iLen * 4 + 2) / 3; // output length without padding
int oLen = ((iLen + 2) / 3) * 4; // output length including padding
char[] out = new char[oLen];
int ip = iOff;
int iEnd = iOff + iLen;
int op = 0;
while (ip < iEnd) {
int i0 = in[ip++] & 0xff;
int i1 = ip < iEnd ? in[ip++] & 0xff : 0;
int i2 = ip < iEnd ? in[ip++] & 0xff : 0;
//Verify bitwise review to explanation on this operators
int o0 = i0 >>> 2;
int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
int o3 = i2 & 0x3F;
out[op++] = map1[o0];
out[op++] = map1[o1];
out[op] = op < oDataLen ? map1[o2] : '=';
op++;
out[op] = op < oDataLen ? map1[o3] : '=';
op++;
}
return out;
}
/**
* Decodes a string from Base64 format. No blanks or line breaks are allowed
* within the Base64 encoded input data.
*
* @param s
* A Base64 String to be decoded.
* @return A String containing the decoded data.
* @throws IllegalArgumentException
* If the input is not valid Base64 encoded data.
*/
public static String decodeString(String s) {
return new String(decode(s));
}
/**
* Decodes a byte array from Base64 format and ignores line separators, tabs
* and blanks. CR, LF, Tab and Space characters are ignored in the input
* data. This method is compatible with
* <code>sun.misc.BASE64Decoder.decodeBuffer(String)</code>.
*
* @param s
* A Base64 String to be decoded.
* @return An array containing the decoded data bytes.
* @throws IllegalArgumentException
* If the input is not valid Base64 encoded data.
*/
public static byte[] decodeLines(String s) {
char[] buf = new char[s.length()];
int p = 0;
for (int ip = 0; ip < s.length(); ip++) {
char c = s.charAt(ip);
if (c != ' ' && c != '\r' && c != '\n' && c != '\t')
buf[p++] = c;
}
return decode(buf, 0, p);
}
/**
* Decodes a byte array from Base64 format. No blanks or line breaks are
* allowed within the Base64 encoded input data.
*
* @param s
* A Base64 String to be decoded.
* @return An array containing the decoded data bytes.
* @throws IllegalArgumentException
* If the input is not valid Base64 encoded data.
*/
public static byte[] decode(String s) {
return decode(s.toCharArray());
}
/**
* Decodes a byte array from Base64 format. No blanks or line breaks are
* allowed within the Base64 encoded input data.
*
* @param in
* A character array containing the Base64 encoded data.
* @return An array containing the decoded data bytes.
* @throws IllegalArgumentException
* If the input is not valid Base64 encoded data.
*/
public static byte[] decode(char[] in) {
return decode(in, 0, in.length);
}
/**
* Decodes a byte array from Base64 format. No blanks or line breaks are
* allowed within the Base64 encoded input data.
*
* @param in
* A character array containing the Base64 encoded data.
* @param iOff
* Offset of the first character in <code>in</code> to be
* processed.
* @param iLen
* Number of characters to process in <code>in</code>, starting
* at <code>iOff</code>.
* @return An array containing the decoded data bytes.
* @throws IllegalArgumentException
* If the input is not valid Base64 encoded data.
*/
public static byte[] decode(char[] in, int iOff, int iLen) {
if (iLen % 4 != 0)
throw new IllegalArgumentException(
"Length of Base64 encoded input string is not a multiple of 4.");
while (iLen > 0 && in[iOff + iLen - 1] == '=')
iLen--;
int oLen = (iLen * 3) / 4;
byte[] out = new byte[oLen];
int ip = iOff;
int iEnd = iOff + iLen;
int op = 0;
while (ip < iEnd) {
int i0 = in[ip++];
int i1 = in[ip++];
int i2 = ip < iEnd ? in[ip++] : 'A';
int i3 = ip < iEnd ? in[ip++] : 'A';
if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
throw new IllegalArgumentException(
"Illegal character in Base64 encoded data.");
int b0 = map2[i0];
int b1 = map2[i1];
int b2 = map2[i2];
int b3 = map2[i3];
if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
throw new IllegalArgumentException(
"Illegal character in Base64 encoded data.");
int o0 = (b0 << 2) | (b1 >>> 4);
int o1 = ((b1 & 0xf) << 4) | (b2 >>> 2);
int o2 = ((b2 & 3) << 6) | b3;
out[op++] = (byte) o0;
if (op < oLen)
out[op++] = (byte) o1;
if (op < oLen)
out[op++] = (byte) o2;
}
return out;
}
// Dummy constructor.
private Base64Coder() {
}
} // end class Base64Coder
package biz.source_code.base64Coder;
/**
* Check for reference
* http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.19
* http://en.wikipedia.org/wiki/Bitwise_operation#Shifts_in_Java
*
* @author jzamora02
*
*/
public class BitwiseReview {
private static final String ZEROS = "00000";
public static void main(String... args){
int bitmask = 0x000F;
int val = 0x2222;
// prints "2"
System.out.println("Bitmask " + (val & bitmask));
val = 0xF00F;
//The unsigned right shift operator ">>>" shifts a zero into the leftmost
//position, while the leftmost position after ">>" depends on sign extension.
for(int i=0;i<10;i++){
System.out.format(i+" %h - ",val); //%n platform independent newline character
System.out.format("%d%n",val); //%n platform independent newline character
val = val >>> 1; //BE CAREFULL YOU ARE SHIFTING ONE POSITION
}
//Keep in mind
//If n is positive, then the result is the same as that of n >> s.
//If n is negative and the type of the left-hand operand is int, then the result is equal to that of the expression (n >> s) + (2 << ~s).
//If n is negative and the type of the left-hand operand is long, then the result is equal to that of the expression (n >> s) + (2L << ~s).
val = 0xF00F;
//The unsigned right shift operator ">>>" shifts a zero into the leftmost
//position, while the leftmost position after ">>" depends on sign extension.
for(int j=0;j<10;j++){
System.out.format(j+" %h - ",val); //%n platform independent newline character
System.out.format("%d%n",val); //%n platform independent newline character
val = val >> 1;
}
System.out.println(Integer.toBinaryString(0xF00F));
System.out.println(Integer.toBinaryString(0xF00F>>>1));
System.out.println(Integer.toBinaryString(-1));
System.out.println(Integer.toBinaryString(-1>>>30));
//See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.19
//If the promoted type of the left-hand operand is int, only the five lowest-order
//bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand
//were subjected to a bitwise logical AND operator & (�15.22.1) with the mask value 0x1f (0b11111).
//The shift distance actually used is therefore always in the range 0 to 31, INCLUSIVE.
//that is -1 >>> 32 is equivalent to -1 >>> 0 and -1 >>> 33 is equivalent to -1 >>> 1 and,
//especially confusing, -1 >>> -1 is equivalent to -1 >>> 31
System.out.println(Integer.toBinaryString(-1>>>32));
}
/**
* Formats the string to the specified length using the padString and padded either left (PAD_LEFT) or right (PAD_RIGHT)
* @param str
* @param len
* @param padStr
* @param padLeft true if pad left, false otherwise (use constants PAD_LEFT, PAD_RIGHT)
* @return
*/
private static String formatStrLen(final String str, final int len) {
return str == null || str.length() == 0 ? ZEROS.substring(0, len) :
str.length() >= len ? str : ZEROS.substring(str.length(), len) + str;
}
}
package operations.file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
* Java 7 File IO
* @author jzamora02
*
*/
public class Tipical {
public static void main(String... aArgs) throws IOException{
Tipical text = new Tipical();
//treat as a small file
List<String> lines = text.readSmallTextFile(FILE_NAME);
//log(lines);
//text.writeSmallTextFile(lines, FILE_NAME);
//treat as a large file - use some buffering
//text.readLargerTextFile(FILE_NAME);
//lines = Arrays.asList("Down to the Waterline", "Water of Love");
text.writeLargerTextFile(OUTPUT_FILE_NAME, lines);
}
final static String FILE_NAME = "C:\\temp\\test.txt";
final static String OUTPUT_FILE_NAME = "C:\\temp\\output.txt";
final static Charset ENCODING = StandardCharsets.UTF_8;
//For smaller files
/**
Note: the javadoc of Files.readAllLines says it's intended for small
files. But its implementation uses buffering, so it's likely good
even for fairly large files.
*/
List<String> readSmallTextFile(String aFileName) throws IOException {
Path path = Paths.get(aFileName);
return Files.readAllLines(path, ENCODING);
}
void writeSmallTextFile(List<String> aLines, String aFileName) throws IOException {
Path path = Paths.get(aFileName);
Files.write(path, aLines, ENCODING);
}
//For larger files
void readLargerTextFile(String aFileName) throws IOException {
Path path = Paths.get(aFileName);
try (Scanner scanner = new Scanner(path, ENCODING.name())){
while (scanner.hasNextLine()){
//process each line in some way
log(scanner.nextLine());
}
}
}
void readLargerTextFileAlternate(String aFileName) throws IOException {
Path path = Paths.get(aFileName);
try (BufferedReader reader = Files.newBufferedReader(path, ENCODING)){
String line = null;
while ((line = reader.readLine()) != null) {
//process each line in some way
log(line);
}
}
}
void writeLargerTextFile(String aFileName, List<String> aLines) throws IOException {
Path path = Paths.get(aFileName);
try (BufferedWriter writer = Files.newBufferedWriter(path, ENCODING)){
for(String line : aLines){
splitLines(60,line,writer);
}
}
}
void splitLines(int length,String line,BufferedWriter writer) throws IOException
{
int index = 0;
String temporal = "";
if (line.length() > length) {
index = line.indexOf(" ",length);
if(index > 0){
temporal = line.substring(0,index);
} else {
temporal = line.substring(0,length);
index = length;
}
writer.write(temporal);
writer.newLine();
splitLines( length,line.substring(index+1,line.length()),writer );
} else {
writer.write(line);
writer.newLine();
return;
}
}
private static void log(Object aMsg){
System.out.println(String.valueOf(aMsg));
}
}
// Sample program to decode a Base64 text file into a binary file.
// Author: Christian d'Heureuse (www.source-code.biz)
package samples;
import biz.source_code.base64Coder.Base64Coder;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
public class Base64FileDecoder {
public static void main (String args[]) throws IOException {
if (args.length != 2) {
System.out.println ("Command line parameters: inputFileName outputFileName");
System.exit (9); }
decodeFile (args[0], args[1]); }
private static void decodeFile (String inputFileName, String outputFileName) throws IOException {
BufferedReader in = null;
BufferedOutputStream out = null;
try {
in = new BufferedReader(new FileReader(inputFileName));
out = new BufferedOutputStream(new FileOutputStream(outputFileName));
decodeStream (in, out);
out.flush(); }
finally {
if (in != null) in.close();
if (out != null) out.close(); }}
private static void decodeStream (BufferedReader in, OutputStream out) throws IOException {
while (true) {
String s = in.readLine();
if (s == null) break;
byte[] buf = Base64Coder.decodeLines(s);
out.write (buf); }}
} // end class Base64FileDecoder
// Sample program to encode a binary file into a Base64 text file.
// Author: Christian d'Heureuse (www.source-code.biz)
package samples;
import biz.source_code.base64Coder.Base64Coder;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.IOException;
public class Base64FileEncoder {
public static void main(String args[]) throws IOException {
if (args.length != 2) {
System.out
.println("Command line parameters: inputFileName outputFileName");
System.exit(9);
}
encodeFile(args[0], args[1]);
}
public static void encodeFile(String inputFileName, String outputFileName)
throws IOException {
BufferedInputStream in = null;
BufferedWriter out = null;
try {
in = new BufferedInputStream(new FileInputStream(inputFileName));
out = new BufferedWriter(new FileWriter(outputFileName));
encodeStream(in, out);
out.flush();
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}
public static void encodeStream(InputStream in, BufferedWriter out)
throws IOException {
int lineLength = 72;
byte[] buf = new byte[lineLength / 4 * 3];
while (true) {
int len = in.read(buf);
if (len <= 0)
break;
out.write(Base64Coder.encode(buf, 0, len));
out.newLine();
}
}
} // end class Base64FileEncoder
// Tests for the Base64Coder class.
package test;
import biz.source_code.base64Coder.Base64Coder;
import java.util.Random;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import org.junit.Test;
public class TestBase64Coder {
// Test Base64Coder with constant strings.
@Test
public void test1() {
check ("Aladdin:open sesame", "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="); // example from RFC 2617
check ("", "");
check ("Man", "TWFu"); //Wikipedia example
check ("1", "MQ==");
check ("22", "MjI=");
check ("333", "MzMz");
check ("4444", "NDQ0NA==");
check ("55555", "NTU1NTU=");
check ("abc:def", "YWJjOmRlZg=="); }
private static void check (String plainText, String base64Text)
{
String s1 = Base64Coder.encodeString(plainText);
String s2 = Base64Coder.decodeString(base64Text);
if (!s1.equals(base64Text) || !s2.equals(plainText))
fail ("Check failed for \""+plainText+"\" / \""+base64Text+"\".");
}
// Test Base64Coder against sun.misc.BASE64Encoder/Decoder with random data.
// Line length below 76.
@Test
public void test2() throws Exception {
final int maxLineLen = 76 - 1; // the Sun encoder adds a CR/LF when a line is longer
final int maxDataBlockLen = (maxLineLen*3) / 4;
sun.misc.BASE64Encoder sunEncoder = new sun.misc.BASE64Encoder();
sun.misc.BASE64Decoder sunDecoder = new sun.misc.BASE64Decoder();
Random rnd = new Random(0x538afb92);
for (int i=0; i<50000; i++) {
int len = rnd.nextInt(maxDataBlockLen+1);
byte[] b0 = new byte[len];
rnd.nextBytes(b0);
String e1 = new String(Base64Coder.encode(b0));
String e2 = sunEncoder.encode(b0);
assertEquals (e2, e1);
byte[] b1 = Base64Coder.decode(e1);
byte[] b2 = sunDecoder.decodeBuffer(e2);
assertArrayEquals (b0, b1);
assertArrayEquals (b0, b2); }}
// Test Base64Coder line encoding/decoding against sun.misc.BASE64Encoder/Decoder
// with random data.
@Test
public void test3() throws Exception {
final int maxDataBlockLen = 512;
sun.misc.BASE64Encoder sunEncoder = new sun.misc.BASE64Encoder();
sun.misc.BASE64Decoder sunDecoder = new sun.misc.BASE64Decoder();
Random rnd = new Random(0x39ac7d6e);
for (int i=0; i<10000; i++) {
int len = rnd.nextInt(maxDataBlockLen+1);
byte[] b0 = new byte[len];
rnd.nextBytes(b0);
String e1 = new String(Base64Coder.encodeLines(b0));
String e2 = sunEncoder.encodeBuffer(b0);
assertEquals (e2, e1);
byte[] b1 = Base64Coder.decodeLines(e1);
byte[] b2 = sunDecoder.decodeBuffer(e2);
assertArrayEquals (b0, b1);
assertArrayEquals (b0, b2); }}
} // end class TestBase64Coder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment