?
Solved

How to read only a certain part of a file in java.

Posted on 2010-01-07
13
Medium Priority
?
624 Views
Last Modified: 2013-12-29
I have BufferedInputStream in a Java program that is being instantiated as follows:

fis = new java.io.FileInputStream(file);
bis = new java.io.BufferedInputStream(fis);


However, I want The bufferedInputStream to only be able to read a certain portion of the files, that is, as if it had an offset and length. I want it to be able to read the bytes in the middle with a certain offset and inset. How can I do this?

Your help is greatly appreciated.
0
Comment
Question by:Tuxx
  • 6
  • 6
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 26205027
It will fill as much buffer as you allow it to. From memory, it will use 8KiB, so if you want it to use less, specify the buffer size. Of course, irrespective of the buffer size, it will only read what you tell it to. You can also skip() to a certain point. See Javadoc
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 26205169
You might OTH be better off using RandomAccessFile
0
 
LVL 5

Author Comment

by:Tuxx
ID: 26205319
@CEHJ I will check out your suggestion.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Assisted Solution

by:Frelling
Frelling earned 600 total points
ID: 26206641
Dear Tuxed:

If you want to use your current approach to read a specific offset into the opened file, you can use

bis.read( buffer, offset, num );

which reads num bytes starting at offset bytes in the file into the byte buffer. However, if you find yourself having to randomly access files, rather than just skipping forward, using random access files accessors would be better, as CEHJ recommended. Here is a link to an introduction to the latter approach.

Best regards,

Frelling
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 26207729
>>@CEHJ I will check out your suggestion.

OK. If you give us some more details i can give you a specific solution probably
0
 
LVL 5

Author Comment

by:Tuxx
ID: 26209292
@CEHJ

Ok, it goes like this..

I want the CipherInputStream to only read the first 50 bytes of the file. How may I do this?
fis = new java.io.FileInputStream(file);
cin = new javax.crypto.CipherInputStream(fis, pbeCipher);
zin = new java.util.zip.ZipInputStream(new java.io.BufferedInputStream(cin));

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1400 total points
ID: 26213253
You could do the following, but 50 bytes is a strange figure - let's hope it's not a standard block cipher


DataInpuStream din = new DataInputStream(zin);
byte[] data = new byte[50];
din.readFully(data);

Open in new window

0
 
LVL 5

Author Comment

by:Tuxx
ID: 26213301
@CEHJ hmm.. Thank you very much for that. What I wanted is for it to read only a certain amount of bytes, so I used 50 as an example. I shall try your code and see that it works.
0
 
LVL 5

Author Comment

by:Tuxx
ID: 26275567
@CEHJ When I looked at your possible solution, I saw you were using another type of InputStream that I did not use. So it gave me the idea to use another stream, I did this, and hopefully it will work.
fis = new java.io.FileInputStream(in);
byte[] data = new byte[fis.available()];
fis.read(data);
byte[] data2 = java.util.Arrays.copyOfRange(data, 0, 50);
java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(data2);
// ^^Stream containing first 50 bytes of fis

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 26277243
I would just use the exact code i gave you
0
 
LVL 5

Author Comment

by:Tuxx
ID: 26277894
With  a minor adjustment, this is the code that worked.
fis = new java.io.FileInputStream(in);
byte[] data = new byte[fis.available()];
fis.read(data);
byte[] data2 = java.util.Arrays.copyOfRange(data, 0, 49);
java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(data2);
// ^^Stream containing first 50 bytes of fis

cin = new javax.crypto.CipherInputStream(bais, pbeCipher);
zin = new java.util.zip.ZipInputStream(new java.io.BufferedInputStream(cin));

Open in new window

0
 
LVL 5

Author Closing Comment

by:Tuxx
ID: 31674288
Thanks for the tips guys!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 26278242
That will work, but is less than optimal - you read the whole file then read the first bit again
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question