Results 1 to 16 of 16
- 04-07-2011, 07:07 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 20
- Rep Power
- 0
Writing huge sized file data(more than 100Mb) to a output stream(converting to byte[]
Hi All,
I have a file which is more than 100MB which needs to be written to a output stream (ServletOutputStream) as a response to a request where we can write byte array to a output stream. (outputStream.write(byte[])
Currently, i am using apache commons IOUtils package,
IOUtils.toByteArray(InputStream inputStream)
but this is not working when the file size is more.
Please let me know the better approach with good performance to handle huge files like more than 100MB.
Thanks,
Chinnu
- 04-07-2011, 07:12 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Have a look at the following.
http://code.hammerpig.com/how-to-rea...s-in-java.html
It's a very common solution, yet it has some drawbacks as well.
- 04-07-2011, 07:12 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Have a look at the following.
How to Read Really Large Files in Java | Code Comments
It's a very common solution, yet it has some drawbacks as well.
- 04-07-2011, 07:16 PM #4
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-07-2011, 07:21 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-08-2011, 09:21 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Where is this file coming from, and do you need to read it all in in one go before streaming it out?
If it's simply passing through, then read and write at the same time.
- 04-08-2011, 02:28 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 20
- Rep Power
- 0
Kevin - Thanks for making me understand about Crosspost.
I am currently using java 1.4.
I have huge files whose size is more than > 150 MB. I need to write it to output response so that user can download the report in excel format.
But, to write to response outputstream
OutputStream out
out.write(byte[]) - outputStream accepts byte[].
So, i need to convert huge file data into byte[] first to pass it to response.
Tolls, i am generating file locally with the data.
Thanks,
Chinnu
- 04-11-2011, 09:52 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Wrap the OutputStream in an OutputStreamWriter (or something along those lines) and use that.
Then write to that stream as you generate the data. Don't (if at all possible, and it almost always is) wait until you have all the data and then write. That will mean you can handle any size of data.
- 04-11-2011, 04:38 PM #9
Member
- Join Date
- Apr 2011
- Posts
- 20
- Rep Power
- 0
Writing huge sized file data(more than 100Mb) to a zip file(converting to byte[]
Thanks Tolls for your reply,
Problem is :
I have a file which will be more than 100 MB based on data coming from DB, i need to put this file in a zip file.
while ((newEntry = inZip.getNextEntry()) != null)
{
destEntry = new ZipEntry (newEntry.getName());
ZipEntry out;
InputStream source;
if (destEntry.getName().equals("HugeFile.xml"))
{
out = new ZipEntry(destEntry);
out.setSize(HugeFilesByteData.length);
// HugeFilesByteArrayData is a HugeFile's data converted to byte array
source = new ByteArrayInputStream(HugeFilesByteArrayData);
}
else
{
/* just copy from source ZIP */
out = destEntry;
source = inZip;
}
outZip.putNextEntry(out);
IOUtils.copy(source,outZip);
To put Huge file into Zip file, i need to convert the file data into byte[].
byte[] fileBytes = null;
try {
fileBytes = new byte[(int) HugeFile.length()]; // here it fails and throws outofmemory error.
fileInStream = new FileInputStream(inputFile);
fileInStream.read(fileBytes);
}
and then this Zip file which contains many files along with HugeFile.xml will be sent as a response through outputstream.
please let me know the better approach.
Thanks,
Chinnu
- 04-11-2011, 04:50 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Don't read it all in one go.
This is what I have been telling you.
That's how you do this.Java Code:Open input stream Open output stream create byte buffer while (read stuff into byte buffer) { write byte buffer to output stream }
You use a buffer (say 8k).
Read into that, write it out.
Repeat until you run out of things to read in.
- 04-11-2011, 04:50 PM #11
Member
- Join Date
- Apr 2011
- Posts
- 20
- Rep Power
- 0
Writing huge sized file data(more than 100Mb) to a zip file(converting to byte[]
May i know what will be the maximum size byte[] can take?
byte b[] = new byte[???];
Thanks,
Chinnu
- 04-11-2011, 04:55 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Why?
Just use an 8k buffer. It's good enough for almost any situation.
Java Code:byte[] b = new byte[8192];
- 04-11-2011, 05:08 PM #13
- 04-11-2011, 05:36 PM #14
Member
- Join Date
- Apr 2011
- Posts
- 20
- Rep Power
- 0
Writing huge sized file data(more than 100Mb) to a zip file(converting to byte[]
Thanks Tolls,
I am still getting confused how to move forward.
Can you please give a sample code which might help?
Thanks,
Chinnu
- 04-12-2011, 08:06 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
I have explained above how to write it.
So try.
- 04-12-2011, 03:42 PM #16
Member
- Join Date
- Apr 2011
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Writing output file
By doymand in forum New To JavaReplies: 2Last Post: 11-05-2010, 01:24 PM -
converting byte array to bmp file
By Moorkh in forum New To JavaReplies: 2Last Post: 09-07-2010, 02:58 PM -
[SOLVED] Converting output stream to string
By dan0 in forum New To JavaReplies: 0Last Post: 03-16-2009, 03:44 PM -
[SOLVED] Overwrite text file line with output stream loop
By jenni in forum New To JavaReplies: 3Last Post: 02-16-2009, 09:20 PM -
Reading/Writing a File using byte array
By Java Tip in forum Java TipReplies: 0Last Post: 01-16-2008, 10:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks