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; 
    } 
} 
相關問題