Java Array Trim trim(byte[] src, byte padding, boolean right, int minLength)

Here you can find the source of trim(byte[] src, byte padding, boolean right, int minLength)

Description

trim

License

MIT License

Declaration

public static byte[] trim(byte[] src, byte padding, boolean right, int minLength) 

Method Source Code

//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;
    }
}

Related

  1. safeTrim(byte[] ba, int len)
  2. safeTrim(final char[] ca, final int len)
  3. trim(byte[] bytes)
  4. trim(byte[] bytes, int endMargin)
  5. trim(byte[] data)
  6. trim(String s, char delimit[])
  7. trim(String[] args)
  8. trim(String[] array)
  9. trim(String[] array)