Java library of serialization without using constructors without arguments and Serializable implementation

Is there any way in the java world for serializing without using constructors without arguments and implementing Serializable?

+3
source share
9 answers

JBoss Serialization is a replacement for the standard standard Java serialization, which does not require implementation java.io.Serializable. Besides this (and the fact that it is much faster), it is the same as the standard serialization mechanism (it even uses the same interfaces ObjectInputand ObjectOutput.

P.S. JBoss, JBoss, .

+2
+1

, , Externalizable , .

.

+1

, , . , , , .

, no-arg, . , Serializable 30 /.

, ?

0

... Serializable?

, . Serializable java.io.Serializable. , ObjectInputStream/ObjectOutputStream .

, :

public static byte[] toByteArray(Object object) throws IOException {
        if (!isSerializable(object)) {
            throw new IOException("Object '" + object.getClass().getName() + "' is not serializable.");
        }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(baos);
        oos.writeObject(object);
        oos.flush();
    } finally {
        if (oos != null) {
            try {
                oos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                logger.error("Closing of ObjectOutputStream failed.", e);
            }
        }
    }

    return baos.toByteArray();
}

public static Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
    Object object = null;
    ObjectInputStream ois = null; 

    try {
        ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
        object = ois.readObject();
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                logger.error("Closing of ObjectInputStream failed.", e);
            }
        }
    }

    return object;
}
0

Eishay Smith Java, , , no-arg ( , ). .

0

Databoard, bean -style, record-style immutable-style. .

0

, . Java writeObject readObject, , , Serializable writeObject readObject, , .

0

. http://www.jguru.com/faq/view.jsp?EID=251942.

, Serializable, , .

  • Serializeble without a constructor without arguments, since extends Objectwith a constructor without arguments

    public class MySerializableClass implements Serializable {
       public MySerializableClass (...)...
    }
    
  • Serializeble without a constructor without arguments, since extends MyFirstClasswith a constructor without arguments

    public class MyFirstClass {
    }
    public class MySecondClass extends MyFirstClass implements Serializable {
       public MySecondClass (...)...
    }
    
  • NOT serializeble as MyFirstClassdoes not implement SerializableAND does not have a default constructor.

    public class MyFirstClass {
       public MyFirstClass (...)...
    }
    public class MySecondClass extends MyFirstClass implements Serializable {
       public MySecondClass (...)...
    }
    
0
source

Source: https://habr.com/ru/post/1773630/


All Articles