Java ByteBuffer Dump dumpBytes(PrintStream ps, ByteBuffer buf)

Here you can find the source of dumpBytes(PrintStream ps, ByteBuffer buf)

Description

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 ...

License

Apache License

Parameter

Parameter Description
ps a parameter
buf a parameter

Declaration

public static void dumpBytes(PrintStream ps, ByteBuffer buf) 

Method Source Code

//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());
    }
}

Related

  1. dump(ByteBuffer buffer, OutputStream stream)
  2. dumpAsHex(byte[] byteBuffer, int length)
  3. dumpBoolean(ByteBuffer itemBuffer, StringBuilder sb, String tag)
  4. dumpBytes(ByteBuffer bbuf)
  5. dumpBytes(ByteBuffer byteBuffer)
  6. dumpHexString(ByteBuffer bb)
  7. dumpNextNBytes(ByteBuffer buffer, int n)
  8. dumpToFile(ByteBuffer buf, String fileName)
  9. hexDump(ByteBuffer buffer)