Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Best way to append null bytes to a byte array?

807591Mar 11 2008 — edited Mar 11 2008
I have a fixed length byte array and need to add two null bytes to its end. I have an implementation that I wrote to do this, though I was wondering whether someone could improve on what I have:
public byte[] appendNullBytes( byte[] bytes ) {
   byte[] copyBytes = new byte[bytes.length+2];

   System.arraycopy(bytes, 0, copyBytes, 0, bytes.length);
   copyBytes[copyBytes.length-1] = 0;
   copyBytes[copyBytes.length-2] = 0;

   return copyBytes
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 8 2008
Added on Mar 11 2008
16 comments
11,096 views