/** Create the serversocket and use its stream to receive serialized objects */ public static void main(String args[]) { ServerSocket ser = null; Socket soc = null; String str = null; Date d = null; try { ser = new ServerSocket(8020); /* * This will wait for a connection to be made to this socket. */ soc = ser.accept(); InputStream o = soc.getInputStream(); ObjectInput s = new ObjectInputStream(o); str = (String) s.readObject(); d = (Date) s.readObject(); s.close(); // print out what we just received System.out.println(str); System.out.println(d); } catch (Exception e) { System.out.println(e.getMessage()); System.out.println("Error during serialization"); System.exit(1); } }
public Object nativeToJava(TransferData transferData) { Object o = null; if (transferData == null) { getLog().error("transferData is null"); } if (isSupportedType(transferData)) { byte[] bs = (byte[]) super.nativeToJava(transferData); if (bs != null) { ByteArrayInputStream bis = new ByteArrayInputStream(bs); ObjectInput in; try { in = new ObjectInputStream(bis); o = in.readObject(); bis.close(); in.close(); } catch (OptionalDataException e) { getLog().error("Wrong data", e); } catch (IOException | ClassNotFoundException e) { getLog().error("Error while transfering dnd object back to java", e); } } else { getLog().error("bs is null"); if (transferData == null) { getLog().error("transferData also"); } } } return o; }
/** * Serialize an instance, restore it, and check for equality. In addition, test for a bug that was * reported where the listener list is 'null' after deserialization. */ public void testSerialization() { BarRenderer r1 = new BarRenderer(); r1.setBaseLegendTextFont(new Font("Dialog", Font.PLAIN, 4)); r1.setBaseLegendTextPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.green)); r1.setBaseLegendShape(new Line2D.Double(1.0, 2.0, 3.0, 4.0)); BarRenderer r2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(r1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); r2 = (BarRenderer) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(r1, r2); try { r2.notifyListeners(new RendererChangeEvent(r2)); } catch (NullPointerException e) { assertTrue(false); // failed } }
public static Object byteArray2Object(byte[] b) throws IOException, ClassNotFoundException { /* * http://stackoverflow.com/questions/2836646/java-serializable-object-to-byte-array */ ByteArrayInputStream bis = new ByteArrayInputStream(b); ObjectInput oi = new ObjectInputStream(bis); Object nesne = oi.readObject(); bis.close(); oi.close(); return nesne; }