Java Byte Array to String by Charset toDebugString(final byte[] bytes, final int len, final Charset charset)

Here you can find the source of toDebugString(final byte[] bytes, final int len, final Charset charset)

Description

to Debug String

License

Apache License

Declaration

static String toDebugString(final byte[] bytes, final int len, final Charset charset) 

Method Source Code


//package com.java2s;
/*/*from w  ww .j  a v  a 2s.c  o  m*/
 * #%L
 * ExpectIt
 * %%
 * Copyright (C) 2014 Alexey Gavrilov and contributors
 * %%
 * Licensed 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.
 * #L%
 */

import java.nio.charset.Charset;

public class Main {
    static final int MAX_STRING_LENGTH = 200;

    static String toDebugString(final byte[] bytes, final int len, final Charset charset) {
        if (charset == null) {
            return toDebugString(new String(bytes, 0, len, Charset.defaultCharset()));
        }
        return toDebugString(new String(bytes, 0, len, charset));
    }

    static String toDebugString(final String string) {
        if (string == null) {
            return "null";
        }
        final StringBuilder builder = new StringBuilder(string.length());
        for (int i = 0; i < string.length(); i++) {
            final char c = string.charAt(i);
            switch (c) {
            case '\n':
                builder.append("\\n");
                break;
            case '\r':
                builder.append("\\r");
                break;
            default:
                builder.append(c);
            }
            if (i > MAX_STRING_LENGTH) {
                builder.append(" ... (");
                builder.append(string.length() - i);
                builder.append(" char more)");
                break;
            }
        }
        return builder.toString();
    }

    static String toDebugString(final Object object) {
        return toDebugString(String.valueOf(object));
    }
}

Related

  1. stringToByteArray(String str, Charset charsetForEncoding, int lenOfByteArray, byte filler)
  2. toBytes(CharSequence str, String charsetName)
  3. toBytes(String string, Charset charset)
  4. toChars(byte[] b, String charset)
  5. toCharSet(byte[] bytes, String fromCharsetName, String toCharsetName)
  6. toEncodedString(byte[] bytes, Charset charset)
  7. toString(byte[] b, String charset)
  8. toString(byte[] bytes, Charset charset)
  9. toString(byte[] bytes, String charsetName)