Wednesday, January 20, 2010

In C, C++, we have the option to make FILE pointer to point the last byte and make reverse read by decreasing pointer position. However, we do not have option to do the same in Java using pre-existing InputStream or Reader in JAVA SE.

One of the classic cryptography uses reverse the file content and share to the receiver. Receiver also needs to reverse the content and understand the meaning. If we would like to do the same in computer, reversing algorithm useful. However, just reversing will not help we have several mechanism to protect the message over network in modern cryptography.

If we ask our friends to write reversing program, most of them will write a program, in which content first read and put it into bytes or character array and then reverse order will be stored in another byte or character array. This program perform not good for bigger files.

Reader content reversing

To reverse content from java.io.Reader, we do not require char array. We can use StringBuilder which helps us to append/insert content in any index place. Read character one by one from Reader and insert at ZERO location, which will reverse the content.

InputStream content reversing

In InputStream, we have an API available() which gives the content length. byte array will be created using this length. Read bytes from this InputStream and store in byte array with reverse order

0
comments:

Post a Comment

Recent Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com