473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

FileInputStream is too slow

Nepomuk
3,112 Expert 2GB
Hi!

I'm trying to transfer files and to do so must, of course, read them. Now, my reference File has a size of 4.240.821 Bytes and I need something between 9 and 14 seconds to simply read it and save the data to arrays.

When however I transfer the file via FTP, everything is done in about 1 second - reading, transferring and writing.

I've written the following code, to find out, how much time is needed with different sizes of arrays:

Expand|Select|Wrap|Line Numbers
  1. import java.io.IOException;
  2. import java.io.FileInputStream;
  3. import java.io.File;
  4.  
  5. public class FileReadSpeed {
  6.     public static void main(String[] args) {
  7.         int howMany = 8;
  8.         int howOften = 5;
  9.         long[][] results = new long[howMany][howOften];
  10.         int size = 64;
  11.  
  12.         try
  13.         {
  14.             File file = new File("E:\\temp\\file.tar.gz");
  15.  
  16.             for(int i=8;i<8+howMany;i++)
  17.             {
  18.                 size *= 2;
  19.                 for(int j=0; j<howOften; j++)
  20.                 {
  21.                     System.gc();
  22.                     FileInputStream fiStream = new FileInputStream(file);
  23.                     long startTime = System.currentTimeMillis();
  24.                     byte[] data = new byte[size];
  25.                     long amount = file.length() / size;
  26.                     if(file.length() % size > 0) amount++;
  27.                     for(int k=0; k<amount; k++)
  28.                     {
  29.                         for(int l=0; l<data.length; l++)
  30.                         {
  31.                             if((k == amount-1) && (l == file.length() % size)) break;
  32.                             data[l] = (byte) fiStream.read();
  33.                         }
  34.                     }
  35.                     long finishTime = System.currentTimeMillis();
  36.                     long timeTaken = finishTime - startTime;
  37.                     results[i-8][j] = timeTaken;
  38.                     System.out.println("Recorded (" + (i-8) + " | " + j + ") with size = " + size + " Bytes");
  39.                 }
  40.             }
  41.         }
  42.         catch(IOException ioe)
  43.         {
  44.             System.err.println(ioe);
  45.         }
  46.  
  47.         System.out.println();
  48.  
  49.         size = 64;
  50.         for(int i=0; i<results.length; i++)
  51.         {
  52.             size *= 2;
  53.             System.out.print(size + ":\t");
  54.             for(int j=0; j<results[i].length; j++)
  55.             {
  56.                 System.out.print(results[i][j] + "\t");
  57.             }
  58.             double tmp = 0.;
  59.             for(int j=0; j<results[i].length; j++)
  60.             {
  61.                 tmp += results[i][j];
  62.             }
  63.             System.out.println("-> " + tmp/results[0].length);
  64.         }
  65.     }
  66. }
  67.  
Does anyone know, if there are implementations of FileInputStream or similar, which are faster? (Maybe with reduced functionality?)

Greetings,
Nepomuk
Sep 18 '07 #1
6 5916
JosAH
11,448 Expert 8TB
You're reading a single byte every time; that is dead slow; you can read entire
byte arrays in one sweep, use that instead (check the FileInputStream API).
A byte array size of, say, 4096 is a typical buffer size.

kind regards,

Jos
Sep 18 '07 #2
Nepomuk
3,112 Expert 2GB
You're reading a single byte every time; that is dead slow; you can read entire
byte arrays in one sweep, use that instead (check the FileInputStream API).
A byte array size of, say, 4096 is a typical buffer size.

kind regards,

Jos
OK, I've tried that, but somehow I've made a mistake and can't find it... it should be directly before my eyes I guess, but maybe someone can help.

Here's my changed for-loop:
Expand|Select|Wrap|Line Numbers
  1. for(int k=0; k<amount; k++)
  2. {
  3.     int offset = (int)(k*size);
  4.     System.out.println("offset:\t" + offset + "\nsize:\t" + size);
  5.     fiStream.read(data,offset,size);
  6. }
  7.  
