/** * compare the byte-array representation of two TaskObjects. * * @param f TaskObject * @param s TaskObject * @return true if the two arguments have the same byte-array */ private boolean sameBytes(TaskObject f, TaskObject s) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = null; try { out = new ObjectOutputStream(bos); out.writeObject(f); byte[] fs = bos.toByteArray(); out.writeObject(s); byte[] ss = bos.toByteArray(); if (fs.length != ss.length) return false; for (int i = 0; i < fs.length; i++) { if (fs[i] != ss[i]) return false; } return true; } catch (IOException e) { e.printStackTrace(); return false; } finally { if (out != null) { try { out.close(); } catch (IOException e) { // ignore } try { bos.close(); } catch (IOException e) { // ignore } } } }
/** * 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 byte[] getByteContent() throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(baos); out.writeObject(content); out.close(); return baos.toByteArray(); }
// write data to server's database file private void updateFile(String file_name, Object data) { try { OutputStream file_data = new FileOutputStream(file_name); OutputStream buffer = new BufferedOutputStream(file_data); ObjectOutput data_out = new ObjectOutputStream(buffer); data_out.writeObject(data); data_out.close(); } catch (Exception e) { System.out.println("updateFile: Failed to update data to " + file_name); } }