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

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.
LVL 5
TuxxAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
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
 
CEHJCommented:
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
 
CEHJCommented:
You might OTH be better off using RandomAccessFile
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
TuxxAuthor Commented:
@CEHJ I will check out your suggestion.
0
 
FrellingConnect With a Mentor Commented:
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
 
CEHJCommented:
>>@CEHJ I will check out your suggestion.

OK. If you give us some more details i can give you a specific solution probably
0
 
TuxxAuthor Commented:
@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
 
TuxxAuthor Commented:
@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
 
TuxxAuthor Commented:
@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
 
CEHJCommented:
I would just use the exact code i gave you
0
 
TuxxAuthor Commented:
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
 
TuxxAuthor Commented:
Thanks for the tips guys!
0
 
CEHJCommented:
That will work, but is less than optimal - you read the whole file then read the first bit again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.