I also changed the settings a little:
Expand|Select|Wrap|Line Numbers
  1. int howMany = 4;
  2. int howOften = 5;
  3. int size = 512;
  4.  
And I get a IndexOutOfBoundsException in the line, which does the reading, when the output is as follows:
Expand|Select|Wrap|Line Numbers
  1. offset:    0
  2. size:    1024
  3. offset:    1024
  4. size:    1024
  5. java.lang.IndexOutOfBoundsException...
  6.  
It seems to me, that the offset is wrong somehow, but I don't understand, what is wrong...

Greetings,
Nepomuk
Sep 18 '07 #3
JosAH
11,448 Expert 8TB
Just do a:

Expand|Select|Wrap|Line Numbers
  1. int nofBytesRead= fiStream.read(data);
  2.  
and it will attempt to read as many bytes as possible in the 'data' byte array.
All in one sweep that is.

kind regards,

Jos
Sep 18 '07 #4
Nepomuk
3,112 Expert 2GB
Just do a:

Expand|Select|Wrap|Line Numbers
  1. int nofBytesRead= fiStream.read(data);
  2.  
and it will attempt to read as many bytes as possible in the 'data' byte array.
All in one sweep that is.

kind regards,

Jos
The Problem with that is, that my program should be able to send files of any size and with a file, which is over 100MB big, that won't work (I have tried it).

So, what mistake did I make with the offset version?

Greetings,
Nepomuk
Sep 18 '07 #5
Nepomuk
3,112 Expert 2GB
OK, I've found the error - I thought offset was the offset for the file, not for the array. It does work much faster now.

Thank you very much!

Greetings,
Nepomuk
Sep 19 '07 #6
JosAH
11,448 Expert 8TB
OK, I've found the error - I thought offset was the offset for the file, not for the array. It does work much faster now.

Thank you very much!

Greetings,
Nepomuk
Told you so ;-) Simply create a byte array of 64K or so, keep on reading into that
array, write the array until the read() call returns -1. The previous read might
return a value less than the size of the array so you should always use that
value.

kind regards,

Jos
Sep 19 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jacqueline Snook | last post by:
I am trying to reference a file on an apple mac from Java. In order to find what the URL is supposed to be, I opened it in the Safari web browser. The directory as returned was...
6
by: Patrick | last post by:
Hello all! I am porting an application from C++ to Java and have run into a problem using the DataInputStream reader object. The file I am trying to read in is anywhere from 20 to 60 MB and has a...
1
by: Abhijit Gadekar | last post by:
Hello, I am getting a problem with objectinputstream and fileinputstream. Whenever I try to close a ObjectInputStream object it gives an Exception. And if I try to delete the associated file on...
2
by: David | last post by:
Hi, We have an internal network of 3 users. Myself & one other currently have individual copies of the front-end MS Access forms and via our individual ODBC links we have used the: File > Get...
3
by: Jennyfer J Barco | last post by:
In my application I have a datagrid. The code calls a Stored procedure and brings like 200 records. I created a dataset and then a dataview to bind the results of the query to my grid using ...
50
by: diffuser78 | last post by:
I have just started to learn python. Some said that its slow. Can somebody pin point the issue. Thans
2
by: mezise | last post by:
Posted by Pratchaya: ------------------------------------------------------ MySQL Slow Log ERROR In my.cnf i add these lines ####### log-bin log-slow-queries = /var/log/mysqld-slow.log
6
by: Tukeind | last post by:
Hello, I've opened a file (code below) containing file names onto a FileInputStream, and attempting to open each file and place its contents onto a FileOutputStream (or buffer) and need help getting...
2
by: rubyhuang | last post by:
when using eclipse, if want to read a file(eg. using bufferedReader to read), then where to put the .txt file, no matter what place i put the .txt file to, it always said at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.