Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6075271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:28:33+00:00 2026-05-23T10:28:33+00:00

I have a collection of objects: Map<BufferedImage, Map<ImageTransform, Set<Point>>> map I want to write

  • 0

I have a collection of objects:

Map<BufferedImage, Map<ImageTransform, Set<Point>>> map

I want to write those to a file, and then be able to read them back in the same struct.


I can’t just write the collection as it is, because BufferedImage doesn’t implement the Serializable (nor the Externalizable) interface. So I need to use the methods from the ImageIO class to write the image.

ImageTransform is a custom object that implements Serializable. So, I believe the value part of my map collection, should be writeable as it is.


Here is what I do to write to the file:

    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
    for (BufferedImage image : map.keySet()) {
        ImageIO.write(image, "PNG", out);  // write the image to the stream
        out.writeObject(map.get(image));   // write the 'value' part of the map
    }

Here is what I do to read back from the file:

    ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
    while(true) {
        try {
            BufferedImage image = ImageIO.read(in);
            Map<ImageTransform, Set<Point>> value = 
                (Map<ImageTransform, Set<Point>>) in.readObject(); // marker
            map.put(image, value);
        } catch (IOException ioe) {
            break;
        }
    }

However, this doesn’t work. I get a java.io.OptionalDataException at marker.

java.io.OptionalDataException
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1300)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:368)

My question is, firstly, is the writing concept correct ? is ImageIO#write good for this case, or should I think about using/storing the BufferedImage#getRgb int[] array ? is the array more compact (as in, takes up less space in the file) ?
Secondly, how should I be reading the object back from the file ? How do I know when the EOF is reached ? Why doesn’t the above work ?

I hope the info provided is enough, if you need more info on something, please tell me.
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:28:34+00:00Added an answer on May 23, 2026 at 10:28 am

    It’s not working as ObjectOutputStream and ObjectInputStream write/expect a certain file format that is violated when you write an image out of order. To use ObjectStreams successfully you will need to observe the contract that is specifed by ObjectStreams.

    To do this you will need to create a holding class, and use this class as the key to your map instead of BufferedImages. This holding class should implement Serializable and a three methods (not in any actual interface) that mark the Class as needing special handling during reading and writing. The method signatures must be exactly as specified or serialization won’t work.

    For more information have a look at the documentation on ObjectOutputStream.

    public class ImageHolder implements Serializable {
    
        BufferedImage image;
    
        public ImageHolder(BufferedImage image) {
            this.image = image;
        }
    
        private void readObject(ObjectInputStream stream) 
                throws IOException, ClassNotFoundException {
            image = ImageIO.read(stream);
        }
    
        private void writeObject(ObjectOutputStream stream) 
                throws IOException {
            ImageIO.write(image, "PNG", stream);
        }
    
        private void readObjectNoData() throws ObjectStreamException {
            // leave image as null
        }
    

    And then serialsation should be as simple as outputStream.writeObject(map). Though you will need to check that the implementing class of ImageTransform is serialisable too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Collection objects, I want to associate each object of these two
I have a collection of objects that is modified by one thread and read
I have a collection of the form: map<key, list<object> > I only ever insert
In my application, I have three collection objects which store data. The data which
I have a collection of objects to which I'd like to just add a
I have a collection of objects and am curious about the way you would
I have a collection of objects which describe an image-name, its size and it's
I have a collection of objects of the same type, let's call it DataItem
So I have a collection of objects. The exact type isn't important. From it
C#: I have a collection of objects . T has 2 properties. Property A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.