0

I have a file which is comprised of one serialized String object written to the start of the file followed by the raw bytes of the file I am attempting to extract.

Here is my code:

FileInputStream fileInputStream = new FileInputStream("C:\Test.tst");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
String string = (String) objectInputStream.readObject();
FileOutputStream fileOutputStream = new FileOutputStream("C:\ExtractedTest.tst");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
while(fileInputStream.available())
{
  int i = fileInputStream.read();
  bufferedOutputStream.write(i);
}
bufferedOutputStream.close();
fileOutputStream.close();

The code takes an unusable long time for large files (1.5 GB, for example). How can I speed up the code? Am I using the wrong classes?

Regards.

Danny Rancher
  • 1,923
  • 3
  • 24
  • 43

2 Answers2

1

First of all you don't need that I guess:

ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
String string = (String) objectInputStream.readObject();

... and your loop should look more like that:

final byte[] temp = new byte[1000];
while (fileInputStream.available() > 0){
 int i = fileInputStream.read(temp);
 bufferedOutputStream.write(temp, 0, i);
}
michali
  • 401
  • 2
  • 11
  • And remember about closing the fileInputStream :] – michali Dec 20 '13 at 10:48
  • Say there was another object after the file and the first object was instead a long typed object representing the size of the file. Is it possible to siphon out the file? temp[1000] goes beyond the file boundary into the second object. – Danny Rancher Dec 20 '13 at 11:38
  • @DannyRancher I don't understand what you are saying but variable i stands for the number of bytes you want to write to the output stream. Maybe play with that OR explain more clearly what you want to achieve ;) – michali Dec 20 '13 at 12:33
0

You can try to finetune your application by changing the buffer size.

http://docs.oracle.com/javase/7/docs/api/java/io/BufferedOutputStream.html

Here you have documented a version of the constructor with a buffer size. Maybe you can use a big buffer (at expense of your memory usage, of course, be ready to increase your heap size too)

Increase heap size in Java

Community
  • 1
  • 1
Jorge_B
  • 9,712
  • 2
  • 17
  • 22