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.
package org.apache.cxf.common.util;
Base64Utility - this static class provides useful base64
encoding utilities.
- Author(s):
- Darach Ennis
- Craig Ryan
This class converts to/from base64. The alternative conversions include:
encode:
byte[] into String
byte[] into char[]
byte[] into OutStream
byte[] into Writer
decode:
char[] into byte[]
String into byte[]
char[] into OutStream
String into OutStream
private static final char[] BCS = { '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',
private static final char PAD = '=';
private static final int BDTSIZE = 128;
private static final byte[] BDT = new byte[128];
for (int i = 0; i < BDTSIZE; i++) { The
decode_chunk routine decodes a chunk of data
into its native encoding.
base64 encodes each 3 octets of data into 4 characters from a
limited 64 character set. The 3 octets are joined to form
24 bits which are then split into 4 x 6bit values. Each 6 bit
value is then used as an index into the 64 character table of
base64 chars. If the total data length is not a 3 octet multiple
the '=' char is used as padding for the final 4 char group,
either 1 octet + '==' or 2 octets + '='.
- Parameters:
id The input data to be processedo The offset from which to begin processingl The length (bound) at which processing is to end- Returns:
- The decoded data
- Throws:
Base64Exception Thrown is processing fails due to
formatting exceptions in the encoded data
int octetCount = 3 * (l / 4);
octetCount -= (id[l - 2] == PAD) ? 2 : 1;
byte[] ob = new byte[octetCount];
for (int i = o; i < o + l && i < id.length; i++) { if (ibcount == ib.length) { if (obcount != ob.length) { byte []tmp = new byte[obcount];
public static void decode(char[] id,
return new String(cd, 0, cd.length);
out = new char[l / 3 * 4];
out = new char[l / 3 * 4 + 4];
int i = ((id[rindex] & 0xff) << 16)
+ ((id[rindex + 1] & 0xff) << 8)
+ (id[rindex + 2] & 0xff);
out[windex++] = BCS[i >> 18];
out[windex++] = BCS[(i >> 12) & 0x3f];
out[windex++] = BCS[(i >> 6) & 0x3f];
out[windex++] = BCS[i & 0x3f];
int i = id[rindex] & 0xff;
out[windex++] = BCS[i >> 2];
out[windex++] = BCS[(i << 4) & 0x3f];
int i = ((id[rindex] & 0xff) << 8) + (id[rindex + 1] & 0xff);
out[windex++] = BCS[i >> 10];
out[windex++] = BCS[(i >> 4) & 0x3f];
out[windex++] = BCS[(i << 2) & 0x3f];
public static void encode(byte[] id,
The
process routine processes an atomic base64
unit of encoding (encodeme) into its native encoding. This class is
used by decode routines to do the grunt work of decoding
base64 encoded information
- Parameters:
ib Input character buffer of encoded bytesob Output byte buffer of decoded bytesp Pointer to the encodeme of interest- Returns:
- The decoded encodeme
- Throws:
Base64Exception Thrown is processing fails due to
formatting exceptions in the encoded data
ob[p] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3);
ob[p++] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3);
ob[p] = (byte)(b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
ob[p++] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3);
ob[p++] = (byte)(b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
ob[p] = (byte)(b2 << 6 & 0xc0 | b3 & 0x3f);