Java Byte Array Dump dumpArray(byte[] buffer, boolean breakLines)

Here you can find the source of dumpArray(byte[] buffer, boolean breakLines)

Description

Return the array as a printable string.

License

Educational Community License

Parameter

Parameter Description
buffer a parameter
breakLines Insert \n in returned String if CRLF was found

Declaration

public static String dumpArray(byte[] buffer, boolean breakLines) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2003, 2004 The Regents of the University of Michigan, Trustees of Indiana University,
 *                  Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation
 *
 * Licensed under the Educational Community License Version 1.0 (the "License");
 * By obtaining, using and/or copying this Original Work, you agree that you have read,
 * understand, and will comply with the terms and conditions of the Educational Community License.
 * You may obtain a copy of the License at:
 *
 *      http://cvs.sakaiproject.org/licenses/license_1_0.html
 *
 * 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.
 *
 *//*from ww  w. ja  v  a 2 s.c  om*/

public class Main {
    /**
     * Return the array as a printable string.
     * 
     * @param buffer
     * @param breakLines Insert \n in returned String if CRLF was found
     * @return
     */
    public static String dumpArray(byte[] buffer, boolean breakLines) {
        StringBuilder sb = new StringBuilder();
        boolean lastOneIsR = false;
        for (byte b : buffer) {
            if (b >= 32 && b <= 126)
                sb.append(new String(new byte[] { b }));
            else if (b == 10) {
                sb.append("\\n");
                if (lastOneIsR) {
                    if (breakLines)
                        sb.append("\n");
                    lastOneIsR = false;
                }
            } else if (b == 13) {
                sb.append("\\r");
                lastOneIsR = true;
            } else if (b == 0)
                sb.append("\\0");
            else
                sb.append("?");
        }
        return sb.toString();
    }
}

Related

  1. dump(byte[] mem, int start, int len)
  2. dump(byte[] rec)
  3. dump(byte[] str)
  4. dumpArray(byte b[])
  5. dumpArray(byte[] b)
  6. dumpByteArray(byte[] ab)
  7. dumpByteArray(byte[] b)
  8. dumpByteArray(byte[] buffer)
  9. dumpByteArray(byte[] bytes)