aspose file tools*
The moose likes I/O and Streams and the fly likes How to read from a text file, a buffer at a time (specific bytes at a time)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to read from a text file, a buffer at a time (specific bytes at a time)?" Watch "How to read from a text file, a buffer at a time (specific bytes at a time)?" New topic
Author

How to read from a text file, a buffer at a time (specific bytes at a time)?

harke baj
Greenhorn

Joined: Feb 02, 2010
Posts: 28
Hi,

Could someone tell me how I can read a text file, a buffer at a time? I want to read certain bytes at a time.

I wanted to encrypt a text file using RSA ( just for knowledge sake ), using 1024 key size. But the size of the message to encrypt is limited. I can't go on increasing the key size as the process become super slow.

I was wondering if I could read certain bytes at a time, encrypt them and write the encrypted data to a file.

Like so:


also should I use the doFinal() or the updata()? Can someone tell me a better way to do this, if there is another way?

Thank you.

regards,
harke
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

InputStream has read() methods that take a byte[]. They will read up to the array length in bytes, and return the number of bytes read. You can use those two (the array and the number of bytes read) in the update() method. doFinal() should be called when you've called update() with all read data already, to get the remainder.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

harke baj wrote:should i put the doFinal() immediately after the update(), or after the loop finishes?

It's called doFinal; doesn't that say enough?

Also, when should I write the encoded data to a file.

I would do it in between. In pseudo code:
harke baj
Greenhorn

Joined: Feb 02, 2010
Posts: 28
I have done it.

But not using the update(). I read the files in chunks and then used doFinal().



But I still could not get, how to use the update().
Could you direct me to some examples?

thanks you
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

You're calling doFinal() each time so you are not encrypting the entire file; you are encrypting each chunk as if it were a separate entity. This will lead to problems if the cipher adds padding to make the final result of a certain length (like a multitude of 8 bytes). The update uses intermediate results, and does not add padding. You will only get that padding with doFinal(). In short:
harke baj
Greenhorn

Joined: Feb 02, 2010
Posts: 28
this did not work.
I go the following error:


my code:



The key size i used to generate the keys, is 1024.

Thanks,
harke
harke baj
Greenhorn

Joined: Feb 02, 2010
Posts: 28
hey i solved the problem. The problem was because i was using wrong byte size. I change the byte[] to 100 from 50. and it worked.\

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to read from a text file, a buffer at a time (specific bytes at a time)?