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