0

I have data from 3 files in a ByteArrayOutputStream variable. But when I try to pass it on through BufferedOutputStream, it only sends the data for the last file.

byte [] finalData = new byte[64000];
finalData = outputStream.toByteArray();     
output.write(finalData, 0, finalData.length);

It is overwriting the previous data for 2 files somehow.

05_pingpong
  • 77
  • 1
  • 2
  • 13

1 Answers1

0

ByteArrayOutputStream variable. But when I try to pass it on through BufferedOutputStream

I suggest writing the files directly to the BufferedOutputStream to reduce memory copies.

it only sends the data for the last file.

Most likely to are opening and closing the file repeatedly, writing over the file each time. You could append to the file, but the most efficient way is to write directly to the file.

Note: to read the file you need some way of determining where a file starts and ends. One approach is to write an index to the end of the file (e.g. as ZIP does) or write a second file which has the offsets, or write the length to the start of the message appended. If it's a text format you might have another way to determine where one starts/ends.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • while writing directly to the "BufferedOutputStream" it overwrites the existing data probably because of start/end indicators. Could you sugggest me how to avoid that? II want the stream to just continue writing till all the data(files) end. – 05_pingpong Dec 12 '18 at 09:22
  • @05_pingpong writing more data isn't the problem. The problem is reading the data to know there is more than one file in what was written. How you do that depends on your file format. – Peter Lawrey Dec 12 '18 at 11:34