Here you can find the source of dumpBytes(PrintStream ps, ByteBuffer buf)
Parameter | Description |
---|---|
ps | a parameter |
buf | a parameter |
public static void dumpBytes(PrintStream ps, ByteBuffer buf)
//package com.java2s; /*//from w w w . ja va 2 s . co m * Copyright 2015 bladesilent@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import java.io.PrintStream; import java.nio.ByteBuffer; public class Main { public static final int DUMP_BYTES_PER_GROUP = 4; public static final int DUMP_GROUPS_PER_LINE = 8; /** * Dup dat into binary format like * * 00010203 04050607 08090001 02030405 06070809 00010203 04050607 08090001 * 08090001 02030405 06070809 00010203 04050607 08090001 02030405 06070809 * 06070809 00010203 04050607 08090001 02030405 ... * * @param ps * @param buf */ public static void dumpBytes(PrintStream ps, ByteBuffer buf) { dumpBytes(ps, buf.array()); } public static void dumpBytes(PrintStream ps, byte[] buf) { if (buf != null) { int bytesPerLine = DUMP_GROUPS_PER_LINE * DUMP_BYTES_PER_GROUP; for (int idx = 0, cnt = 1; idx < buf.length; ++cnt) { if (cnt != 0 && cnt % (DUMP_BYTES_PER_GROUP + 1) == 0) ps.print(" "); else ps.print(String.format("%02x", buf[idx++])); // check if time to break a line if (idx % bytesPerLine == 0) { ps.print('\n'); cnt = 0; } } ps.print("\n"); } } public static void dumpBytes(PrintStream ps, byte b) { ps.print(String.format("%02x", b)); } public static void dumpBytes(PrintStream ps, int i) { dumpBytes(ps, ByteBuffer.allocate(4).putInt(i)); } public static void dumpBytes(PrintStream ps, long l) { dumpBytes(ps, ByteBuffer.allocate(8).putLong(l)); } public static void dumpBytes(PrintStream ps, short s) { dumpBytes(ps, ByteBuffer.allocate(2).putShort(s)); } public static void dumpBytes(PrintStream ps, String str) { dumpBytes(ps, str.getBytes()); } }