Java Tutorial/Development/Base64 — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:28, 31 мая 2010
Base64 encoding/decoding.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
/** This class provides Base64 encoding/decoding.
*/
public class Base64
{
private Base64()
{
//Avoid instantiation of this class
}
/** Encodes datas using base64 encoding.
* @param abyte0 data for encoding
* @return encoded string
*/
static String encode(final byte abyte0[])
{
final StringBuffer stringbuffer = new StringBuffer();
for(int i = 0; i < abyte0.length; i += 3)
stringbuffer.append(encodeBlock(abyte0, i));
return stringbuffer.toString();
}
private static char[] encodeBlock(final byte abyte0[], final int i)
{
int j = 0;
final int k = abyte0.length - i - 1;
final int l = k < 2 ? k : 2;
for(int i1 = 0; i1 <= l; i1++)
{
final byte byte0 = abyte0[i + i1];
final int j1 = byte0 >= 0 ? ((int) (byte0)) : byte0 + 256;
j += j1 << 8 * (2 - i1);
}
char ac[] = new char[4];
for(int k1 = 0; k1 < 4; k1++)
{
final int l1 = j >>> 6 * (3 - k1) & 0x3f;
ac[k1] = getChar(l1);
}
if(k < 1)
ac[2] = "=";
if(k < 2)
ac[3] = "=";
return ac;
}
private static char getChar(final int i)
{
if(i >= 0 && i <= 25)
return (char)(65 + i);
if(i >= 26 && i <= 51)
return (char)(97 + (i - 26));
if(i >= 52 && i <= 61)
return (char)(48 + (i - 52));
if(i == 62)
return "+";
return i != 63 ? "?" : "/";
}
/** Decode string using Base64 encoding.
* @param s string for decoding
* @return decoded data
*/
static byte[] decode(final String s)
{
if (s.length() == 0) return new byte[0];
int i = 0;
for(int j = s.length() - 1; j > 0 && s.charAt(j) == "="; j--)
i++;
final int k = (s.length() * 6) / 8 - i;
byte abyte0[] = new byte[k];
int l = 0;
for(int i1 = 0; i1 < s.length(); i1 += 4)
{
final int j1 = (getValue(s.charAt(i1)) << 18) + (getValue(s.charAt(i1 + 1)) << 12) + (getValue(s.charAt(i1 + 2)) << 6) + getValue(s.charAt(i1 + 3));
for(int k1 = 0; k1 < 3 && l + k1 < abyte0.length; k1++)
abyte0[l + k1] = (byte)(j1 >> 8 * (2 - k1) & 0xff);
l += 3;
}
return abyte0;
}
private static int getValue(final char c)
{
if(c >= "A" && c <= "Z")
return c - 65;
if(c >= "a" && c <= "z")
return (c - 97) + 26;
if(c >= "0" && c <= "9")
return (c - 48) + 52;
if(c == "+")
return 62;
if(c == "/")
return 63;
return c != "=" ? -1 : 0;
}
}
Encode/decode for RFC ase64 as defined by RFC 2045
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This class provides encode/decode for RFC 2045 Base64 as defined by
* RFC 2045, N. Freed and N. Borenstein. :
* Multipurpose Internet Mail Extensions (MIME) Part One: Format of
* Internet Message Bodies. Reference 1996
*
* @author Jeffrey Rodriguez
* @version $Id: Base64.java 515 2008-03-17 21:02:23Z jfrederic.clere@jboss.ru $
*/
public final class Base64
{
static private final int BASELENGTH = 255;
static private final int LOOKUPLENGTH = 64;
static private final int TWENTYFOURBITGROUP = 24;
static private final int EIGHTBIT = 8;
static private final int SIXTEENBIT = 16;
static private final int SIXBIT = 6;
static private final int FOURBYTE = 4;
static private final int SIGN = -128;
static private final byte PAD = (byte) "=";
static private byte [] base64Alphabet = new byte[BASELENGTH];
static private byte [] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
//static private final Log log = LogSource.getInstance("org.apache.rumons.util.Base64");
static
{
for (int i = 0; i < BASELENGTH; i++ )
{
base64Alphabet[i] = -1;
}
for (int i = "Z"; i >= "A"; i--)
{
base64Alphabet[i] = (byte) (i - "A");
}
for (int i = "z"; i>= "a"; i--)
{
base64Alphabet[i] = (byte) (i - "a" + 26);
}
for (int i = "9"; i >= "0"; i--)
{
base64Alphabet[i] = (byte) (i - "0" + 52);
}
base64Alphabet["+"] = 62;
base64Alphabet["/"] = 63;
for (int i = 0; i <= 25; i++ )
lookUpBase64Alphabet[i] = (byte) ("A" + i);
for (int i = 26, j = 0; i <= 51; i++, j++ )
lookUpBase64Alphabet[i] = (byte) ("a"+ j);
for (int i = 52, j = 0; i <= 61; i++, j++ )
lookUpBase64Alphabet[i] = (byte) ("0" + j);
lookUpBase64Alphabet[62] = (byte) "+";
lookUpBase64Alphabet[63] = (byte) "/";
}
public static boolean isBase64( String isValidString )
{
return isArrayByteBase64(isValidString.getBytes());
}
public static boolean isBase64( byte octect )
{
//shall we ignore white space? JEFF??
return (octect == PAD || base64Alphabet[octect] != -1);
}
public static boolean isArrayByteBase64( byte[] arrayOctect )
{
int length = arrayOctect.length;
if (length == 0)
{
// shouldn"t a 0 length array be valid base64 data?
// return false;
return true;
}
for (int i=0; i < length; i++)
{
if ( !Base64.isBase64(arrayOctect[i]) )
return false;
}
return true;
}
/**
* Encodes hex octects into Base64.
*
* @param binaryData Array containing binary data to encode.
* @return Base64-encoded data.
*/
public static byte[] encode( byte[] binaryData )
{
int lengthDataBits = binaryData.length*EIGHTBIT;
int fewerThan24bits = lengthDataBits%TWENTYFOURBITGROUP;
int numberTriplets = lengthDataBits/TWENTYFOURBITGROUP;
byte encodedData[] = null;
if (fewerThan24bits != 0)
{
//data not divisible by 24 bit
encodedData = new byte[ (numberTriplets + 1 ) * 4 ];
}
else
{
// 16 or 8 bit
encodedData = new byte[ numberTriplets * 4 ];
}
byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
int encodedIndex = 0;
int dataIndex = 0;
int i = 0;
//log.debug("number of triplets = " + numberTriplets);
for ( i = 0; i<numberTriplets; i++ )
{
dataIndex = i*3;
b1 = binaryData[dataIndex];
b2 = binaryData[dataIndex + 1];
b3 = binaryData[dataIndex + 2];
//log.debug("b1= " + b1 +", b2= " + b2 + ", b3= " + b3);
l = (byte)(b2 & 0x0f);
k = (byte)(b1 & 0x03);
encodedIndex = i * 4;
byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
byte val3 = ((b3 & SIGN)==0)?(byte)(b3>>6):(byte)((b3)>>6^0xfc);
encodedData[encodedIndex] = lookUpBase64Alphabet[ val1 ];
//log.debug( "val2 = " + val2 );
//log.debug( "k4 = " + (k<<4) );
//log.debug( "vak = " + (val2 | (k<<4)) );
encodedData[encodedIndex+1] =
lookUpBase64Alphabet[ val2 | ( k<<4 )];
encodedData[encodedIndex+2] =
lookUpBase64Alphabet[ (l <<2 ) | val3 ];
encodedData[encodedIndex+3] = lookUpBase64Alphabet[ b3 & 0x3f ];
}
// form integral number of 6-bit groups
dataIndex = i*3;
encodedIndex = i*4;
if (fewerThan24bits == EIGHTBIT )
{
b1 = binaryData[dataIndex];
k = (byte) ( b1 &0x03 );
//log.debug("b1=" + b1);
//log.debug("b1<<2 = " + (b1>>2) );
byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
encodedData[encodedIndex] = lookUpBase64Alphabet[ val1 ];
encodedData[encodedIndex + 1] = lookUpBase64Alphabet[ k<<4 ];
encodedData[encodedIndex + 2] = PAD;
encodedData[encodedIndex + 3] = PAD;
}
else if (fewerThan24bits == SIXTEENBIT)
{
b1 = binaryData[dataIndex];
b2 = binaryData[dataIndex +1 ];
l = (byte) (b2 & 0x0f);
k = (byte) (b1 & 0x03);
byte val1 = ((b1 & SIGN) == 0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
byte val2 = ((b2 & SIGN) == 0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
encodedData[encodedIndex] = lookUpBase64Alphabet[ val1 ];
encodedData[encodedIndex + 1] =
lookUpBase64Alphabet[ val2 | ( k<<4 )];
encodedData[encodedIndex + 2] = lookUpBase64Alphabet[ l<<2 ];
encodedData[encodedIndex + 3] = PAD;
}
return encodedData;
}
}
to Base64
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
public class EncodeUtils {
public static final int MASK = 0x3F;
public static final int FIRST_MASK = MASK << 18;
public static final int SECOND_MASK = MASK << 12;
public static final int THIRD_MASK = MASK << 6;
public static final int FORTH_MASK = MASK;
public static final byte[] ENCODING = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O", "P" ,"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i",
"j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3",
"4", "5", "6", "7", "8", "9", "+", "/"};
public static final void main(String[] args) throws Exception {
byte[] bytes = {(byte) 0, (byte) 128, (byte) 0};
System.out.println(new String(toBase64(bytes)));
System.out.println(new String(toBase64("Hello, World".getBytes())));
System.out.println(new String(toBase64("Monday".getBytes())));
System.out.println(new String(toBase64("M\u00F6nchengladbach\r\n".getBytes("ISO-8859-1"))));
}
public static byte[] toBase64(byte[] in) {
int inputLength = in.length;
int outputLength = (int) Math.floor((4*inputLength) / 3f) + 3;
outputLength = outputLength + 2 * (int) Math.floor(outputLength / 76f);
byte[] results = new byte[outputLength];
int inputIndex = 0;
int outputIndex = 0;
while (inputLength - inputIndex > 2) {
int one = (toInt(in[inputIndex++]) << 16);
int two = (toInt(in[inputIndex++]) << 8);
int three = toInt(in[inputIndex++]);
int quantum = one | two | three;
int index = (quantum & FIRST_MASK) >> 18;
outputIndex = setResult(results, outputIndex, ENCODING[index]);
index = (quantum & SECOND_MASK) >> 12;
outputIndex = setResult(results, outputIndex, ENCODING[index]);
index = (quantum & THIRD_MASK) >> 6;
outputIndex = setResult(results, outputIndex, ENCODING[index]);
index = (quantum & FORTH_MASK);
outputIndex = setResult(results, outputIndex, ENCODING[index]);
}
switch (inputLength - inputIndex) {
case 1:
int quantum = in[inputIndex++] << 16;
int index = (quantum & FIRST_MASK) >> 18;
outputIndex = setResult(results, outputIndex, ENCODING[index]);
index = (quantum & SECOND_MASK) >> 12;
outputIndex = setResult(results, outputIndex, ENCODING[index]);
outputIndex = setResult(results, outputIndex, (byte) "=");
outputIndex = setResult(results, outputIndex, (byte) "=");
break;
case 2:
quantum = (in[inputIndex++] << 16) + (in[inputIndex++] << 8);
index = (quantum & FIRST_MASK) >> 18;
outputIndex = setResult(results, outputIndex, ENCODING[index]);
index = (quantum & SECOND_MASK) >> 12;
outputIndex = setResult(results, outputIndex, ENCODING[index]);
index = (quantum & THIRD_MASK) >> 6;
outputIndex = setResult(results, outputIndex, ENCODING[index]);
outputIndex = setResult(results, outputIndex, (byte) "=");
break;
}
return results;
}
private static int toInt(byte b) {
return 255 & b;
}
private static int setResult(byte[] results, int outputIndex, byte value) {
results[outputIndex++] = value;
outputIndex = checkLineLength(results, outputIndex);
return outputIndex;
}
private static int checkLineLength(byte[] results, int outputIndex) {
if (outputIndex == 76 || outputIndex > 76 && (outputIndex - 2*Math.floor(outputIndex/76f - 1)) % 76 == 0) {
results[outputIndex++] = "\r";
results[outputIndex++] = "\n";
}
return outputIndex;
}
}