Java ByteBuffer Dump dumpBytes(ByteBuffer bbuf)

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

Description

dump Bytes

License

Open Source License

Declaration

public static String dumpBytes(ByteBuffer bbuf) 

Method Source Code

//package com.java2s;
/*// ww w  .ja v  a2s. c  o  m
 * ByteUtil.java - utilities for byte array handling.
 * 
 * Copyright (c) 2009-2015 PIAX develoment team
 * Copyright (c) 2006-2008 Osaka University
 * Copyright (c) 2004-2005 BBR Inc, Osaka University
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * $Id: ByteUtil.java 1176 2015-05-23 05:56:40Z teranisi $
 */

import java.nio.ByteBuffer;

public class Main {

    public static String dumpBytes(byte[] b, int offset, int len) {
        StringBuilder str = new StringBuilder();
        for (int i = offset; i < offset + len; i++) {
            int _b = ((int) b[i]) & 0xff;
            if (_b >= 0x20 && _b <= 0x7e) {
                str.append((char) _b);
            } else {
                char upper = Character.forDigit(_b / 16, 16);
                char lower = Character.forDigit(_b % 16, 16);
                str.append('.');
                str.append(upper);
                str.append(lower);
            }
        }
        return str.toString();
    }

    public static String dumpBytes(byte[] b) {
        return dumpBytes(b, 0, b.length);
    }

    public static String dumpBytes(ByteBuffer bbuf) {
        //        byte[] b = bbuf.array();
        //        int off = bbuf.arrayOffset() + bbuf.position();
        //        return dumpBytes(b, off, bbuf.remaining());
        StringBuilder str = new StringBuilder();
        int len = bbuf.remaining();
        ByteBuffer b = bbuf.slice();
        for (int i = 0; i < len; i++) {
            int _b = ((int) b.get()) & 0xff;
            if (_b >= 0x20 && _b <= 0x7e) {
                str.append((char) _b);
            } else {
                char upper = Character.forDigit(_b / 16, 16);
                char lower = Character.forDigit(_b % 16, 16);
                str.append('.');
                str.append(upper);
                str.append(lower);
            }
        }
        return str.toString();
    }
}

Related

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