Removing the first 16 bytes from a byte array

后端 未结 4 1512
一个人的身影
一个人的身影 2021-01-17 15:31

In Java, how do I take a byte[] array and remove the first 16 bytes from the array? I know I might have to do this by copying the array into a new array. Any examples or hel

4条回答
  •  抹茶落季
    2021-01-17 16:04

    System.arraycopy() also can do that:

    public static byte[] truncate(byte[] bytes, int srcPos, int newLength) {
        if (bytes.length < newLength) {
            return bytes;
        } else {
            byte[] truncated = new byte[newLength];
            System.arraycopy(bytes, srcPos, truncated, 0, newLength);
            return truncated;
        }
    }
    

提交回复
热议问题