Here you can find the source of fileHexDump(String fileName, byte[] data)
Parameter | Description |
---|---|
fileName | output file name |
data | target data |
public static final void fileHexDump(String fileName, byte[] data)
//package com.java2s; /*/*w ww.ja v a 2 s .c om*/ * 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. */ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.text.DecimalFormat; public class Main { /** * Constants used to convert bytes to hex digits. */ private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; /** * Writes a hex dump to a file for a large byte array. * * @param fileName output file name * @param data target data */ public static final void fileHexDump(String fileName, byte[] data) { try { FileOutputStream os = new FileOutputStream(fileName); os.write(hexdump(data, true, 16, "").getBytes()); os.close(); } catch (IOException ex) { ex.printStackTrace(); } } /** * Writes a hex dump to a file from a POI input stream. * Note that this assumes that the complete size of the data in * the stream is returned by the available() method. * * @param fileName output file name * @param is input stream */ public static final void fileHexDump(String fileName, InputStream is) { try { byte[] data = new byte[is.available()]; is.read(data); fileHexDump(fileName, data); } catch (IOException ex) { ex.printStackTrace(); } } /** * This method generates a formatted version of the data contained * in a byte array. The data is written both in hex, and as ASCII * characters. * * @param buffer data to be displayed * @param offset offset of start of data to be displayed * @param length length of data to be displayed * @param ascii flag indicating whether ASCII equivalent chars should also be displayed * @return formatted string */ public static final String hexdump(byte[] buffer, int offset, int length, boolean ascii) { StringBuffer sb = new StringBuffer(); if (buffer != null) { char c; int loop; int count = offset + length; for (loop = offset; loop < count; loop++) { sb.append(" "); sb.append(HEX_DIGITS[(buffer[loop] & 0xF0) >> 4]); sb.append(HEX_DIGITS[buffer[loop] & 0x0F]); } if (ascii == true) { sb.append(" "); for (loop = offset; loop < count; loop++) { c = (char) buffer[loop]; if ((c > 200) || (c < 27)) { c = ' '; } sb.append(c); } } } return (sb.toString()); } /** * This method generates a formatted version of the data contained * in a byte array. The data is written both in hex, and as ASCII * characters. * * @param buffer data to be displayed * @param ascii flag indicating whether ASCII equivalent chars should also be displayed * @return formatted string */ public static final String hexdump(byte[] buffer, boolean ascii) { int length = 0; if (buffer != null) { length = buffer.length; } return (hexdump(buffer, 0, length, ascii)); } /** * This method generates a formatted version of the data contained * in a byte array. The data is written both in hex, and as ASCII * characters. The data is organised into fixed width columns. * * @param buffer data to be displayed * @param ascii flag indicating whether ASCII equivalent chars should also be displayed * @param columns number of columns * @param prefix prefix to be added before the start of the data * @return formatted string */ public static final String hexdump(byte[] buffer, boolean ascii, int columns, String prefix) { StringBuffer sb = new StringBuffer(); if (buffer != null) { int index = 0; DecimalFormat df = new DecimalFormat("00000"); while (index < buffer.length) { if (index + columns > buffer.length) { columns = buffer.length - index; } sb.append(prefix); sb.append(df.format(index)); sb.append(":"); sb.append(hexdump(buffer, index, columns, ascii)); sb.append('\n'); index += columns; } } return (sb.toString()); } }