1

In my project I have a class which often needs to be serialized to byte array. I currently have a constructor in my class which takes the array, parses it and creates a new object. Once it is done, the constructor reads needed fields from that (new) object and sets appropriate values in the class.

public class MyClass implements Serializable {
  private int fieldOne;
  private boolean fieldTwo;
  ...

  // This is default constructor
  public MyClass(){
  }

  // This is the constructor we are interested in
  public MyClass(byte[] input){
    MyClass newClass = null;

    try(ByteArrayInputStream bis = new ByteArrayInputStream(input);
         ObjectInput in = new ObjectInputStream(bis)) {
          newClass = (MyClass) in.readObject();
      } catch (ClassNotFoundException | IOException e) {
          e.printStackTrace();
      }
    if (newClass != null) {
      this.fieldOne = newClass.getFieldOne;
      this.fieldTwo = newClass.getFieldTwo;
      ...
    }
  }

  public int getFieldOne(){
    return fieldOne;
  }
  public boolean getFieldTwo(){
    return fieldTwo;
  }
  ...
}

Code like this works correctly, but the question is: Is it possible to create (with that constructor) MyClass object directly, without creating that "newClass" instance and setting all values manually?

1
  • Why do you need it to be a constructor? You could just make it a static method and simply return newClass. Commented Mar 18, 2016 at 12:39

2 Answers 2

1

No this is not possible.

But instead of a constructor MyClass(byte[]) which then creates two MyClass objects, you can introduce a static factory method:

public static MyClass create(byte[] input) {
    try(ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(input))) {
        return (MyClass)in.readObject();
    }
    catch (Exception e) {
        throw new IllegalStateException("could not create object", e);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You should not be deserializing your object like that, rather implement readObject as the specification indicates:

private void readObject(ObjectInputStream in)
    throws IOException, ClassNotFoundException {

    in.defaultReadObject();

    // custom
    this.fieldOne = in.readXXX();
    this.fieldTwo = in.readXXX();
}

And this is specifically for custom serialization, why don't you use the api directly, or make a static method for retrieving an object:

public static MyClass readFromByteArray(byte[] input) {
    Myclass obj = null;

    try (ByteArrayInputStream bis = new ByteArrayInputStream(input);
        ObjectInputStream ois = new ObjectInputStream(bis)) {
        obj = (MyClass) in.readObject();
    } catch (ClassNotFoundException | IOException e) {
        e.printStackTrace();
    } 

    return obj;   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.