Java Byte Array Dump dump(byte[] data, int offset, int length)

Here you can find the source of dump(byte[] data, int offset, int length)

Description

dump

License

Open Source License

Declaration

public static String dump(byte[] data, int offset, int length) 

Method Source Code

//package com.java2s;
/*/* w ww.j  a  v  a  2 s.  c om*/
 * Copyright (c) 2013, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software;Designed and Developed mainly by many Chinese 
 * opensource volunteers. you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License version 2 only, as published by the
 * Free Software Foundation.
 *
 * This code 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
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * Any questions about this component can be directed to it's project Web address 
 * https://code.google.com/p/opencloudb/.
 *
 */

public class Main {
    public static String dump(byte[] data, int offset, int length) {

        StringBuilder sb = new StringBuilder();
        sb.append(" byte dump log ");
        sb.append(System.lineSeparator());
        sb.append(" offset ").append(offset);
        sb.append(" length ").append(length);
        sb.append(System.lineSeparator());
        int lines = (length - 1) / 16 + 1;
        for (int i = 0, pos = 0; i < lines; i++, pos += 16) {
            sb.append(String.format("0x%04X ", i * 16));
            for (int j = 0, pos1 = pos; j < 16; j++, pos1++) {
                sb.append(pos1 < length ? String.format("%02X ", data[offset + pos1]) : "   ");
            }
            sb.append(" ");
            for (int j = 0, pos1 = pos; j < 16; j++, pos1++) {
                sb.append(pos1 < length ? print(data[offset + pos1]) : '.');
            }
            sb.append(System.lineSeparator());
        }
        sb.append(length).append(" bytes").append(System.lineSeparator());
        return sb.toString();
    }

    public static char print(byte b) {
        return (b < 32 || b > 127) ? '.' : (char) b;
    }
}

Related

  1. dump(byte[] array)
  2. dump(byte[] buff, int size, boolean asBits)
  3. dump(byte[] buffer)
  4. dump(byte[] bytes)
  5. dump(byte[] bytes, int offset, int byteslen)
  6. dump(byte[] data, int pos, int length)
  7. dump(byte[] mem, int start, int len)
  8. dump(byte[] rec)
  9. dump(byte[] str)