Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
harke baj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic