Java Byte Array Shorten abbreviate(byte[] bytes, int offset, int maxLength)

Here you can find the source of abbreviate(byte[] bytes, int offset, int maxLength)

Description

Abbreviates to a string to max length

License

LGPL

Parameter

Parameter Description
msgBuf a parameter
offset a parameter
maxLength a parameter

Declaration

public static String abbreviate(byte[] bytes, int offset, int maxLength) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    public static final String EMPTY_STRING = "";
    public static final String ELLIPSIS_STRING = "...";

    /**//from   w  w w.  j  a  v  a2  s . co  m
     * If String exceeds maxLength, truncate it and add {@link #ELLIPSIS_STRING} 
     * to end.
     * @param msg
     * @param maxLength
     * @return
     */
    public static String abbreviate(String msg, int maxLength) {
        if (msg == null)
            return null;
        if (msg.length() > maxLength) {
            return msg.substring(0, maxLength) + ELLIPSIS_STRING;
        }
        return msg;
    }

    /**
     * Abbreviates to a string to max length
     * @param msgBuf
     * @param offset
     * @param maxLength
     * @return
     */
    public static String abbreviate(byte[] bytes, int offset, int maxLength) {
        if (bytes == null || offset < 0 || maxLength < 1)
            return EMPTY_STRING;
        int msgLength = bytes.length - offset;
        int length = msgLength > maxLength ? maxLength : msgLength;
        String message = new String(bytes, offset, length);
        if (msgLength > maxLength)
            message += ELLIPSIS_STRING;
        return message;
    }
}

Related

  1. abbrevBytes(long bytes)
  2. abbreviate(final byte[] bytes)