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