/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (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.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is part of dcm4che, an implementation of DICOM(TM) in * Java(TM), hosted at https://github.com/gunterze/dcm4che. * * The Initial Developer of the Original Code is * Agfa Healthcare. * Portions created by the Initial Developer are Copyright (C) 2011 * the Initial Developer. All Rights Reserved. * * Contributor(s): * See @authors listed below * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ package org.dcm4che3.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; /** * @author Gunter Zeilinger <gunterze@gmail.com> */ public class Base64 { private static final char[] BASE64 = { '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', '+', '/' }; private static final byte INV_BASE64[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 }; public static void encode(byte[] src, int srcPos, int srcLen, char[] dest, int destPos) { if (srcPos < 0 || srcLen < 0 || srcLen > src.length - srcPos) throw new IndexOutOfBoundsException(); int destLen = (srcLen * 4 / 3 + 3) & ~3; if (destPos < 0 || destLen > dest.length - destPos) throw new IndexOutOfBoundsException(); byte b1, b2, b3; int n = srcLen / 3; int r = srcLen - 3 * n; while (n-- > 0) { dest[destPos++] = BASE64[((b1 = src[srcPos++]) >>> 2) & 0x3F]; dest[destPos++] = BASE64[((b1 & 0x03) << 4) | (((b2 = src[srcPos++]) >>> 4) & 0x0F)]; dest[destPos++] = BASE64[((b2 & 0x0F) << 2) | (((b3 = src[srcPos++]) >>> 6) & 0x03)]; dest[destPos++] = BASE64[b3 & 0x3F]; } if (r > 0) if (r == 1) { dest[destPos++] = BASE64[((b1 = src[srcPos]) >>> 2) & 0x3F]; dest[destPos++] = BASE64[((b1 & 0x03) << 4)]; dest[destPos++] = '='; dest[destPos++] = '='; } else { dest[destPos++] = BASE64[((b1 = src[srcPos++]) >>> 2) & 0x3F]; dest[destPos++] = BASE64[((b1 & 0x03) << 4) | (((b2 = src[srcPos]) >>> 4) & 0x0F)]; dest[destPos++] = BASE64[(b2 & 0x0F) << 2]; dest[destPos++] = '='; } } public static void decode(char[] ch, int off, int len, OutputStream out) throws IOException { byte b2, b3; while ((len -= 2) >= 0) { out.write((byte)((INV_BASE64[ch[off++]] << 2) | ((b2 = INV_BASE64[ch[off++]]) >>> 4))); if ((len-- == 0) || ch[off] == '=') break; out.write((byte)((b2 << 4) | ((b3 = INV_BASE64[ch[off++]]) >>> 2))); if ((len-- == 0) || ch[off] == '=') break; out.write((byte)((b3 << 6) | INV_BASE64[ch[off++]])); } } /** * Convenience method. To achieve best performance, use Base64.encode * @param bytes data to encode * @return Base64-encoded string */ public static String toBase64(byte[] bytes) { final char[] encodedChars = new char[(bytes.length * 4 / 3 + 3) & ~3]; encode(bytes, 0, bytes.length, encodedChars, 0); return new String(encodedChars); } /** * Convenience method. To achieve best performance, use Base64.decode * @param base64String encoded string * @return Decoded data * @throws IOException */ public static byte[] fromBase64(String base64String) throws IOException { final int encodedLength = base64String.length(); final int decodedLength = encodedLength * 3 / 4; final char[] encodedChars = new char[encodedLength]; base64String.getChars(0, encodedLength, encodedChars, 0); final ByteArrayOutputStream stream = new ByteArrayOutputStream(decodedLength); decode(encodedChars,0, encodedLength,stream); return stream.toByteArray(); } }