jQuery in Action, 2nd edition*
The moose likes I/O and Streams and the fly likes BufferedWriter and ByteArray Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "BufferedWriter and ByteArray" Watch "BufferedWriter and ByteArray" New topic
Author

BufferedWriter and ByteArray

Tom Griffith
Ranch Hand

Joined: Aug 06, 2004
Posts: 257
Hello. If anybody has a minute, I am coming up with blanks on this so far. What I am trying to do is write an input stream (say, an html page) to a new output stream that i want to keep in memory as a byte array. I keep getting nothing on it...if i stream it to a file at that point, it writes...but i need to do more stuff to thebyte array downstream so i'm hoping to keep it in memory...here's a rough schematic of the problematic streaming...

// ByteArray stores the HTML page in memory at this point...


InputStream is_reader = new ByteArrayInputStream(ByteArray);

BufferedReader rd = new BufferedReader(new InputStreamReader(is_reader));

ByteArrayOutputStream os_writer = new ByteArrayOutputStream();

BufferedWriter wtr = new BufferedWriter(new OutputStreamWriter(os_writer));

while ( (current_line = rd.readLine()) != null) {

wtr.write( current_line );

System.out.println(current_line);

// ALL THE HTML SOURCE CODE IS PRINTED TO THE CONSOLE..LOOKS GOOD

}


// NOW COMMIT THE OS_WRITER STREAM TO BYTES IN MEMORY...

byte [] ByteArray2 = os_writer.toByteArray();


// NOW PRINT WHATS IN THE BYTE ARRAY...


InputStream is_reader2= new ByteArrayInputStream(ByteArray2);

BufferedReader rd2 = new BufferedReader(new InputStreamReader(is_reader2));

ByteArrayOutputStream os_writer2 = new ByteArrayOutputStream();

BufferedWriter wtr2 = new BufferedWriter(new OutputStreamWriter(os_writer2));

while ( (current_line = rd2.readLine()) != null) {

wtr2.write( current_line );

System.out.println(current_line);

//NOTHING PRINTS...

}

THank you very much for reading this or whatever. Any input is really appreciated.
[ March 14, 2007: Message edited by: Tom Griffith ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8839
    
    7

Why are you constructing streams around the byte array instead of just dumping it into the output stream with OutputStream.write(byte[])?
[ March 14, 2007: Message edited by: Joe Ess ]

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Tom Griffith
Ranch Hand

Joined: Aug 06, 2004
Posts: 257
Hello. Thank you for your post. Yeah, it's hard to gauge how much code to post lest it get buried or not reveal enough. In a nutshell, most of that lump of streams and above is just troubleshooting so i could print out what's in the new byte array...line by line...the first loop reads all the html source code from the first byte array (Byte Array)...the second byte array (ByteArray2) has nothing...

This is a summary...

1) I am trying to stream an html page and save it locally to a new zip archive. I was able to do that via byte chunking but the saved htm was blank and i ~think~ i need a writer...so...

2) I threw the writer in (the posted code below) and am attempting to commit the writer output stream to a byte array (ByteArray2).

3) The byte array (ByteArray2) will then become the input stream for the zip archive entry...like this...

InputStream zip_in = new ByteArrayInputStream(ByteArray2);

4) I'm probably way off but I always viewed byte arrays as 'bridges' between streams. Like in this case, what was the output stream will become the next input stream and so on...which is why i was employing it.

