net.arnx.wmf2svg.util.Base64 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wmf2svg Show documentation
Show all versions of wmf2svg Show documentation
WMF to SVG Converting Tool & Library
/*
* Copyright 2007-2008 Hidekatsu Izuno
*
* Licensed 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 net.arnx.wmf2svg.util;
/**
* @author Hidekatsu Izuno
*/
public class Base64 {
private static final char[] ENCODE_DATA = {
'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 String encode(byte[] data) {
char[] buffer = null;
if (data.length % 3 == 0) {
buffer = new char[data.length / 3 * 4];
} else {
buffer = new char[(data.length / 3 + 1) * 4];
}
int buf = 0;
for (int i = 0; i < data.length; i++) {
switch (i % 3) {
case 0 :
buffer[i / 3 * 4] = ENCODE_DATA[(data[i] & 0xFC) >> 2];
buf = (data[i] & 0x03) << 4;
if (i + 1 == data.length) {
buffer[i / 3 * 4 + 1] = ENCODE_DATA[buf];
buffer[i / 3 * 4 + 2] = '=';
buffer[i / 3 * 4 + 3] = '=';
}
break;
case 1 :
buf += (data[i] & 0xF0) >> 4;
buffer[i / 3 * 4 + 1] = ENCODE_DATA[buf];
buf = (data[i] & 0x0F) << 2;
if (i + 1 == data.length) {
buffer[i / 3 * 4 + 2] = ENCODE_DATA[buf];
buffer[i / 3 * 4 + 3] = '=';
}
break;
case 2 :
buf += (data[i] & 0xC0) >> 6;
buffer[i / 3 * 4 + 2] = ENCODE_DATA[buf];
buffer[i / 3 * 4 + 3] = ENCODE_DATA[data[i] & 0x3F];
break;
}
}
return new String(buffer);
}
}