/** Serialize an instance, restore it, and check for equality. */ public void testSerialization() { DefaultHighLowDataset d1 = new DefaultHighLowDataset( "Series 1", new Date[] {new Date(123L)}, new double[] {1.2}, new double[] {3.4}, new double[] {5.6}, new double[] {7.8}, new double[] {99.9}); DefaultHighLowDataset d2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(d1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); d2 = (DefaultHighLowDataset) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(d1, d2); }
/** * Reads a saved serialized HashMap of TreeData objects in 'dir' named 'id' and returns the * TreeContainer object. * * @param id name of HashMap * @param dir location of saved .ser file * @param printData if true, will print HashMap to string (via toString method) * @return HashMap of TreeData objects specified by 'id' and 'dir', or null if no TreeContainer * found */ @SuppressWarnings("unchecked") public static HashMap<String, TreeData> readTreeDataMap( String id, String dir, boolean printData) { ObjectInput inputObject; HashMap<String, TreeData> tdHash = null; try { inputObject = new ObjectInputStream(new BufferedInputStream(new FileInputStream(dir + id + ".ser"))); try { tdHash = (HashMap<String, TreeData>) inputObject.readObject(); } catch (ClassNotFoundException e) { tdHash = null; Logger.logln("Couldn't load ArrayList<TreeData>: " + id + ", from: " + dir); e.printStackTrace(); } finally { inputObject.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (printData == true && tdHash != null) { System.out.println(tdHash.toString()); } return tdHash; }
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. */ public void testSerialization() { List u1 = new java.util.ArrayList(); u1.add("URL A1"); u1.add("URL A2"); u1.add("URL A3"); List u2 = new java.util.ArrayList(); u2.add("URL B1"); u2.add("URL B2"); u2.add("URL B3"); CustomXYURLGenerator g1 = new CustomXYURLGenerator(); CustomXYURLGenerator g2 = null; g1.addURLSeries(u1); g1.addURLSeries(u2); try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(g1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); g2 = (CustomXYURLGenerator) in.readObject(); in.close(); } catch (Exception e) { System.out.println(e.toString()); } assertEquals(g1, g2); }
public static Action deserializeAction(byte[] byteArray) { ByteArrayInputStream bis = new ByteArrayInputStream(byteArray); ObjectInput in = null; Action o = null; try { in = new ObjectInputStream(bis); o = (Action) in.readObject(); } catch (IOException | ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { bis.close(); } catch (IOException ex) { // ignore close exception } try { if (in != null) { in.close(); } } catch (IOException ex) { // ignore close exception } } return o; }
public static TaggedDocument readTaggedDocument(String id, String dir, boolean printData) { ObjectInput inputObject; TaggedDocument td = null; try { inputObject = new ObjectInputStream(new BufferedInputStream(new FileInputStream(dir + id + ".ser"))); try { td = (TaggedDocument) inputObject.readObject(); } catch (ClassNotFoundException e) { td = null; Logger.logln("Couldn't load TaggedDocument: " + id + ", from: " + dir); e.printStackTrace(); } finally { inputObject.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (printData == true && td != null) { System.out.println(td.toString()); } return td; }
/** 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 static void breakSingletonWithSerialization() { System.out.println(); final String fileName = "singleton.ser"; // getting singleton instance Singleton instanceOne = Singleton.getInstance(); instanceOne.setValue(10); try { // Serialize to a file ObjectOutput objectOutput = new ObjectOutputStream(new FileOutputStream(fileName)); objectOutput.writeObject(instanceOne); objectOutput.close(); // now changed the value of singleton instanceOne.setValue(20); // Serialize to a file ObjectInput objectInput = new ObjectInputStream(new FileInputStream(fileName)); Singleton instanceTwo = (Singleton) objectInput.readObject(); objectInput.close(); System.out.println("Instance One Value= " + instanceOne.getValue()); System.out.println("Instance Two Value= " + instanceTwo.getValue()); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }
/** * 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 } }
static ExecutionSetEntry load(final File folder, final String uid) { if (folder == null) { return null; } File file = new File(folder, uid); if (!file.isFile()) { return null; } ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(RuleEngine.class.getClassLoader()); ObjectInput strm = new ObjectInputStream(new FileInputStream(file)); try { ExecutionSetEntry result = (ExecutionSetEntry) strm.readObject(); LOG.debug("execution set data loaded from " + file); return result; } finally { strm.close(); } } catch (Exception e) { LOG.warn("failed loading execution set data from file " + file, e); return null; } finally { Thread.currentThread().setContextClassLoader(oldCl); } }
private void loadCartmap() throws Exception { ObjectInput oi = null; try { // look for a frozen map - otherwise create an empty one oi = new ObjectInputStream(new FileInputStream(_mapFile)); _cartMap = (HashMap<String, byte[]>) oi.readObject(); oi.close(); oi = null; } catch (Throwable e) { throw new Exception("Exception loading cartmap", e); } finally { try { if (null != oi) oi.close(); } catch (Throwable e) { e.printStackTrace(); } } }
@SuppressWarnings("unchecked") private void read(String file) throws IOException, ClassNotFoundException { InputStream buffer; System.err.println("Reading Data file:" + file); buffer = new BufferedInputStream(new FileInputStream(file)); ObjectInput input = new ObjectInputStream(buffer); trigramTable = (Hashtable<String, Integer>) input.readObject(); bigramTable = (Hashtable<String, Integer>) input.readObject(); input.close(); System.err.println("Finished"); }
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; }
/** Serialize an instance, restore it, and check for equality. */ public void testSerialization() { // try a default instance StandardDialScale s1 = new StandardDialScale(); StandardDialScale s2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(s1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); s2 = (StandardDialScale) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(s1, s2); // try a customised instance s1 = new StandardDialScale(); s1.setExtent(123.4); s1.setMajorTickPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.white)); s1.setMajorTickStroke(new BasicStroke(2.0f)); s2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(s1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); s2 = (StandardDialScale) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(s1, s2); }
/** Serialize an instance, restore it, and check for equality. */ public void testSerialization() { // test a default instance DialBackground b1 = new DialBackground(); DialBackground b2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(b1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); b2 = (DialBackground) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(b1, b2); // test a customised instance b1 = new DialBackground(); b1.setPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.green)); b1.setGradientPaintTransformer( new StandardGradientPaintTransformer(GradientPaintTransformType.CENTER_VERTICAL)); b2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(b1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); b2 = (DialBackground) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(b1, b2); }
/** Serialize an instance, restore it, and check for equality. */ public void testSerialization() { DefaultXYDataset d1 = new DefaultXYDataset(); DefaultXYDataset d2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(d1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); d2 = (DefaultXYDataset) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(d1, d2); // try a dataset with some content... double[] x1 = new double[] {1.0, 2.0, 3.0}; double[] y1 = new double[] {4.0, 5.0, 6.0}; double[][] data1 = new double[][] {x1, y1}; d1.addSeries("S1", data1); try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(d1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); d2 = (DefaultXYDataset) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(d1, d2); }
/** Serialize an instance, restore it, and check for equality. */ @Test public void testSerialization() throws IOException, ClassNotFoundException { StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(r1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); StackedXYAreaRenderer2 r2 = (StackedXYAreaRenderer2) in.readObject(); in.close(); assertEquals(r1, r2); }
/** Serialize an instance, restore it, and check for equality. */ @Test public void testSerialization() throws IOException, ClassNotFoundException { TextTitle t = new TextTitle("Title"); XYTitleAnnotation a1 = new XYTitleAnnotation(1.0, 2.0, t); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(a1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); XYTitleAnnotation a2 = (XYTitleAnnotation) in.readObject(); in.close(); assertEquals(a1, a2); }
/** Serialize an instance, restore it, and check for equality. */ @Test public void testSerialization() throws IOException, ClassNotFoundException { StandardXYItemLabelGenerator g1 = new StandardXYItemLabelGenerator(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(g1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); StandardXYItemLabelGenerator g2 = (StandardXYItemLabelGenerator) in.readObject(); in.close(); assertEquals(g1, g2); }
/** Serialize an instance, restore it, and check for equality. */ public void testSerialization() { GradientBarPainter p1 = new GradientBarPainter(0.1, 0.2, 0.3); GradientBarPainter p2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(p1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); p2 = (GradientBarPainter) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(p1, p2); }
/** Serialize an instance, restore it, and check for equality. */ @Test public void testSerialization() throws IOException, ClassNotFoundException { QuarterDateFormat qf1 = new QuarterDateFormat(TimeZone.getTimeZone("GMT"), new String[] {"1", "2", "3", "4"}); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(qf1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); QuarterDateFormat qf2 = (QuarterDateFormat) in.readObject(); in.close(); assertEquals(qf1, qf2); }
/** Serialize an instance, restore it, and check for equality. */ public void testSerialization() { IntervalBarRenderer r1 = new IntervalBarRenderer(); IntervalBarRenderer 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 = (IntervalBarRenderer) in.readObject(); in.close(); } catch (Exception e) { throw new RuntimeException(e); } assertEquals(r1, r2); }
/** Serialize an instance, restore it, and check for equality. */ @Test public void testSerialization() throws IOException, ClassNotFoundException { MinMaxCategoryRenderer r1 = new MinMaxCategoryRenderer(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(r1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); MinMaxCategoryRenderer r2 = (MinMaxCategoryRenderer) in.readObject(); in.close(); assertEquals(r1, r2); }
/** Check serialization of a more complex instance. */ @Test public void testSerialization2() throws IOException, ClassNotFoundException { DefaultStatisticalCategoryDataset d1 = new DefaultStatisticalCategoryDataset(); d1.add(1.2, 3.4, "Row 1", "Column 1"); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(d1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); DefaultStatisticalCategoryDataset d2 = (DefaultStatisticalCategoryDataset) in.readObject(); in.close(); assertEquals(d1, d2); }
/** Serialize an instance, restore it, and check for equality. */ public void testSerialization() { Second s1 = new Second(); Second s2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(s1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); s2 = (Second) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(s1, s2); }
/** Serialize an instance, restore it, and check for equality. */ public void testSerialization() { LegendItemEntity e1 = new LegendItemEntity(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0)); LegendItemEntity e2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(e1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); e2 = (LegendItemEntity) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(e1, e2); }
/** Serialize an instance, restore it, and check for equality. */ public void testSerialization() { EmptyBlock b1 = new EmptyBlock(1.0, 2.0); EmptyBlock b2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(b1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); b2 = (EmptyBlock) in.readObject(); in.close(); } catch (Exception e) { fail(e.toString()); } assertEquals(b1, b2); }
/** Serialize an instance, restore it, and check for equality. */ public void testSerialization() { BorderArrangement b1 = new BorderArrangement(); BorderArrangement b2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(b1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); b2 = (BorderArrangement) in.readObject(); in.close(); } catch (Exception e) { fail(e.toString()); } assertEquals(b1, b2); }
private static Object deserialize(String serialized) throws ClassNotFoundException, IOException { byte[] bytes = Base64.decode(serialized); ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInput in = null; try { in = new ObjectInputStream(bis); return in.readObject(); } finally { try { if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } }
@SuppressWarnings("unchecked") public static <T> InterfacePractice<T> read(String filePath) { InterfacePractice<T> object = null; try { ObjectInput in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(filePath))); try { object = (InterfacePractice<T>) in.readObject(); } finally { in.close(); } } catch (Exception e) { e.printStackTrace(); } return object; }