001    /**
002     *
003     * Copyright 2003-2006 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.mail.util;
019    
020    import java.io.ByteArrayOutputStream;
021    import java.io.IOException;
022    import java.io.OutputStream;
023    
024    public class Hex
025    {
026        private static final Encoder encoder = new HexEncoder();
027    
028        /**
029         * encode the input data producing a Hex encoded byte array.
030         *
031         * @return a byte array containing the Hex encoded data.
032         */
033        public static byte[] encode(
034            byte[]    data)
035        {
036            return encode(data, 0, data.length);
037        }
038    
039        /**
040         * encode the input data producing a Hex encoded byte array.
041         *
042         * @return a byte array containing the Hex encoded data.
043         */
044        public static byte[] encode(
045            byte[]    data,
046            int       off,
047            int       length)
048        {
049            ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
050    
051            try
052            {
053                encoder.encode(data, off, length, bOut);
054            }
055            catch (IOException e)
056            {
057                throw new RuntimeException("exception encoding Hex string: " + e);
058            }
059    
060            return bOut.toByteArray();
061        }
062    
063        /**
064         * Hex encode the byte data writing it to the given output stream.
065         *
066         * @return the number of bytes produced.
067         */
068        public static int encode(
069            byte[]         data,
070            OutputStream   out)
071            throws IOException
072        {
073            return encoder.encode(data, 0, data.length, out);
074        }
075    
076        /**
077         * Hex encode the byte data writing it to the given output stream.
078         *
079         * @return the number of bytes produced.
080         */
081        public static int encode(
082            byte[]         data,
083            int            off,
084            int            length,
085            OutputStream   out)
086            throws IOException
087        {
088            return encoder.encode(data, 0, data.length, out);
089        }
090    
091        /**
092         * decode the Hex encoded input data. It is assumed the input data is valid.
093         *
094         * @return a byte array representing the decoded data.
095         */
096        public static byte[] decode(
097            byte[]    data)
098        {
099            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    }