Here you can find the source of trim(byte[] bytes, int endMargin)
public static byte[] trim(byte[] bytes, int endMargin)
//package com.java2s; /******************************************************************************* Copyright 2014 Pawel Pastuszak//w w w . ja v a2s. c o m This file is part of Arget. Arget is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Arget is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Arget. If not, see <http://www.gnu.org/licenses/>. ******************************************************************************/ import java.util.Arrays; public class Main { public static byte[] trim(byte[] bytes, int endMargin) { int i = bytes.length - 1; while (i >= 0 && bytes[i] == 0) { --i; } return Arrays.copyOf(bytes, i + 1 + endMargin); } }