In any case, the problem that I'm getting and i really don't know why is that nothing seems to be saved to ByteArray2 after the writer does it's thing...although i am printing out all the source html code in the same loop...
[ March 14, 2007: Message edited by: Tom Griffith ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The immediate cause of the problem you're having is that BufferedWriter, not surprisingly, buffers what you write -- it effectively caches it until it gets a big enough chunk to send to the underlying stream . When you call os_writer.toArray(), the ByteArrayOutputStream may not have seen all (or even any!) of the data yet; it could be in the BufferedWriter's buffer. Calling flush() on the BufferedWriter will send all the accumulated data happily on its way (as would closing the BufferedWriter, which would work just fine here too.)

One other thing: your readLine() calls will return individual lines, which you're writing to the BufferedWriter. The line separators in between the lines are just being discarded, which may mess up your whitespace. Note also that readLine() does a lot of completely unnecessary work. Why not just use read() to read data into a byte array?


[Jess in Action][AskingGoodQuestions]
Tom Griffith
Ranch Hand

Joined: Aug 06, 2004
Posts: 257
I think i went down the writer route because I wanted to print out what was in the byte array...and I do see the html source code. Then i kinda was hungup on thinking i needed a writer...but i think it's confirmed that no writer is needed judging from what i am reading. So i think i have to go back to the original problem...eliminating the writer jazz all together...and what i was doing initially...

1. I write the html page into a byte array (ByteArray2)...via httpurlconnection, GET, etc...(the writer above proved the html source code is there or whatever)...

2. I stream ByteArray2 into a zip entry (via ZipOutputStream)...and i open the html file from within the zip archive...i just get a blank screen in IE. The view-source has virtually nothing in it...but the size of the file inside the zip is ~5 mb, consistent with the size of the original html...so i ~think~ the data is ~there~...but i don't know where/how/what the bytes are...and why the web page isn't displaying...
[ March 14, 2007: Message edited by: Tom Griffith ]
Tom Griffith
Ranch Hand

Joined: Aug 06, 2004
Posts: 257
ok...this is the code to write the bytes into the zip entry...

FileOutputStream fos=new FileOutputStream("my_test.html");

fos.write(ByteArray2);

// THIS LOCAL FILE IS FOR TROUBLESHOOTING...AT THIS POINT...I OPEN my_test.html ON THE HARDDRIVE AND THE HTML TEXT DISPLAYS...THE IMAGES DON'T...BUT THATS THE NEXT PROBLEM...

// OK..NOW CAN I DUMP THIS SUCKER INTO A ZIP?

try {
// Create the ZIP file
String outFilename = "c:\\my_test.zip";
ZipOutputStream zip_os = new ZipOutputStream(new FileOutputStream(outFilename));


// Create a ZIP entry and add it to the output stream.
zip_os.putNextEntry(new ZipEntry("my_test.html"));

// Write the byte array to the output stream
zip_os.write(ByteArray2);

// Close the entry in the archive
zip_os.closeEntry();

// Close the ZIP archive
zip_os.close();

} catch (IOException e) {
}

// I OPEN THE ZIP ON THE HARDDRIVE...AND A BLANK HTML DISPLAYS FOR THAT ENTRY

So I can't figure out what goes wrong between writing the byte array to a local file and writing the same byte array into a zip entry...Thank you again for reading...
[ March 14, 2007: Message edited by: Tom Griffith ]
Danny Varghese
Greenhorn

Joined: Jul 21, 2005
Posts: 7
Hi Tom,

I may/may not be understanding your issue completely, but I believe your seeing the side affect of writing out an input stream. I've experienced a similar problem your having on a past project.

It seems the input stream gets side affected anytime it's read/written out. Suggestions are you place a "mark" and then "reset" the stream. However, I didn't have any luck with that.

I hope that gives a little more insight.
Tom Griffith
Ranch Hand

Joined: Aug 06, 2004
Posts: 257
Yeah...this is really weird. It just occured to me that i am being forced to use jvm 1.3...so i'm wondering if there might be a bug in this edition regarding the zipoutputstream. I even tried to stream the local html file into the zip entry and it's still a blank page. I'm going to try jvm 1.4 tomorrow. Thank you for reading...
Tom Griffith
Ranch Hand

Joined: Aug 06, 2004
Posts: 257
Yeah...this is really weird. It just occured to me that i am being forced to use jvm 1.3...so i'm wondering if there might be a bug in this edition regarding the zipoutputstream. I even tried to stream the local html file that i created above into the zip entry and it's still a blank page. I'm going to try jvm 1.4 tomorrow. Thank you for reading...
Tom Griffith
Ranch Hand

Joined: Aug 06, 2004
Posts: 257
Hi. It was the jvm...ran it in 1.4, no problem...so something was probably amiss with the ZipOutputStream in 1.3. Welp, that was a day and a half of tripping over streams and adding writers to troubleshoot just becasue the 1.3 lightbulb didn't go on. Thank you again everybody. Oh, I was using 1.3 becasue this is a lotus notes 6x application and it's bundled with 1.3...so I'm posting the "resolution" in case somebody just might be out there using lotus notes 6.5 and streaming to a zip.
[ March 15, 2007: Message edited by: Tom Griffith ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BufferedWriter and ByteArray
 
Similar Threads
java.sql.SQLException: ORA-01407:
Sound recording
BufferdReader/Writer problem
How to send comands to anoyther program
All SCJP gurus..... HELP - -- -IO