1 package org.sapia.ubik.net.udp; 2 3 import java.io.*; 4 5 import java.net.DatagramPacket ; 6 7 8 16 public class Util { 17 20 public Util() { 21 super(); 22 } 23 24 public static Object fromDatagram(DatagramPacket pack) 25 throws IOException, ClassNotFoundException { 26 ByteArrayInputStream bis = new ByteArrayInputStream(pack.getData(), 27 pack.getOffset(), pack.getLength()); 28 ObjectInputStream ois = null; 29 30 try { 31 ois = new ObjectInputStream(bis); 32 33 Object o = ois.readObject(); 34 35 return o; 36 } finally { 37 if (ois != null) { 38 ois.close(); 39 } 40 } 41 } 42 43 public static byte[] toBytes(Object o, int bufsize, ObjectStreamFactory fac) 44 throws IOException { 45 ByteArrayOutputStream bos = new ByteArrayOutputStream(bufsize); 46 ObjectOutputStream ous = fac.toOutput(bos); 47 48 ous.writeObject(o); 49 ous.flush(); 50 ous.close(); 51 52 return bos.toByteArray(); 53 } 54 55 public static int getSizeInBytes(Object o) throws IOException { 56 return toBytes(o, 1000, ObjectStreamFactory.DEFAULT_FACTORY).length; 57 } 58 } 59 | Popular Tags |