27

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.

3
  • 1
    What are the remaining bytes in your new array going to equal? What you're looking to do doesn't make much sense. Commented Jun 8, 2013 at 19:44
  • A good old for loop? It will be simpler if you can't have zeros in the data part. Commented Jun 8, 2013 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. Commented Jun 8, 2013 at 19:45

5 Answers 5

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; } 
Sign up to request clarification or add additional context in comments.

3 Comments

Is it safe to remove all bytes from end which are 0. This won't anything?
@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.
@SagarKharab Indeed tweaking a binary file always calls for caution, but this is most probably another use-case as the one of the PO. :)
8

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(); 

1 Comment

I like the use of chaining. This is clear and makes it a one liner!
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); 

1 Comment

The asker specified that he does not know how many non-zero bytes there are. This approach would only work if he knew beforehand.
2

Another option using java string trim():

byte[] array={65, 66, 67, 0, 0, 0}; byte[] trimmedArray = new String(array).trim().getBytes(); 

Comments

1

With kotlin :

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.