4

I want to create a byte array of size 512 bytes.

For the first 100 bytes I want to reserve it for filename, for the next 412 bytes I want to use it to store data from the file itself.

Something like this :

|----100byte for file name ----||------------412 byte for file data------------|

      byte[] buffer = new byte[512];
      add the filename of  byte[] type into the first 100 position
      insert the file data after the first 100 position 

The file name can be less than 100 bytes.

But I am unable to append the file data at the specific position... how should I do that?

5
  • Why are you unable to do that? Commented Feb 11, 2015 at 14:17
  • How about using System.arraycopy()? Commented Feb 11, 2015 at 14:18
  • Your getting an index out of bounds exception i'm guessing. try making an array like: byte [] newArr = new byte[array1.length + array2.length]; then add to newArr Showing your code would help out alot Commented Feb 11, 2015 at 14:19
  • lets say I add the first byte array of 6 for my file name and then the 412 byte of data for the actual data inside the file. The buffer.length would be 418 instead of 512. Commented Feb 11, 2015 at 14:22
  • Your comment is contradictory to the question. I've edited my answer, but i'm still confused. If you add 6 bytes for the filename, but then skip 94 bytes because the first 100 are reserved for the filename, you still need 512 bytes total in order to fit 412 bytes of data. Is the filename of variable length, but maximal 100 bytes, or fixed at 100 bytes where some bytes can remain unused? Commented Feb 11, 2015 at 14:33

3 Answers 3

6

How about using System.arraycopy()?

byte[] buffer = new byte[data.length + name.length];
System.arraycopy(name, 0, buffer,           0, name.length)
System.arraycopy(data, 0, buffer, name.length, data.length)

You might need to add a check to ensure data.length + name.length does not exceed 512.

To fix the length of the name to 100, do it like this:

byte[] buffer = new byte[100 + name.length];
System.arraycopy(name, 0, buffer,   0, Math.min(100, name.length))
System.arraycopy(data, 0, buffer, 100, data.length)

To fix the total length to 512, add a limit to data.length:

byte[] buffer = new byte[512];
System.arraycopy(name, 0, buffer,   0, Math.min(100, name.length))
System.arraycopy(data, 0, buffer, 100, Math.min(412, data.length))
Sign up to request clarification or add additional context in comments.

4 Comments

I want to reserve the first 100 byte for the filename, so it should be System.arraycopy(data, 0, buffer, 100 , data.length) right?
You can replace all instances of name.length with 100 yes. You can do the same with name.length, replace it with 412 if you want to fix the array at 512 bytes. If data.lenght can be variable, consider to use Math.min(512, data.length + name.length) and Math.min(512 - name.length, data.length) for the array size, and the last parameter of the second arraycopy respectively.
In the third example. You can decrease the size of the byte array to new byte[100 + Math.min(412, data.length] if you want.
Thank you for your effort !:)
3

You can use a ByteBuffer. it is easier to read and follow among other options. You also gain a lot of other functionality, if needed down the road.

byte[] buffer = new byte[512];
byte[] fileName = new byte[100];
byte[] data = new byte[412];

// Create a ByteBuffer from the byte[] you want to populate
ByteBuffer buf = ByteBuffer.wrap(buffer);

// Add the filename
buf.position(0);
buf.put(fileName);

// Add the file data
buf.position(99);
buf.put(data);

// Get out the newly populated byte[]
byte[] result = buf.array();

Comments

0

System.arraycopy works well for the name, as @Dorus said, but the file data can be read directly into the array:

    byte[] buffer = new byte[512];
    File file = new File("/path/to/file");
    byte[] fileNameBytes = file.getName().getBytes();
    System.arraycopy(fileNameBytes, 0, buffer, 0, fileNameBytes.length > 100 ? 100 : fileNameBytes.length);
    FileInputStream in = new FileInputStream(file); 
    in.read(buffer, 100, file.length() > 412 ? 412 : (int)file.length());

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.