1

对社区的问候最近我在java项目中提出了序列化和反序列化问题。我有一个对象包含其他对象作为字段如何序列化一个java对象内部不包含Serializable字段为字节数组并反序列化数组以获取原始对象

我想对象的状态存储到一个字节数组然后反序列化的字节数组,并返回原来的对象不过,由我对象的字段不可序列化对象(来自第三党的图书馆)所以不得不首先宣布它们是暂时的。

现在我的对象是序列化和反序列化的,但是由于我提到过的暂态声明它的字段为空,我尝试在本地创建Serialization类中的所有元素,并为它们分配原始的价值观并继续这一过程,但它没有任何区别。我引用我的代码的一部分,任何想法?由于事先:)

这里是类我的对象与它的领域

public class AbePublicKey implements java.io.Serializable{ 

private static final long serialVersionUID = 7526472295622776147L; 
public transient Element g; 
public transient Element h; 
public transient Element f; 
public transient Element e_g_g_hat_alpha; 
} 

这里是我的串行功能

public byte[] PublicKeytoByteArray(AbePublicKey publickey) throws IOException { 

    KeyAuthority keyauthority = new KeyAuthority(); 
    byte[] bytes = null; 
    ByteArrayOutputStream bos = null; 
    ObjectOutputStream oos = null; 
    publickey.setElements(g, h, f, e_g_g_hat_alpha); 

    try { 
     bos = new ByteArrayOutputStream(); 
     oos = new ObjectOutputStream(bos); 
     oos.writeObject(publickey); 
     oos.flush(); 
     bytes = bos.toByteArray(); 

    } finally { 
     if (oos != null) 
      oos.close(); 
     } 
     if (bos != null) { 
      bos.close(); 
     } 

    } 

    return bytes; 
} 

这里是我的解串器功能

public static AbePublicKey PublicKeyBytestoObject(byte[] publickeybytes) throws IOException, ClassNotFoundException { 
    AbePublicKey obj = null; 
    ByteArrayInputStream bis = null; 
    ObjectInputStream ois = null; 
    try { 
     bis = new ByteArrayInputStream(publickeybytes); 
     ois = new ObjectInputStream(bis); 
     obj = (AbePublicKey) ois.readObject(); 

    } finally { 
     if (bis != null) { 
      bis.close(); 
     } 
     if (ois != null) { 
      ois.close(); 
     } 
    } 
    return obj; 
} 
+0

可能重复的[Java Custom Serialization](http://stackoverflow.com/questions/7290777/java-custom-serialization) – sidgate

回答

1

如果你想控制一个对象是如何序列化的,实现Externalizable接口和相关的readExternal和writeExternal方法。这使您可以完全控制对象序列化的方式。

很明显,你不能序列化一个包含不可序列化字段的类。但是你也许可以编写足够的数据来自己重新创建对象。

+0

感谢您的提示! – NickAth

0

如果您能够将需要的值复制到类中的新Serializable CustomElement对象中,它应该有所作为。使用复制构造函数(如果有的话),或者如果你有足够的信息可以反射。

0

您可以将Element字段包装在Serializable的类中,以便您可以编写它们。此解决方案假设您可以在读取它之后调用必要的setter或构造函数来重新创建Element

下面是一个例子:

非常基本的元素,是不是Serializable

public class Element { 

    private String value; 

    public Element(){ 
     value = null; 
    } 

    public Element(String value){ 
     setValue(value); 
    } 

    public void setValue(String value){ 
     this.value = value; 
    } 

    public String getValue(){ 
     return value; 
    } 
} 

现在一个非常基本的包装类,是Serializable

import java.io.IOException; 
import java.io.ObjectStreamException; 
import java.io.Serializable; 

public class SerializableElement implements Serializable{ 

    // Generated ID 
    private static final long serialVersionUID = -6751688345227423403L; 

    private transient Element element; 

    public SerializableElement(Element el) 
    { 
     element = el; 
    } 

    private void writeObject(java.io.ObjectOutputStream out) 
      throws IOException{ 
     out.writeObject(element.getValue()); 
    } 
    private void readObject(java.io.ObjectInputStream in) 
     throws IOException, ClassNotFoundException{ 
     String elementValue = (String)in.readObject(); 
     element = new Element(elementValue); 
    } 
    private void readObjectNoData() 
     throws ObjectStreamException{ 
     element = null; 
    } 

    public Element getElement(){ 
     return element; 
    } 
} 

最后一个主类运行序列化和反序列化逻辑(稍微修改你发布的内容):

import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 

public class SerializeMain { 

    public static void main(String[] args) { 
     SerializableElement serializableElement = new SerializableElement(
       new Element("test value")); 

     try { 
      byte[] serializedData = storeElement(serializableElement); 
      SerializableElement loadedElement = loadElement(serializedData); 
      System.out.println("loadedElement.element.value: " 
        + loadedElement.getElement().getValue()); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } 

    } 

    public static byte[] storeElement(SerializableElement sElement) 
      throws IOException { 
     byte[] bytes = null; 
     ByteArrayOutputStream bos = null; 
     ObjectOutputStream oos = null; 

     try { 
      bos = new ByteArrayOutputStream(); 
      oos = new ObjectOutputStream(bos); 
      oos.writeObject(sElement); 
      oos.flush(); 
      bytes = bos.toByteArray(); 

     } finally { 
      if (oos != null) { 
       oos.close(); 
      } 
      if (bos != null) { 
       bos.close(); 
      } 

     } 

     return bytes; 
    } 

    public static SerializableElement loadElement(byte[] byteData) 
      throws IOException, ClassNotFoundException { 
     SerializableElement obj = null; 
     ByteArrayInputStream bis = null; 
     ObjectInputStream ois = null; 
     try { 
      bis = new ByteArrayInputStream(byteData); 
      ois = new ObjectInputStream(bis); 
      obj = (SerializableElement) ois.readObject(); 

     } finally { 
      if (bis != null) { 
       bis.close(); 
      } 
      if (ois != null) { 
       ois.close(); 
      } 
     } 
     return obj; 
    } 
} 
相关问题