24

Have:

[46][111][36][11][101][55][87][30][122][75][66][32][49][55][67][77][88][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]

Want:

[46][111][36][11][101][55][87][30][122][75][66][32][49][55][67][77][88]

I have an array of bytes size 8192 to start, and starting at some index in that first array until the end of the array the bytes are all null bytes. So there might be 6000 bytes with values and 2196 null bytes at the end in the array. How do I efficiently create a new array of size (6000) and copy those bytes over? Note: I do not know how many null bytes, or bytes with values there will be.

Atomix
  • 2,440
  • 8
  • 36
  • 48
  • 1
    What are the remaining bytes in your new array going to equal? What you're looking to do doesn't make much sense. –  Jun 08 '13 at 19:44
  • A good old for loop? It will be simpler if you can't have zeros in the data part. – Pragmateek Jun 08 '13 at 19:44
  • A new `byte[]` will have 0 in all elements, so you won't actually accomplish anything if you want an 8192 element array at the end. – kdgregory Jun 08 '13 at 19:45

5 Answers5

34

Here is my try:

static byte[] trim(byte[] bytes)
{
    int i = bytes.length - 1;
    while (i >= 0 && bytes[i] == 0)
    {
        --i;
    }

    return Arrays.copyOf(bytes, i + 1);
}

public static void main(String[] args)
{
    byte[] bytes = { 0, 1, 2, 0, 3, 4, 5, 0, 6, 0, 0, 7, 8, 9, 10, 0, 0, 0, 0 };

    byte[] trimmed = trim(bytes);

    return;
}
Pragmateek
  • 13,174
  • 9
  • 74
  • 108
  • 1
    Is it safe to remove all bytes from end which are 0. This won't anything? – Sagar Trehan Jan 01 '19 at 18:57
  • @Pragmateek : I think in some cases it's going to make it lose the data, I have an xlsx file which has last octet of hex as D5000000 however if I use this as leave only D5, file corrupts. – Sagar Kharab Jul 05 '21 at 12:51
  • 1
    @SagarKharab Indeed tweaking a binary file always calls for caution, but this is most probably another use-case as the one of the PO. :) – Pragmateek Jul 06 '21 at 09:29
7

why not try the static method array copy in the system class just give the source array src start position , destination array , destination start position and the length

        System.arraycopy(src, srcPos, dest, destPos, length);
        byte [] dest= new byte [6000];
        System.arraycopy(src, 0, dest, 0, 6000);
Mohammed Falha
  • 967
  • 8
  • 22
  • 6
    The asker specified that he does not know how many non-zero bytes there are. This approach would only work if he knew beforehand. – Octahedron Jun 08 '13 at 20:55
7

I think we can do in this way also

byte []array={0, 69, 0, 71, 0, 72};

byte ar[]=new String(array).replaceAll("\0", "").getBytes();
Rich Benner
  • 7,873
  • 9
  • 33
  • 39
2

Another option using java string trim():

byte[] array={65, 66, 67, 0, 0, 0};

byte[] trimmedArray = new String(array).trim().getBytes();
Uri Loya
  • 1,181
  • 2
  • 13
  • 34
0

With kotlin :

val strWithoutZeros = String(byteArray,StandardCharsets.UTF_8).replace(0.toChar().toString(), "")

val byteArrayWithoutZeros = strWithoutZeros.toByteArray(StandardCharsets.UTF_8)
Nilesh Deokar
  • 2,975
  • 30
  • 53