aspose file tools*
The moose likes I/O and Streams and the fly likes load Properites from ByteArrayInputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "load Properites from ByteArrayInputStream" Watch "load Properites from ByteArrayInputStream" New topic
Author

load Properites from ByteArrayInputStream

Cindy Cindy
Greenhorn

Joined: Feb 08, 2002
Posts: 3
I'm trying to decrypt an encrypted property file and then load it. The file is decrypted using a CipherInputStream and read into a byte array. I'm then attempting to use a ByteArrayInputStream to load the properties file. When the load method tries to read a line, it returns null. If I write the byte array to a FileOutputStream, read it into a FileInputStream, and load it, it works fine. However, that defeats the purpose of encrypting the file in the first place.
Why can't I load a ByteArrayInputStream? How can I convert the byte array into the appropriate format so I can load the properties?
FileInputStream fisCipher = new FileInputStream("test.props");
byte[] decryptedBytes = Cryptography.decryptInfo(fisCipher);
fisCipher.close();
ByteArrayInputStream bis = new ByteArrayInputStream(decryptedBytes);
props.load(bis);
bis.close();
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Dunno. I'd try writing the byte array to a file as an intermediate step, so you can look at the file and see if it looks OK. Also you can print out the bytes, as characters and as numbers, as an aid in verifying they're correct:

If you don't see a valid-looking properties file, then you'll want to focus on debugging the Cryptography class.


"I'm not back." - Bill Harding, Twister
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Cindy G:

byte[] decryptedBytes = Cryptography.decryptInfo(fisCipher);
fisCipher.close();


I'm not familiar with the Cryptography class...is that a custom class? You're calling a static method decryptInfo?
What exactly is it returning? Is it returning a new byte[] instance, or is it returning the buffer internal to the Cryptography class? If it's returning the internal buffer, won't that get clobbered when you call fisCipher.close()?


Rob
SCJP 1.4
Cindy Cindy
Greenhorn

Joined: Feb 08, 2002
Posts: 3
In response to the two previous posts...
I'm using the Java Cryptology Extension and am using the CipherOutputStream in ENCRYPT_MODE to write an encrypted properties file. I'm then calling my own static method, decryptInfo() which uses a CipherInputStream in DECRYPT_MODE to decrypt the file. I'm using the read(byte[]) method in CipherInputStream which decrypts the file into a byte array. Then I'm trying to use a ByteArrayInputStream to load (Properties.load()) that byte array. The Properties.load() method returns null when it tries to read the first line.
I have tried to write the byte array to a file and read it in again. The format is fine and I can then load the properties. However, writing the decrypted contents to a file to read it back in somewhat defeats the purpose of encrypting it in the first place.
Any ideas?
Cindy Cindy
Greenhorn

Joined: Feb 08, 2002
Posts: 3
The problem is fixed (thanks Rob). I was closing the file too early. I thought it was OK since I still had data in the byte array and could later right the byte array to a file. Apparently not.
FileInputStream fisCipher = new FileInputStream("test.props");
byte[] decryptedBytes = Cryptography.decryptInfo(fisCipher);
ByteArrayInputStream bis = new ByteArrayInputStream(decryptedBytes);
props.load(bis);
bis.close();
fisCipher.close();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: load Properites from ByteArrayInputStream
 
Similar Threads
Dynamically reading a file
Reading a properties file using BufferdInputStream
What exactly is BufferedInputStream or BufferedOutputStream
Convert java.io.File to ByteArryInputStream?
Reading file into byte array