Prints a hexadecimal representation of a buffer to standard out. - Java java.nio

Java examples for java.nio:IntBuffer

Description

Prints a hexadecimal representation of a buffer to standard out.

Demo Code


//package com.java2s;
import java.nio.ByteBuffer;

public class Main {
    /**/*  w w  w . jav  a  2s  .c  o  m*/
     * Prints a hexadecimal representation of a buffer to standard out.
     *
     * @param buffer
     */
    public static void printByteBuffer(ByteBuffer buffer) {

        System.out.println("Position: " + buffer.position() + " Limit: "
                + buffer.limit() + " Capacity: " + buffer.capacity());

        int count = 0;
        while (count < buffer.capacity()) {
            if (count % 16 == 15) {
                System.out.println(String.format("%02x",
                        buffer.get(count) & 0xff));
            } else if (count % 16 == 7) {
                System.out.print(String.format("%02x",
                        buffer.get(count) & 0xff) + "  ");
            } else {
                System.out.print(String.format("%02x",
                        buffer.get(count) & 0xff) + " ");
            }
            count++;
        }
        if (count % 16 != 15) {
            System.out.println();
        }
    }
}

Related Tutorials