Java ByteBuffer Dump dumpNextNBytes(ByteBuffer buffer, int n)

Here you can find the source of dumpNextNBytes(ByteBuffer buffer, int n)

Description

dump Next N Bytes

License

Open Source License

Declaration

public static void dumpNextNBytes(ByteBuffer buffer, int n) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.PrintWriter;
import java.nio.ByteBuffer;

public class Main {
    public static PrintWriter debugLog = null;

    public static void dumpNextNBytes(ByteBuffer buffer, int n) {
        if (!debugging())
            return;

        int oldPosition = buffer.position();

        byte[] bytesToRead = new byte[Math.min(buffer.remaining(), n)];

        buffer.get(bytesToRead);/*www .jav  a2s. c  o m*/
        log(Integer.toHexString(oldPosition) + "  "
                + getHexString(bytesToRead));
        buffer.position(oldPosition);
    }

    public static boolean debugging() {
        return debugLog != null;
    }

    public static void log(String line) {
        if (debugLog != null)
            debugLog.println(line);
    }

    public static String getHexString(byte[] data) {
        StringBuilder result = new StringBuilder();

        for (byte b : data) {
            result.append(String.format("%02X", b));
        }

        return result.toString();
    }
}

Related

  1. dumpBoolean(ByteBuffer itemBuffer, StringBuilder sb, String tag)
  2. dumpBytes(ByteBuffer bbuf)
  3. dumpBytes(ByteBuffer byteBuffer)
  4. dumpBytes(PrintStream ps, ByteBuffer buf)
  5. dumpHexString(ByteBuffer bb)
  6. dumpToFile(ByteBuffer buf, String fileName)
  7. hexDump(ByteBuffer buffer)
  8. hexDump(PrintStream ps, ByteBuffer bb)
  9. printByteDump(StringBuilder strBuilder, ByteBuffer data, int offset, int length, int columnNum)