Java Byte Array Shorten abbreviate(final byte[] bytes)

Here you can find the source of abbreviate(final byte[] bytes)

Description

abbreviate

License

Apache License

Declaration

public static String abbreviate(final byte[] bytes) 

Method Source Code

//package com.java2s;
/*/*www  . j  ava 2s . c o  m*/
 * Copyright 2016 NAVER Corp.
 *
 * 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.
 *
 */

public class Main {
    private static final int DEFAULT_ABBREVIATE_MAX_WIDTH = 32;

    public static String abbreviate(final byte[] bytes) {
        return abbreviate(bytes, DEFAULT_ABBREVIATE_MAX_WIDTH);
    }

    public static String abbreviate(final byte[] bytes, final int maxWidth) {
        if (bytes == null) {
            return "null";
        }
        if (maxWidth < 0) {
            throw new IllegalArgumentException("negative maxWidth:" + maxWidth);
        }
        // TODO handle negative limit

        // Last valid index is length - 1
        int bytesMaxLength = bytes.length - 1;
        final int maxLimit = maxWidth - 1;
        if (bytesMaxLength > maxLimit) {
            bytesMaxLength = maxLimit;
        }
        if (bytesMaxLength == -1) {
            if (bytes.length == 0) {
                return "[]";
            } else {
                return "[...(" + bytes.length + ")]";
            }
        }

        final StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0;; i++) {
            sb.append(bytes[i]);
            if (i == bytesMaxLength) {
                if ((bytes.length - 1) <= maxLimit) {
                    return sb.append(']').toString();
                } else {
                    sb.append(", ...(");
                    sb.append(bytes.length - (i + 1));
                    sb.append(")]");
                    return sb.toString();
                }
            }
            sb.append(", ");
        }
    }
}

Related

  1. abbrevBytes(long bytes)
  2. abbreviate(byte[] bytes, int offset, int maxLength)