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