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 public class Hex 027 { 028 private static final Encoder encoder = new HexEncoder(); 029 030 /** 031 * encode the input data producing a Hex encoded byte array. 032 * 033 * @return a byte array containing the Hex encoded data. 034 */ 035 public static byte[] encode( 036 byte[] data) 037 { 038 return encode(data, 0, data.length); 039 } 040 041 /** 042 * encode the input data producing a Hex encoded byte array. 043 * 044 * @return a byte array containing the Hex encoded data. 045 */ 046 public static byte[] encode( 047 byte[] data, 048 int off, 049 int length) 050 { 051 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 052 053 try 054 { 055 encoder.encode(data, off, length, bOut); 056 } 057 catch (IOException e) 058 { 059 throw new RuntimeException("exception encoding Hex string: " + e); 060 } 061 062 return bOut.toByteArray(); 063 } 064 065 /** 066 * Hex encode the byte data writing it to the given output stream. 067 * 068 * @return the number of bytes produced. 069 */ 070 public static int encode( 071 byte[] data, 072 OutputStream out) 073 throws IOException 074 { 075 return encoder.encode(data, 0, data.length, out); 076 } 077 078 /** 079 * Hex encode the byte data writing it to the given output stream. 080 * 081 * @return the number of bytes produced. 082 */ 083 public static int encode( 084 byte[] data, 085 int off, 086 int length, 087 OutputStream out) 088 throws IOException 089 { 090 return encoder.encode(data, 0, data.length, out); 091 } 092 093 /** 094 * decode the Hex encoded input data. It is assumed the input data is valid. 095 * 096 * @return a byte array representing the decoded data. 097 */ 098 public static byte[] decode( 099 byte[] data) 100 { 101 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 102 103 try 104 { 105 encoder.decode(data, 0, data.length, bOut); 106 } 107 catch (IOException e) 108 { 109 throw new RuntimeException("exception decoding Hex string: " + e); 110 } 111 112 return bOut.toByteArray(); 113 } 114 115 /** 116 * decode the Hex 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 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 124 125 try 126 { 127 encoder.decode(data, bOut); 128 } 129 catch (IOException e) 130 { 131 throw new RuntimeException("exception decoding Hex string: " + e); 132 } 133 134 return bOut.toByteArray(); 135 } 136 137 /** 138 * decode the Hex encoded String data writing it to the given output stream, 139 * whitespace characters will be ignored. 140 * 141 * @return the number of bytes produced. 142 */ 143 public static int decode( 144 String data, 145 OutputStream out) 146 throws IOException 147 { 148 return encoder.decode(data, out); 149 } 150 }