Here you can find the source of dumpAsHex(byte[] byteBuffer, int length)
Parameter | Description |
---|---|
byteBuffer | the data to print as hex |
length | the number of bytes to print |
public static String dumpAsHex(byte[] byteBuffer, int length)
//package com.java2s; /*/*w w w . j av a 2 s . c o m*/ Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved. The MySQL Connector/J is licensed under the terms of the GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors. There are special exceptions to the terms and conditions of the GPLv2 as it is applied to this software, see the FLOSS License Exception <http://www.mysql.com/about/legal/licensing/foss-exception.html>. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.IllegalCharsetNameException; import java.nio.charset.UnsupportedCharsetException; import java.util.concurrent.ConcurrentHashMap; public class Main { private static final ConcurrentHashMap<String, Charset> charsetsByAlias = new ConcurrentHashMap<String, Charset>(); private static final String platformEncoding = System .getProperty("file.encoding"); /** * Dumps the given bytes to STDOUT as a hex dump (up to length bytes). * * @param byteBuffer * the data to print as hex * @param length * the number of bytes to print * * @return ... */ public static String dumpAsHex(byte[] byteBuffer, int length) { StringBuilder outputBuilder = new StringBuilder(length * 4); int p = 0; int rows = length / 8; for (int i = 0; (i < rows) && (p < length); i++) { int ptemp = p; for (int j = 0; j < 8; j++) { String hexVal = Integer .toHexString(byteBuffer[ptemp] & 0xff); if (hexVal.length() == 1) { hexVal = "0" + hexVal; } outputBuilder.append(hexVal + " "); ptemp++; } outputBuilder.append(" "); for (int j = 0; j < 8; j++) { int b = 0xff & byteBuffer[p]; if (b > 32 && b < 127) { outputBuilder.append((char) b + " "); } else { outputBuilder.append(". "); } p++; } outputBuilder.append("\n"); } int n = 0; for (int i = p; i < length; i++) { String hexVal = Integer.toHexString(byteBuffer[i] & 0xff); if (hexVal.length() == 1) { hexVal = "0" + hexVal; } outputBuilder.append(hexVal + " "); n++; } for (int i = n; i < 8; i++) { outputBuilder.append(" "); } outputBuilder.append(" "); for (int i = p; i < length; i++) { int b = 0xff & byteBuffer[i]; if (b > 32 && b < 127) { outputBuilder.append((char) b + " "); } else { outputBuilder.append(". "); } } outputBuilder.append("\n"); return outputBuilder.toString(); } public static String toString(byte[] value, int offset, int length, String encoding) throws UnsupportedEncodingException { Charset cs = findCharset(encoding); return cs.decode(ByteBuffer.wrap(value, offset, length)).toString(); } public static String toString(byte[] value, String encoding) throws UnsupportedEncodingException { Charset cs = findCharset(encoding); return cs.decode(ByteBuffer.wrap(value)).toString(); } public static String toString(byte[] value, int offset, int length) { try { Charset cs = findCharset(platformEncoding); return cs.decode(ByteBuffer.wrap(value, offset, length)) .toString(); } catch (UnsupportedEncodingException e) { // can't happen, emulating new String(byte[]) } return null; } public static String toString(byte[] value) { try { Charset cs = findCharset(platformEncoding); return cs.decode(ByteBuffer.wrap(value)).toString(); } catch (UnsupportedEncodingException e) { // can't happen, emulating new String(byte[]) } return null; } static Charset findCharset(String alias) throws UnsupportedEncodingException { try { Charset cs = charsetsByAlias.get(alias); if (cs == null) { cs = Charset.forName(alias); charsetsByAlias.putIfAbsent(alias, cs); } return cs; // We re-throw these runtimes for compatibility with java.io } catch (UnsupportedCharsetException uce) { throw new UnsupportedEncodingException(alias); } catch (IllegalCharsetNameException icne) { throw new UnsupportedEncodingException(alias); } catch (IllegalArgumentException iae) { throw new UnsupportedEncodingException(alias); } } }