• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

ByteArrayIn/OutputStream

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I want to save a BufferedImage to hard disk, as well as transmission across a network. Since it isn't serializable I want to shove it into a byte array using the ByteArrayOutputStream, and read it back as a ByteArrayInputStream. I tried simply using ImageIcon instead, but the file-size is very large when I serialize it.

Anyways, I was stuck and found some sample code a nice guy called Christen posted called "WrapBufferedImage". I wrote a test harness around it and tested it but can't get it to work correctly. I now believe I understand it, and could write it from scratch myself, but I cannot see the flaw in it so feel it would be a waste of time until I can get this to work.

The test harness loads a picture into a JFrame, and saves this image as a byte array from a BufferedImage. Then you click the LoadBack button and it is supposed load the saved file back into a BufferedImage and display it. Unfortunately it only displays about 5% of the image, and the rest looks corrupt. I did a System.out.print of the byte array immediately after it was created and everything seems fine, but when I do the same as soon as it's loaded back, 95% of the array are zeroes.

So I can presume that either the array isn't saving correctly (though as I say, its being created fine), or the data is on the hard disk ok, it just isn't loading it back fully.

I'll post the code, the main class here is the WrapBufferedImage, whereas the test harness is MyFrame. If anyone could just paste this into their editor and have a quick look it'd be great. This has been frying my head for a week now.

Thanks in advance.

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try closing your output stream when you write the object out. I didn't see where you were closing the stream.
 
Jase Jones
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the advice,

I tagged on in WrapBufferedImage, but there wasn't a difference unfortunately.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't close your stream in your writeObject() method. I'd close it after you call it...


That, however, won't solve your problem. Make sure it's actually filling up your byte array when you read in the data from your file. The read method returns an integer indicating how many bytes it actually read. That should be equal to your length, right?
 
Jase Jones
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point, and this does seem wierd.

I changed the

read(byte[])
to
read(byte[], offset, length)

where the length should be 86409, but the return value from read(..) is 1020. This would definately seem to be the problem. Do you think its a problem in the save section?
But I don't see how it would be reading only 1020 bytes if I'm specifically supplying 86409?, but at the same time, I can't see any problem in the save section either?
 
Jase Jones
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just threw the byte array into a basic class, saved the object to HDD, and it worked fine. Should have just done that from the start.

Thanks.
 
This is my favorite tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic