001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  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    package org.apache.commons.io;
018    
019    import java.io.IOException;
020    import java.io.OutputStream;
021    
022    /**
023     * Dumps data in hexadecimal format.
024     * <p>
025     * Provides a single function to take an array of bytes and display it
026     * in hexadecimal form.
027     * <p>
028     * Origin of code: POI.
029     *
030     * @author Scott Sanders
031     * @author Marc Johnson
032     * @version $Id: HexDump.java 596667 2007-11-20 13:50:14Z niallp $
033     */
034    public class HexDump {
035    
036        /**
037         * Instances should NOT be constructed in standard programming.
038         */
039        public HexDump() {
040            super();
041        }
042    
043        /**
044         * Dump an array of bytes to an OutputStream.
045         *
046         * @param data  the byte array to be dumped
047         * @param offset  its offset, whatever that might mean
048         * @param stream  the OutputStream to which the data is to be
049         *               written
050         * @param index initial index into the byte array
051         *
052         * @throws IOException is thrown if anything goes wrong writing
053         *         the data to stream
054         * @throws ArrayIndexOutOfBoundsException if the index is
055         *         outside the data array's bounds
056         * @throws IllegalArgumentException if the output stream is null
057         */
058    
059        public static void dump(byte[] data, long offset,
060                                OutputStream stream, int index)
061                throws IOException, ArrayIndexOutOfBoundsException,
062                IllegalArgumentException {
063            
064            if ((index < 0) || (index >= data.length)) {
065                throw new ArrayIndexOutOfBoundsException(
066                        "illegal index: " + index + " into array of length "
067                        + data.length);
068            }
069            if (stream == null) {
070                throw new IllegalArgumentException("cannot write to nullstream");
071            }
072            long display_offset = offset + index;
073            StringBuffer buffer = new StringBuffer(74);
074    
075            for (int j = index; j < data.length; j += 16) {
076                int chars_read = data.length - j;
077    
078                if (chars_read > 16) {
079                    chars_read = 16;
080                }
081                dump(buffer, display_offset).append(' ');
082                for (int k = 0; k < 16; k++) {
083                    if (k < chars_read) {
084                        dump(buffer, data[k + j]);
085                    } else {
086                        buffer.append("  ");
087                    }
088                    buffer.append(' ');
089                }
090                for (int k = 0; k < chars_read; k++) {
091                    if ((data[k + j] >= ' ') && (data[k + j] < 127)) {
092                        buffer.append((char) data[k + j]);
093                    } else {
094                        buffer.append('.');
095                    }
096                }
097                buffer.append(EOL);
098                stream.write(buffer.toString().getBytes());
099                stream.flush();
100                buffer.setLength(0);
101                display_offset += chars_read;
102            }
103        }
104    
105        /**
106         * The line-separator (initializes to "line.separator" system property.
107         */
108        public static final String EOL =
109                System.getProperty("line.separator");
110        private static final char[] _hexcodes =
111                {
112                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
113                    'A', 'B', 'C', 'D', 'E', 'F'
114                };
115        private static final int[] _shifts =
116                {
117                    28, 24, 20, 16, 12, 8, 4, 0
118                };
119    
120        /**
121         * Dump a long value into a StringBuffer.
122         *
123         * @param _lbuffer the StringBuffer to dump the value in
124         * @param value  the long value to be dumped
125         * @return StringBuffer containing the dumped value.
126         */
127        private static StringBuffer dump(StringBuffer _lbuffer, long value) {
128            for (int j = 0; j < 8; j++) {
129                _lbuffer
130                        .append(_hexcodes[((int) (value >> _shifts[j])) & 15]);
131            }
132            return _lbuffer;
133        }
134    
135        /**
136         * Dump a byte value into a StringBuffer.
137         *
138         * @param _cbuffer the StringBuffer to dump the value in
139         * @param value  the byte value to be dumped
140         * @return StringBuffer containing the dumped value.
141         */
142        private static StringBuffer dump(StringBuffer _cbuffer, byte value) {
143            for (int j = 0; j < 2; j++) {
144                _cbuffer.append(_hexcodes[(value >> _shifts[j + 6]) & 15]);
145            }
146            return _cbuffer;
147        }
148    
149    }