Java ByteBuffer Dump dump(ByteBuffer buffer, OutputStream stream)

Here you can find the source of dump(ByteBuffer buffer, OutputStream stream)

Description

dump

License

Open Source License

Declaration

public static void dump(ByteBuffer buffer, OutputStream stream) throws IOException 

Method Source Code


//package com.java2s;
/* Copyright 2004 - 2007 Kasper Nielsen <kasper@codehaus.org> Licensed under 
 * the Apache 2.0 License, see http://coconut.codehaus.org/license.
 *///from w ww .j  a va 2  s. co m

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;

public class Main {
    private static final String EOL = System.getProperty("line.separator");
    private static final StringBuilder _lbuffer = new StringBuilder(8);
    private static final StringBuilder _cbuffer = new StringBuilder(2);
    private static final char _hexcodes[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
            'E', 'F' };
    private static final int _shifts[] = { 28, 24, 20, 16, 12, 8, 4, 0 };

    public static void dump(ByteBuffer buffer, OutputStream stream) throws IOException {
        int size = buffer.limit() - buffer.position();
        byte[] bytes = new byte[size];
        System.arraycopy(buffer.array(), buffer.position(), bytes, 0, size);
        dump(bytes, 0, stream, 0);
    }

    public static void dump(byte[] data, long offset, OutputStream stream, int index) throws IOException {
        if ((index < 0) || (index >= data.length)) {
            throw new ArrayIndexOutOfBoundsException(
                    "illegal index: " + index + " into array of length " + data.length);
        }
        if (stream == null) {
            throw new IllegalArgumentException("cannot write to nullstream");
        }
        long display_offset = offset + index;
        StringBuilder buffer = new StringBuilder(74);

        for (int j = index; j < data.length; j += 16) {
            int chars_read = data.length - j;

            if (chars_read > 16) {
                chars_read = 16;
            }
            buffer.append(dump(display_offset)).append(' ');
            for (int k = 0; k < 16; k++) {
                if (k < chars_read) {
                    buffer.append(dump(data[k + j]));
                } else {
                    buffer.append("  ");
                }
                buffer.append(' ');
            }
            for (int k = 0; k < chars_read; k++) {
                if ((data[k + j] >= ' ') && (data[k + j] < 127)) {
                    buffer.append((char) data[k + j]);
                } else {
                    buffer.append('.');
                }
            }
            buffer.append(EOL);
            stream.write(buffer.toString().getBytes());
            stream.flush();
            buffer.setLength(0);
            display_offset += chars_read;
        }
    }

    private static StringBuilder dump(long value) {
        _lbuffer.setLength(0);
        for (int j = 0; j < 8; j++) {
            _lbuffer.append(_hexcodes[((int) (value >> _shifts[j])) & 15]);
        }
        return _lbuffer;
    }

    private static StringBuilder dump(byte value) {
        _cbuffer.setLength(0);
        for (int j = 0; j < 2; j++) {
            _cbuffer.append(_hexcodes[(value >> _shifts[j + 6]) & 15]);
        }
        return _cbuffer;
    }
}

Related

  1. dump(ByteBuffer bb)
  2. dump(ByteBuffer buff, int size, boolean asBits)
  3. dump(ByteBuffer buffer)
  4. dump(ByteBuffer buffer)
  5. dump(ByteBuffer buffer, int pos, int limit)
  6. dumpAsHex(byte[] byteBuffer, int length)
  7. dumpBoolean(ByteBuffer itemBuffer, StringBuilder sb, String tag)
  8. dumpBytes(ByteBuffer bbuf)
  9. dumpBytes(ByteBuffer byteBuffer)