1 29 package net.metanotion.io; 30 31 import java.io.ByteArrayInputStream ; 32 import java.io.ByteArrayOutputStream ; 33 import java.io.DataInputStream ; 34 import java.io.DataOutputStream ; 35 import java.io.IOException ; 36 37 import net.metanotion.io.Serializer; 38 39 public abstract class SerialStreams implements Serializer { 40 public byte[] getBytes(Object o) { 41 try { 42 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 43 DataOutputStream dos = new DataOutputStream (baos); 44 writeOut(dos, o); 45 return baos.toByteArray(); 46 } catch (IOException ioe) { throw new Error (); } 47 } 48 49 public Object construct(byte[] b) { 50 try { 51 ByteArrayInputStream bais = new ByteArrayInputStream (b); 52 DataInputStream dis = new DataInputStream (bais); 53 return readIn(dis); 54 } catch (IOException ioe) { 55 ioe.printStackTrace(); 56 throw new Error (); 57 } 58 } 59 60 abstract public void writeOut(DataOutputStream dos, Object o) throws IOException ; 61 abstract public Object readIn(DataInputStream dis) throws IOException ; 62 } 63 | Popular Tags |