4

I am trying to develop a team management software. I have a Team object:

public class Team implements Serializable{
    private String name;//
    private ArrayList<Player> teamMembers;//
    private String sport;//
    private ArrayList<Staff> staff;//Non-Player Members such as head coach, assisstant coach, physio, etc.
    private Object schedule;
    private String teamHometown;
    private String teamState;   

  // Getter and setter methods go here

    public Team(String name, String sport, ArrayList<Player> players, ArrayList<Staff> staff){
        teamMembers = players;
        this.staff = staff;
        this.sport =(sport);
        this.name = (name);

    }

}

The team object contains an ArrayList of Player Objects. In each Player object the player has many attributes such as a name, stats, and a player photo(a Buffered Image):

public class Player implements Serializable{

    private BufferedImage playerPhoto;
    private String firstName;
    private String lastName;
    private int jerseyNumber;
    private String playerPosition;
    private String status;//Is the player Eligible to plays
    private int gameAbscenses;
    private int goals;
    private int appearances;
    private int yellowCards;
    private int redCards;
    private LocalDate birthday;
    private boolean starter;
    private int shots;
    private int shotsOnGoal;
    private int assists;

   //Getter and Setter Methods go here

    public Player(String firstName, String lastName, int jerseyNumber, String playerPosition,BufferedImage playerPhoto) {
        this.firstName = (firstName);
        this.lastName = (lastName);
        this.jerseyNumber = (jerseyNumber);
        this.playerPosition = (playerPosition);
        this.gameAbscenses = (0);
        this.status = ("Healthy");
        this.starter = false;

        //stats
        this.shots = (0);
        this.appearances = (0);
        this.shotsOnGoal = (0);
        this.redCards = (0);
        this.yellowCards = (0);
        this.assists =  (0);
        this.goals = (0);

        this.birthday = LocalDate.now();

    }

My goal is to serialize an arrayList of Team objects however Buffered images aren't serializable and since the Player object contains a buffered image as one of its private instance variables I can't serialize the ArrayList of Team objects.

I originally tried doing this:

 ArrayList<Team> finalTeams = new ArrayList<>(SignInController.userTeams);//Converting the user teams Observable list to an Array List

            //Saving the user's Teams locally

            FileOutputStream fos = new FileOutputStream(SignInController.userfname+"'s Teams/"+SignInController.userfname+"_teams.xtm");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(finalTeams);//serializing the ARRAYlist of teams
            oos.close();
            fos.close();

However, it didn't work. I also tried looking at other stack overflow questions that related to serializing Buffered Images but nothing was able to help me.

If someone could explain to me what I am doing wrong that would be awesome! Thanks!

xkunle97
  • 43
  • 3
  • If buffered image is serializable you shouldn't have any problem, and if it's not, it mean that you can serialize the object only if you declare this field as `transient` (meaning - it will not be serialized and this information will not pass across the wire). – Nir Alfasi Nov 24 '16 at 00:03
  • Yeah I know but that is what I did not want to happen. I wanted to know if the BufferedImage could be serialized somehow – xkunle97 Nov 24 '16 at 00:22

2 Answers2

2

yes u can, and why not?! but we will follow un-straight method and it worked fine with me

  1. it's known that primitive types and array of primitive types are all Serializable
  2. it's also known that we can convert a buffered image instance to a byte array and the inverse is also applicable
  3. so instead of serializing a buffered image we will serialize a byte array that represents this buffered image

I have created a demo that simulate your question

import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
public class Member implements Serializable
{

    private String name;
    private byte[] imagebytes;

    public Member(String name, BufferedImage image) throws IOException
    {
        this.name = name;
        this.setImage(image);
    }

    public Member(String name, File imageFile) throws IOException
    {
        this.name = name;
        this.setImage(imageFile);
    }

    public final void setImage(BufferedImage image) throws IOException
    {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", outputStream);
        this.imagebytes = outputStream.toByteArray();
    }

    public final void setImage(File imageFile) throws IOException
    {
        BufferedImage bufferedImage = ImageIO.read(imageFile);
        this.setImage(bufferedImage);
    }


    public BufferedImage getImage()
    {

        try
        {
            return ImageIO.read(new ByteArrayInputStream(imagebytes));
        } catch (Exception io)
        {
            return null;
        }
    }

    public String getName()
    {
        return name;
    }

} 

you can add methods as u like and customize this class as u need i hope this answer is useful

Anas
  • 688
  • 7
  • 24
0

As per the javadocs

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

 private void writeObject(java.io.ObjectOutputStream out)
     throws IOException
 private void readObject(java.io.ObjectInputStream in)
     throws IOException, ClassNotFoundException;
 private void readObjectNoData()
     throws ObjectStreamException;

So maybe extend BufferedImage with your own classes and implement the above methods

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64