Java tutorial
//package com.java2s; import java.util.Arrays; public class Main { public static byte[] trimLast(byte[] bytes) { int i = bytes.length - 1; for (; i >= 0; i--) { if (bytes[i] != 0) { break; } } return Arrays.copyOfRange(bytes, 0, i + 1); } }