Java ByteBuffer Dump dumpBytes(ByteBuffer byteBuffer)

Here you can find the source of dumpBytes(ByteBuffer byteBuffer)

Description

Convert the byteBuffer into a String to be used for logging and debugging.

License

Open Source License

Parameter

Parameter Description
byteBuffer the byteBuffer to be dumped

Return

the String representation

Declaration

public static String dumpBytes(ByteBuffer byteBuffer) 

Method Source Code

//package com.java2s;
/*/*from  w w w  .j  a va2 s.  c o  m*/
 *  Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
 *
 *  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; version 2 of the License.
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */

import java.nio.ByteBuffer;

public class Main {
    /** Convert the byte[] into a String to be used for logging and debugging.
     * 
     * @param bytes the byte[] to be dumped
     * @return the String representation
     */
    public static String dumpBytes(byte[] bytes) {
        StringBuffer buffer = new StringBuffer("byte[");
        buffer.append(bytes.length);
        buffer.append("]: [");
        for (int i = 0; i < bytes.length; ++i) {
            buffer.append((int) bytes[i]);
            buffer.append(" ");
        }
        buffer.append("]");
        return buffer.toString();
    }

    /** Convert the byteBuffer into a String to be used for logging and debugging.
     * 
     * @param byteBuffer the byteBuffer to be dumped
     * @return the String representation
     */
    public static String dumpBytes(ByteBuffer byteBuffer) {
        byteBuffer.mark();
        int length = byteBuffer.limit() - byteBuffer.position();
        byte[] dst = new byte[length];
        byteBuffer.get(dst);
        byteBuffer.reset();
        return dumpBytes(dst);
    }
}

Related

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