Java Array Dump dump_octets(byte[] oct)

Here you can find the source of dump_octets(byte[] oct)

Description

dumoctets

License

Apache License

Declaration

public static String dump_octets(byte[] oct) 

Method Source Code

//package com.java2s;
/*//from  w w w.  ja  va 2  s .  co  m
 *  Licensed to the Apache Software Foundation (ASF) under one or more
*  contributor license agreements.  See the NOTICE file distributed with
*  this work for additional information regarding copyright ownership.
*  The ASF licenses this file to You under the Apache License, Version 2.0
*  (the "License"); you may not use this file except in compliance with
*  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

public class Main {
    private static final char[] HEX_DIGIT = "0123456789abcdef".toCharArray();
    private static final int PRINTABLE_CHAR_LOW = 31;
    private static final int PRINTABLE_CHAR_HIGH = 127;

    public static String dump_octets(byte[] oct) {
        StringBuilder sb = new StringBuilder();
        dump_octets(oct, 0, oct.length, sb);
        return sb.toString();
    }

    public static void dump_octets(byte[] oct, StringBuilder sb) {
        dump_octets(oct, 0, oct.length, sb);
    }

    public static void dump_octets(final byte[] oct, final int offset, final int count, final StringBuilder sb) {
        if (count <= 0) {
            return;
        }

        final StringBuilder ascii = new StringBuilder(18);
        switch (offset % 0x10) {
        case 0:
            break;
        case 0xf:
            sb.append("  ");
            ascii.append(" ");
        case 0xe:
            sb.append("  ");
            ascii.append(" ");
        case 0xd:
            sb.append("  ");
            ascii.append(" ");
        case 0xc:
            sb.append("   ");
            ascii.append(" ");
        case 0xb:
            sb.append("  ");
            ascii.append(" ");
        case 0xa:
            sb.append("  ");
            ascii.append(" ");
        case 0x9:
            sb.append("  ");
            ascii.append(" ");
        case 0x8:
            sb.append("   ");
            ascii.append(" ");
        case 0x7:
            sb.append("  ");
            ascii.append(" ");
        case 0x6:
            sb.append("  ");
            ascii.append(" ");
        case 0x5:
            sb.append("  ");
            ascii.append(" ");
        case 0x4:
            sb.append("   ");
            ascii.append(" ");
        case 0x3:
            sb.append("  ");
            ascii.append(" ");
        case 0x2:
            sb.append("  ");
            ascii.append(" ");
        case 0x1:
            sb.append("  ");
            ascii.append(" ");
        }

        ascii.append(" \"");

        for (int i = offset; i < (offset + count); i++) {
            final int b = oct[i] & 0xff;

            // build up the ascii string for the end of the line
            ascii.append((PRINTABLE_CHAR_LOW < b && b < PRINTABLE_CHAR_HIGH) ? (char) b : '.');

            // print the high hex nybble
            sb.append(HEX_DIGIT[b >> 4]);
            // and the low hex nybble
            sb.append(HEX_DIGIT[b & 0xf]);

            if (i % 0x4 == (0x4 - 1)) {
                // space the columns on every 4-byte boundary
                sb.append(' ');
                if (i % 0x10 == (0x10 - 1)) {
                    // write the ascii interpretation on the end of every line
                    sb.append(ascii).append("\"\n");
                    ascii.setLength(0);
                    ascii.append(" \"");
                    if (i % 0x100 == (0x100 - 1)) {
                        // separating line every 0x100 bytes
                        //         00000000 00000000 00000000 00000000  "................"
                        sb.append("-----------------------------------\n");
                    }
                }
            }
        }

        switch ((offset + count) % 0x10) {
        case 0:
            break;
        case 0x1:
            sb.append("  ");
        case 0x2:
            sb.append("  ");
        case 0x3:
            sb.append("   ");
        case 0x4:
            sb.append("  ");
        case 0x5:
            sb.append("  ");
        case 0x6:
            sb.append("  ");
        case 0x7:
            sb.append("   ");
        case 0x8:
            sb.append("  ");
        case 0x9:
            sb.append("  ");
        case 0xa:
            sb.append("  ");
        case 0xb:
            sb.append("   ");
        case 0xc:
            sb.append("  ");
        case 0xd:
            sb.append("  ");
        case 0xe:
            sb.append("  ");
        case 0xf:
            sb.append("   ").append(ascii).append("\"\n");
        }
    }
}

Related

  1. dump(double[][] a)
  2. dump(final StringBuilder buffer, final byte[] data, final int offset, final int length)
  3. dump(String name, byte[] in)
  4. dump(String t, String[] arr)
  5. dump(T[] from, T[] to)
  6. dump_strarr(String[] arr, String doc)
  7. dumpArray(final float[] array, final int maxElemsPerLine)
  8. dumpArray(String msg, float[][] A, int x1, int x2, int y1, int y2)
  9. dumpArray(String msg, Object[] refs)