001 /** 002 * 003 * Copyright 2003-2004 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.geronimo.mail.util; 019 020 import java.io.ByteArrayOutputStream; 021 import java.io.IOException; 022 import java.io.OutputStream; 023 024 public class Base64 025 { 026 private static final Encoder encoder = new Base64Encoder(); 027 028 /** 029 * encode the input data producing a base 64 encoded byte array. 030 * 031 * @return a byte array containing the base 64 encoded data. 032 */ 033 public static byte[] encode( 034 byte[] data) 035 { 036 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 037 038 try 039 { 040 encoder.encode(data, 0, data.length, bOut); 041 } 042 catch (IOException e) 043 { 044 throw new RuntimeException("exception encoding base64 string: " + e); 045 } 046 047 return bOut.toByteArray(); 048 } 049 050 /** 051 * Encode the byte data to base 64 writing it to the given output stream. 052 * 053 * @return the number of bytes produced. 054 */ 055 public static int encode( 056 byte[] data, 057 OutputStream out) 058 throws IOException 059 { 060 return encoder.encode(data, 0, data.length, out); 061 } 062 063 /** 064 * Encode the byte data to base 64 writing it to the given output stream. 065 * 066 * @return the number of bytes produced. 067 */ 068 public static int encode( 069 byte[] data, 070 int off, 071 int length, 072 OutputStream out) 073 throws IOException 074 { 075 return encoder.encode(data, off, length, out); 076 } 077 078 /** 079 * decode the base 64 encoded input data. It is assumed the input data is valid. 080 * 081 * @return a byte array representing the decoded data. 082 */ 083 public static byte[] decode( 084 byte[] data) 085 { 086 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 087 088 try 089 { 090 encoder.decode(data, 0, data.length, bOut); 091 } 092 catch (IOException e) 093 { 094 throw new RuntimeException("exception decoding base64 string: " + e); 095 } 096 097 return bOut.toByteArray(); 098 } 099 100 /** 101 * decode the base 64 encoded String data - whitespace will be ignored. 102 * 103 * @return a byte array representing the decoded data. 104 */ 105 public static byte[] decode( 106 String data) 107 { 108 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 109 110 try 111 { 112 encoder.decode(data, bOut); 113 } 114 catch (IOException e) 115 { 116 throw new RuntimeException("exception decoding base64 string: " + e); 117 } 118 119 return bOut.toByteArray(); 120 } 121 122 /** 123 * decode the base 64 encoded String data writing it to the given output stream, 124 * whitespace characters will be ignored. 125 * 126 * @return the number of bytes produced. 127 */ 128 public static int decode( 129 String data, 130 OutputStream out) 131 throws IOException 132 { 133 return encoder.decode(data, out); 134 } 135 136 /** 137 * decode the base 64 encoded String data writing it to the given output stream, 138 * whitespace characters will be ignored. 139 * 140 * @param data The array data to decode. 141 * @param out The output stream for the data. 142 * 143 * @return the number of bytes produced. 144 * @exception IOException 145 */ 146 public static int decode(byte [] data, OutputStream out) throws IOException 147 { 148 return encoder.decode(data, 0, data.length, out); 149 } 150 }