Java Dump Byte Array dump(PrintStream printer, byte[] buffer, int offset, int count)

Here you can find the source of dump(PrintStream printer, byte[] buffer, int offset, int count)

Description

Dumps a buffer in printable form.

License

Open Source License

Declaration


public static void dump(PrintStream printer, byte[] buffer, int offset, int count) 

Method Source Code


//package com.java2s;
/*//  www . j a  va2 s  .co  m
 * JSocket Wrench
 * 
 * Copyright (C) act365.com October 2003
 * 
 * Web site: http://www.act365.com/wrench
 * E-mail: developers@act365.com
 * 
 * The JSocket Wrench library adds support for low-level Internet protocols
 * to the Java programming language.
 * 
 * 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 2 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, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.io.*;

public class Main {
    /**
     Dumps a buffer in printable form.
    */

    public static void dump(PrintStream printer, byte[] buffer, int offset, int count) {

        if (count == 0) {
            return;
        }

        final int upper_bound = offset + (count / 8 + 1) * 8;

        int i = offset;

        StringBuffer str = new StringBuffer();

        while (i <= upper_bound) {

            int j;

            String tmpstr;

            if ((i - offset) % 8 == 0) {

                if (i > offset) {

                    str.append(' ');

                    j = i - 9;

                    while (++j < i) {

                        if (j < offset + count) {
                            if (buffer[j % buffer.length] >= 32) {
                                try {
                                    tmpstr = new String(buffer, j, 1, "UTF8");
                                } catch (UnsupportedEncodingException e) {
                                    tmpstr = null;
                                }
                            } else {
                                tmpstr = null;
                            }
                        } else {
                            tmpstr = " ";
                        }

                        if (tmpstr != null) {
                            str.append(tmpstr);
                        } else {
                            str.append('.');
                        }
                    }

                    printer.println(str.toString());
                }

                str = new StringBuffer(38);

                tmpstr = Integer.toHexString(i - offset);

                j = -1;

                while (++j < 4 - tmpstr.length()) {
                    str.append('0');
                }

                str.append(tmpstr);
            }

            str.append(' ');

            if (i < offset + count) {

                int unsigned = buffer[i % buffer.length] >= 0 ? buffer[i % buffer.length]
                        : 0xffffff00 ^ buffer[i % buffer.length];

                if (unsigned < 16) {
                    str.append('0');
                }

                str.append(Integer.toHexString(unsigned));
            } else {
                str.append("  ");
            }

            ++i;
        }
    }
}

Related

  1. dump(byte[] buffer, int start, int size, PrintStream out)
  2. dump(byte[] data, OutputStream out, boolean closeOutput)
  3. dump(final byte[] b, final PrintStream out)
  4. dumpBuffer(final PrintStream out, final String label, final byte[] b)
  5. dumpByteArray(byte[] byteArray)
  6. dumpByteArray(byte[] bytes)
  7. dumpBytes(PrintStream printStream, byte bytes[])