Java ByteBuffer Dump printByteDump(StringBuilder strBuilder, ByteBuffer data, int offset, int length, int columnNum)

Here you can find the source of printByteDump(StringBuilder strBuilder, ByteBuffer data, int offset, int length, int columnNum)

Description

print Byte Dump

License

Open Source License

Declaration

public static void printByteDump(StringBuilder strBuilder, ByteBuffer data, int offset, int length,
            int columnNum) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.//from w w w  .j  av a  2s .  c  o  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/

import java.nio.ByteBuffer;

public class Main {
    /**
     * writes message data to a buffer in hex format
     */
    public static void printByteDump(StringBuilder strBuilder, byte[] data, int offset, int length, int columnNum) {
        printByteDump(strBuilder, data, offset, length, columnNum, true);
    }

    /**
     * writes message data to a buffer in hex format
     */
    public static void printByteDump(StringBuilder strBuilder, byte[] data, int offset, int length, int columnNum,
            boolean hex) {
        int columnCount = 0;
        final int endIndex = offset + length;
        for (int i = offset; i < endIndex; i++) {
            if (columnCount == columnNum) {
                strBuilder.append('\n');
                columnCount = 0;
            }
            if (hex) {
                strBuilder.append(String.format("%02x ", data[i]));
            } else {

                strBuilder.append(data[i]).append(' ');
            }
            columnCount++;
        }
        strBuilder.append('\n');
    }

    public static void printByteDump(StringBuilder strBuilder, ByteBuffer data, int offset, int length,
            int columnNum) {
        int currentPosition = data.position();
        //      data.position(offset);
        int columnCount = 0;
        final int endIndex = offset + length;
        for (int i = offset; i < endIndex; i++) {
            if (columnCount == columnNum) {
                strBuilder.append('\n');
                columnCount = 0;
            }
            strBuilder.append(String.format("%02x ", data.get(i)));
            columnCount++;
        }
        strBuilder.append('\n');

        data.position(currentPosition);
    }
}

Related

  1. dumpHexString(ByteBuffer bb)
  2. dumpNextNBytes(ByteBuffer buffer, int n)
  3. dumpToFile(ByteBuffer buf, String fileName)
  4. hexDump(ByteBuffer buffer)
  5. hexDump(PrintStream ps, ByteBuffer bb)
  6. setDecreasedBuffer(ByteBuffer memoryDumpReader, long baseAddressValue, int innerPointerOffset, long memoryDumpStartingOffset)
  7. toHexDumpString(ByteBuffer data)