Writes a chunk of data to a file in pretty printed hexidecimal. - Java java.lang

Java examples for java.lang:Hex

Description

Writes a chunk of data to a file in pretty printed hexidecimal.

Demo Code

/*//w  ww .  ja  v a 2  s .  c  om
Copyright (c) 2005 Health Market Science, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
USA

You can contact Health Market Science at info@healthmarketscience.com
or at the following address:

Health Market Science
2700 Horizon Drive
Suite 200
King of Prussia, PA 19406
 */
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

public class Main{
    private static final String[] HEX_CHARS = new String[] { "0", "1", "2",
            "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
    private static final int NUM_BYTES_PER_BLOCK = 4;
    private static final int NUM_BYTES_PER_LINE = 24;
    /**
     * Writes a chunk of data to a file in pretty printed hexidecimal.
     */
    public static void toHexFile(String fileName, ByteBuffer buffer,
            int offset, int size) throws IOException {
        PrintWriter writer = new PrintWriter(new FileWriter(fileName));
        try {
            writer.println(toHexString(buffer, offset, size));
        } finally {
            writer.close();
        }
    }
    /**
     * Convert a byte buffer to a hexadecimal string for display
     * @param buffer Buffer to display, starting at offset 0
     * @param size Number of bytes to read from the buffer
     * @return The display String
     */
    public static String toHexString(ByteBuffer buffer, int size) {
        return toHexString(buffer, 0, size);
    }
    /**
     * Convert a byte array to a hexadecimal string for display
     * @param array byte array to display, starting at offset 0
     * @return The display String
     */
    public static String toHexString(byte[] array) {
        return toHexString(ByteBuffer.wrap(array), 0, array.length);
    }
    /**
     * Convert a byte buffer to a hexadecimal string for display
     * @param buffer Buffer to display, starting at offset 0
     * @param offset Offset at which to start reading the buffer
     * @param size Number of bytes to read from the buffer
     * @return The display String
     */
    public static String toHexString(ByteBuffer buffer, int offset, int size) {
        return toHexString(buffer, offset, size, true);
    }
    /**
     * Convert a byte buffer to a hexadecimal string for display
     * @param buffer Buffer to display, starting at offset 0
     * @param offset Offset at which to start reading the buffer
     * @param size Number of bytes to read from the buffer
     * @param formatted flag indicating if formatting is required
     * @return The display String
     */
    public static String toHexString(ByteBuffer buffer, int offset,
            int size, boolean formatted) {

        StringBuilder rtn = new StringBuilder();
        int position = buffer.position();
        buffer.position(offset);

        for (int i = 0; i < size; i++) {
            byte b = buffer.get();
            byte h = (byte) (b & 0xF0);
            h = (byte) (h >>> 4);
            h = (byte) (h & 0x0F);
            rtn.append(HEX_CHARS[h]);
            h = (byte) (b & 0x0F);
            rtn.append(HEX_CHARS[h]);

            int next = (i + 1);
            if (formatted && (next < size)) {
                if ((next % NUM_BYTES_PER_LINE) == 0) {

                    rtn.append("\n");

                } else {

                    rtn.append(" ");

                    if ((next % NUM_BYTES_PER_BLOCK) == 0) {
                        rtn.append(" ");
                    }
                }
            }
        }

        buffer.position(position);
        return rtn.toString();
    }
    /**
     * Convert the given number of bytes from the given database page to a
     * hexidecimal string for display.
     */
    public static String toHexString(DatabaseImpl db, int pageNumber,
            int size) throws IOException {
        ByteBuffer buffer = db.getPageChannel().createPageBuffer();
        db.getPageChannel().readPage(buffer, pageNumber);
        return toHexString(buffer, size);
    }
}

Related Tutorials