1 /** 2 * 3 * Copyright 2003-2004 The Apache Software Foundation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.geronimo.mail.util; 19 20 import java.io.ByteArrayOutputStream; 21 import java.io.IOException; 22 import java.io.OutputStream; 23 24 public class Hex 25 { 26 private static final Encoder encoder = new HexEncoder(); 27 28 /** 29 * encode the input data producing a Hex encoded byte array. 30 * 31 * @return a byte array containing the Hex 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 Hex encoded byte array. 41 * 42 * @return a byte array containing the Hex encoded data. 43 */ 44 public static byte[] encode( 45 byte[] data, 46 int off, 47 int length) 48 { 49 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 50 51 try 52 { 53 encoder.encode(data, off, length, bOut); 54 } 55 catch (IOException e) 56 { 57 throw new RuntimeException("exception encoding Hex string: " + e); 58 } 59 60 return bOut.toByteArray(); 61 } 62 63 /** 64 * Hex encode the byte data writing it to the given output stream. 65 * 66 * @return the number of bytes produced. 67 */ 68 public static int encode( 69 byte[] data, 70 OutputStream out) 71 throws IOException 72 { 73 return encoder.encode(data, 0, data.length, out); 74 } 75 76 /** 77 * Hex encode the byte data writing it to the given output stream. 78 * 79 * @return the number of bytes produced. 80 */ 81 public static int encode( 82 byte[] data, 83 int off, 84 int length, 85 OutputStream out) 86 throws IOException 87 { 88 return encoder.encode(data, 0, data.length, out); 89 } 90 91 /** 92 * decode the Hex encoded input data. It is assumed the input data is valid. 93 * 94 * @return a byte array representing the decoded data. 95 */ 96 public static byte[] decode( 97 byte[] data) 98 { 99 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 100 101 try 102 { 103 encoder.decode(data, 0, data.length, bOut); 104 } 105 catch (IOException e) 106 { 107 throw new RuntimeException("exception decoding Hex string: " + e); 108 } 109 110 return bOut.toByteArray(); 111 } 112 113 /** 114 * decode the Hex encoded String data - whitespace will be ignored. 115 * 116 * @return a byte array representing the decoded data. 117 */ 118 public static byte[] decode( 119 String data) 120 { 121 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 122 123 try 124 { 125 encoder.decode(data, bOut); 126 } 127 catch (IOException e) 128 { 129 throw new RuntimeException("exception decoding Hex string: " + e); 130 } 131 132 return bOut.toByteArray(); 133 } 134 135 /** 136 * decode the Hex encoded String data writing it to the given output stream, 137 * whitespace characters will be ignored. 138 * 139 * @return the number of bytes produced. 140 */ 141 public static int decode( 142 String data, 143 OutputStream out) 144 throws IOException 145 { 146 return encoder.decode(data, out); 147 } 148 }