serialization - BufferedImage Read from file using ObjectStream is null -


description

I am reading a theme object from a file, the list of buffer images in the theme category Is transient, but uses a custom read / write object method that reads and writes ImageIO. The problem is that the first time a buffer image is read in a theme (it is not empty), but the rest is zero, I think there might be something wrong with the type of object that you write, but what?

The program runs trough folders and creates a theme that is located in that folder. Nothing wrong in the images, I have checked using different images, but the result is the same.


DataSetup
  Public class dataset {public zero type () {list & lt; Theme & gt; Themes = getThemes (new file ("data")); {FileOutputStream fos = Try New FileOutputStream ("data / campaign.dat"); ObjectOutputStream OOS = New ObjectOutputstream (fos); For (theme theme: topics) {oos.writeObject (subject); } Oos.close (); } Grip (IOException E) {// TODO Auto-Generated Catch Block e.printStackTrace (); }}}   

theme class
  package data; Import java.awt.image.BufferedImage; Import java.io.IOException; Import java.io.ObjectInputStream; Import java.io.ObjectOutputStream; java.io.Serializable import; Import java.util.ArrayList; Import java.util.list; Import javax.imageio.ImageIO; Public class theme implementation serializable {Private Static Finals Long Serialvarsion UID = 1 L; The name of the private string; Personal transient list & lt; BufferedImage & gt; bufferedImages = new arreelist & lt; BufferedImage (); Public Theme (String Name) {this.name = name; } Private Zero Writing Object (object overputstream OOS) throws IOException {oos.defaultWriteObject (); oos.writeInt (bufferedImages.size ()); (Buffered Image By: Buffered Images) {// Image Type JPG Images: Written (BI, Data. IMUTEE, OOS); } Oos.close (); } Private Zero Read Object (Object Intestish OIS) throws IOException, Class Notfound Expression {ois.defaultReadObject (); Final integer COUNT COUNT = ois.readInt (); BufferedImages = New Array's & lt; BufferedImage & gt; (COUNT); {BufferedImages.add (imageIO.read (ois)) for (Int i = 0; I & lt; COUNT; i ++); } System.out.println ("Check for theme" + getName ()); (Buffer image BI: Buffer images) {System.out.println ("Image is tap" + (BI == empty); } Ois.close (); } Public Zero addBufferedImage (BufferedImage bi) {bufferedImages.add (bye)}} Public BufferedImage getBufferedImage (Int Index) {return bufferedImages.get (index);} Public string getName () {Return name;} Public int getSize () Checking animal image is empty = image is true = true image tap = {return to befarred images ();}}   

output
  Truth is image tap = true image taps = true is checking for theme Joker Image is zeros / false image tap = true image tap = true image no = The image of the correct theme mountain is checked for zero = false image faucet = image of the true theme space = False image tap = true image taps = true    

I think this is a known issue

There is no guarantee that ImageReader is original You will read many bytes in the form of the form ImageWriter . Understanding efficiently (due to buffering, it can be written as an author, more than the stream, more ), it will be read as a bytes. This can lead to a "wrong alliance", and the next reading will fail.

The solution is to buffer each write, then, before the actual image bytes, write the length (byte count) for each image, or write the buffed byte arrays.

While reading back, make sure that the number of bytes you have used, the more you use the bytes to read or leave the required number of extra bytes.

To write, you can use the same code copy:

  BufferedImage image = null; // Your image BitReon Output stream buffer stream = new byteOutStream (); ImageIO.write (image, "JPEG", buffer stream); Byte [] bufferedBytes = Buffer stream. Broken (); Type the object overputstream as an object, or write the buff.bits. To read raw bytes   

as small + buffered bytes:

  byte [] Bytes = ...; // ObjectInputStream from BufferedImage image = ImageIO.read (new bytereinputstream (bytes));    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -