Java ByteBuffer Print printData(ByteBuffer buf, int offset, int len)

Here you can find the source of printData(ByteBuffer buf, int offset, int len)

Description

print Data

License

Open Source License

Declaration

public static String printData(ByteBuffer buf, int offset, int len) 

Method Source Code

//package com.java2s;
/*/*from w w  w .j  a  v  a 2s.c  om*/
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.nio.ByteBuffer;

public class Main {
    public static String printData(ByteBuffer buf, int offset, int len) {
        byte[] tmp = new byte[len];
        int pos = buf.position();
        buf.position(offset);
        buf.get(tmp);
        buf.position(pos);
        return printData(tmp, len);
    }

    /**
     * This method is equivalent to <TT>printData(raw, raw.length)</TT>.
     * 
     * @param raw a byte array
     * @return converted byte array
     * @see #printData(byte[], int)
     */
    public static String printData(byte[] raw) {
        return printData(raw, raw.length);
    }

    /**
     * Converts a byte array to string in a special form. <BR>
     * <BR>
     * On the left side of the generated string, each byte is printed as a hex octet with a trailing
     * space.<BR>
     * On the right side of the generated string, each byte is printed as an ASCII char, unless it's
     * a non-printing character (the same is done to extended ASCII characters): then a period is
     * printed instead.
     * 
     * @param data a byte array
     * @param len number of bytes to print
     * @return converted byte array
     * @see #printData(byte[], int)
     */
    public static String printData(byte[] data, int len) {
        String eol = System.getProperty("line.separator", "\r\n");
        final StringBuilder result = new StringBuilder(eol);

        int counter = 0;

        for (int i = 0; i < len; i++) {
            if (counter % 16 == 0) {
                result.append(fillHex(i, 4));
                result.append(": ");
            }

            result.append(fillHex(data[i] & 0xff, 2));
            result.append(' ');
            counter++;
            if (counter == 16) {
                result.append("   ");

                int charpoint = i - 15;
                for (int a = 0; a < 16; a++) {
                    int t1 = data[charpoint++];
                    if (t1 > 0x1f && t1 < 0x80) {
                        result.append((char) t1);
                    } else {
                        result.append('.');
                    }
                }

                result.append(eol);
                counter = 0;
            }
        }

        int rest = data.length % 16;
        if (rest > 0) {
            for (int i = 0; i < 17 - rest; i++) {
                result.append("   ");
            }

            int charpoint = data.length - rest;
            for (int a = 0; a < rest; a++) {
                int t1 = data[charpoint++];
                if (t1 > 0x1f && t1 < 0x80) {
                    result.append((char) t1);
                } else {
                    result.append('.');
                }
            }

            result.append(eol);
        }

        return result.toString();
    }

    /**
     * Converts a number to hexadecimal format and adds leading zeros if necessary.
     * 
     * @param data a number
     * @param digits minimum hexadecimal digit count
     * @return given number in hexadecimal format
     */
    public static String fillHex(int data, int digits) {
        String hex = Integer.toHexString(data);

        StringBuilder number = new StringBuilder(Math.max(hex.length(), digits));
        for (int i = hex.length(); i < digits; i++)
            number.append(0);
        number.append(hex);

        return number.toString();
    }
}

Related

  1. print(ByteBuffer byteBuffer)
  2. printBuffer(ByteBuffer buffer)
  3. printBuffer(ByteBuffer buffer)
  4. printBuffer(String msg, ByteBuffer buffer)
  5. printByteBuffer(ByteBuffer buf)
  6. printData(ByteBuffer buffer, int len)
  7. printImageContentsSubset(ByteBuffer buf, int w, int h)
  8. println(String string, ByteBuffer buffer)
  9. printTraceHexData(PrintWriter trc, ByteBuffer buf, int startPos, int endPos)