2010-12-14 13 views
1

Ich muss die gleiche Operation aber in einem Strom zu tun. Können Sie mir bitte helfen?Serialisierung und Archivierung Objekt

public static byte[] archivingAndSerialization(Object object){ 

    ByteArrayOutputStream serializationByteArray = new ByteArrayOutputStream(); 
    ByteArrayOutputStream archvingByteArray = new ByteArrayOutputStream(); 
    try { 

    ObjectOutputStream byteStream = new ObjectOutputStream(serializationByteArray); 
    byteStream.writeObject(object); 
    ZipOutputStream out = new ZipOutputStream(archvingByteArray); 
    out.putNextEntry(new ZipEntry("str")); 
    out.write(serializationByteArray.toByteArray()); 
    out.flush(); 
    out.close(); 

    } catch (IOException e) { 
logger.error("Error while IOException!", e); 
    } 
    return archvingByteArray.toByteArray(); 
} 

Antwort

1

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
ObjectOutputStream oos= new ObjectOutputStream(new DeflatingOutputStream(baos)); 
oos.writeObject(object); 
oos.close(); 
return baos.toByteArray(); 

Hinweis Versuchen: Es sei denn, das Objekt zu groß ist mittelgroß, es komprimiert wird es größer machen, als es einen Header hinzuzufügen. ;)

Um

ObjectInputStream ois = new ObjectInputStream(
    new InflatorInputStream(new ByteArrayInputStream(bytes))); 
return ois.readObject(); 
+0

Dank deserialisieren, half man mir viel – darkAngel