Here you can find the source of trim(byte[] src, byte padding, boolean right, int minLength)
public static byte[] trim(byte[] src, byte padding, boolean right, int minLength)
//package com.java2s; /*//from w w w. j av a 2s .c o m * Copyright (c) 2015. Troels Liebe Bentsen <tlb@nversion.dk> * Licensed under the MIT license (LICENSE.txt) */ import java.util.Arrays; public class Main { public static byte[] trim(byte[] src, byte padding, boolean right, int minLength) { if (src.length < minLength) { throw new RuntimeException("src array is smaller than minLength: " + src.length + " < " + minLength); } if (right) { int offset; for (offset = src.length - 1; offset > minLength - 1; offset--) { // [44, 32] if (padding != src[offset]) { break; } } if (offset < 0) { return new byte[0]; } else if (offset < src.length - 1) { return Arrays.copyOfRange(src, 0, offset + 1); } } else { int offset; for (offset = 0; offset < src.length - minLength; offset++) { if (padding != src[offset]) { break; } } if (offset == src.length) { return new byte[0]; } else if (offset > 0) { return Arrays.copyOfRange(src, offset, src.length); } } return src; } }