Here you can find the source of decode(byte[] bytes)
Parameter | Description |
---|---|
bytes | string to decode |
public static byte[] decode(byte[] bytes) throws IOException
//package com.java2s; //License from project: Apache License import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { private static final int BUFFER_SIZE = 1024; public static final String ENCODING = "UTF-8"; private static byte[] B64LOOKUP = new byte[256]; /**/*from w w w .j a va 2 s . c o m*/ * Decodes the specified string. * * @param value string to decode * @return byte[] */ public static byte[] decode(String value) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(value.getBytes(ENCODING)); ByteArrayOutputStream out = new ByteArrayOutputStream(); decode(in, out); return out.toByteArray(); } /** * Decodes the specified bytes. * * @param bytes string to decode * @return byte[] */ public static byte[] decode(byte[] bytes) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(bytes); ByteArrayOutputStream out = new ByteArrayOutputStream(); decode(in, out); return out.toByteArray(); } /** * Create a decoder to decode a String to the specified output stream * * @param value string to be decoded. */ public static void decode(String value, OutputStream out) throws IOException { decode(new ByteArrayInputStream(value.getBytes(ENCODING)), out); } /** * Create a decoder to decode a stream. * * @param in The input stream (to be decoded). * @param out The output stream, to write decoded data to. */ public static void decode(InputStream in, OutputStream out) throws IOException { byte buffer[] = new byte[BUFFER_SIZE]; int shift = 0; // # of excess bits stored in accum int accum = 0; // excess bits int index = 0; int c = -1; // read through the input stream while ((c = in.read(buffer)) > 0) { for (int i = 0; i < c; i++) { int value = B64LOOKUP[buffer[i] & 0xFF]; // ignore high byte of char // skip over non-code if (value >= 0) { accum <<= 6; // bits shift up by 6 each time thru shift += 6; // loop, with new bits being put in accum |= value; // at the bottom. // whenever there are 8 or more shifted in, if (shift >= 8) { shift -= 8; // write them out from the top, leaving any excess at the bottom for next iteration. out.write((byte) ((accum >> shift) & 0xff)); } } } } } }