1package org.bouncycastle.util.encoders; 2 3import java.io.ByteArrayOutputStream; 4import java.io.IOException; 5import java.io.OutputStream; 6 7import org.bouncycastle.util.Strings; 8 9public class Base64 10{ 11 private static final Encoder encoder = new Base64Encoder(); 12 13 public static String toBase64String( 14 byte[] data) 15 { 16 return toBase64String(data, 0, data.length); 17 } 18 19 public static String toBase64String( 20 byte[] data, 21 int off, 22 int length) 23 { 24 byte[] encoded = encode(data, off, length); 25 return Strings.fromByteArray(encoded); 26 } 27 28 /** 29 * encode the input data producing a base 64 encoded byte array. 30 * 31 * @return a byte array containing the base 64 encoded data. 32 */ 33 public static byte[] encode( 34 byte[] data) 35 { 36 return encode(data, 0, data.length); 37 } 38 39 /** 40 * encode the input data producing a base 64 encoded byte array. 41 * 42 * @return a byte array containing the base 64 encoded data. 43 */ 44 public static byte[] encode( 45 byte[] data, 46 int off, 47 int length) 48 { 49 int len = (length + 2) / 3 * 4; 50 ByteArrayOutputStream bOut = new ByteArrayOutputStream(len); 51 52 try 53 { 54 encoder.encode(data, off, length, bOut); 55 } 56 catch (Exception e) 57 { 58 throw new EncoderException("exception encoding base64 string: " + e.getMessage(), e); 59 } 60 61 return bOut.toByteArray(); 62 } 63 64 /** 65 * Encode the byte data to base 64 writing it to the given output stream. 66 * 67 * @return the number of bytes produced. 68 */ 69 public static int encode( 70 byte[] data, 71 OutputStream out) 72 throws IOException 73 { 74 return encoder.encode(data, 0, data.length, out); 75 } 76 77 /** 78 * Encode the byte data to base 64 writing it to the given output stream. 79 * 80 * @return the number of bytes produced. 81 */ 82 public static int encode( 83 byte[] data, 84 int off, 85 int length, 86 OutputStream out) 87 throws IOException 88 { 89 return encoder.encode(data, off, length, out); 90 } 91 92 /** 93 * decode the base 64 encoded input data. It is assumed the input data is valid. 94 * 95 * @return a byte array representing the decoded data. 96 */ 97 public static byte[] decode( 98 byte[] data) 99 { 100 int len = data.length / 4 * 3; 101 ByteArrayOutputStream bOut = new ByteArrayOutputStream(len); 102 103 try 104 { 105 encoder.decode(data, 0, data.length, bOut); 106 } 107 catch (Exception e) 108 { 109 throw new DecoderException("unable to decode base64 data: " + e.getMessage(), e); 110 } 111 112 return bOut.toByteArray(); 113 } 114 115 /** 116 * decode the base 64 encoded String data - whitespace will be ignored. 117 * 118 * @return a byte array representing the decoded data. 119 */ 120 public static byte[] decode( 121 String data) 122 { 123 int len = data.length() / 4 * 3; 124 ByteArrayOutputStream bOut = new ByteArrayOutputStream(len); 125 126 try 127 { 128 encoder.decode(data, bOut); 129 } 130 catch (Exception e) 131 { 132 throw new DecoderException("unable to decode base64 string: " + e.getMessage(), e); 133 } 134 135 return bOut.toByteArray(); 136 } 137 138 /** 139 * decode the base 64 encoded String data writing it to the given output stream, 140 * whitespace characters will be ignored. 141 * 142 * @return the number of bytes produced. 143 */ 144 public static int decode( 145 String data, 146 OutputStream out) 147 throws IOException 148 { 149 return encoder.decode(data, out); 150 } 151} 152