Java Array Trim trimArray(final byte[] seq, final int trimFromFront, final int trimFromBack)

Here you can find the source of trimArray(final byte[] seq, final int trimFromFront, final int trimFromBack)

Description

Trim any number of bases from the front and/or back of an array

License

Open Source License

Parameter

Parameter Description
seq the sequence to trim
trimFromFront how much to trim from the front
trimFromBack how much to trim from the back

Return

a non-null array; can be the original array (i.e. not a copy)

Declaration

public static byte[] trimArray(final byte[] seq, final int trimFromFront, final int trimFromBack) 

Method Source Code

//package com.java2s;
/*//  w ww. j av  a 2  s. com
 * Copyright (c) 2017 NCIC, Institute of Computing Technology, Chinese Academy of Sciences
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.util.Arrays;

public class Main {
    /**
     * Trim any number of bases from the front and/or back of an array
     *
     * @param seq                the sequence to trim
     * @param trimFromFront      how much to trim from the front
     * @param trimFromBack       how much to trim from the back
     * @return a non-null array; can be the original array (i.e. not a copy)
     */
    public static byte[] trimArray(final byte[] seq, final int trimFromFront, final int trimFromBack) {
        if (trimFromFront + trimFromBack > seq.length)
            throw new IllegalArgumentException("trimming total is larger than the original array");

        // don't perform array copies if we need to copy everything anyways
        return (trimFromFront == 0 && trimFromBack == 0) ? seq
                : Arrays.copyOfRange(seq, trimFromFront, seq.length - trimFromBack);
    }
}

Related

  1. trim(String[] array)
  2. trim(String[] data)
  3. trim(String[] s)
  4. trim(String[] strings)
  5. trimArray(char[] buffer, int read)
  6. trimBytes(byte[] source, int fromIndex, int toIndex)
  7. trimLeadingZeros(byte[] original)
  8. trimLeft(byte[] src, int trimSize)
  9. trimRecords(String[] records)