List of usage examples for java.io ObjectOutputStream close
public void close() throws IOException
From source file:org.cyclop.test.AbstractTestCase.java
public byte[] serialize(Object obj) throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bout); out.writeObject(obj);/*from w w w. ja v a 2 s . c o m*/ out.close(); byte[] serialized = bout.toByteArray(); assertNotNull(serialized); return serialized; }
From source file:com.impetus.kundera.property.accessor.ObjectAccessor.java
@Override public final byte[] toBytes(Object o) { try {//from w w w. j a va2 s . c om if (o != null) { if (o instanceof byte[]) { return (byte[]) o; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(o); oos.close(); return baos.toByteArray(); } } catch (IOException e) { throw new PropertyAccessException(e); } return null; }
From source file:fr.ortolang.diffusion.notification.NotificationServiceBean.java
@Override @TransactionAttribute(TransactionAttributeType.REQUIRED) public void throwEvent(String fromObject, String throwedBy, String objectType, String eventType, Map<String, String> arguments) throws NotificationServiceException { try {//w w w .java2 s. co m Message message = context.createMessage(); message.setStringProperty(OrtolangEvent.DATE, OrtolangEvent.getEventDateFormatter().format(new Date())); message.setStringProperty(OrtolangEvent.THROWED_BY, throwedBy); message.setStringProperty(OrtolangEvent.FROM_OBJECT, fromObject); message.setStringProperty(OrtolangEvent.OBJECT_TYPE, objectType); message.setStringProperty(OrtolangEvent.TYPE, eventType); if (arguments != null && arguments instanceof Serializable) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(arguments); oos.close(); message.setStringProperty(OrtolangEvent.ARGUMENTS, Base64.encodeBase64String(baos.toByteArray())); } context.createProducer().send(notificationTopic, message); } catch (Exception e) { LOGGER.log(Level.WARNING, "unable to throw event", e); throw new NotificationServiceException("unable to throw event", e); } }
From source file:PersisTest.java
public void actionPerformed(ActionEvent e) { if (e.getSource() == clearBtn) { // Repaint with an empty display list. displayList = new ArrayList(); repaint();/*from ww w .ja v a2 s .c o m*/ } else if (e.getSource() == saveBtn) { // Write display list array list to an object output stream. try { FileOutputStream fos = new FileOutputStream(pathname); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(displayList); oos.flush(); oos.close(); fos.close(); } catch (IOException ex) { System.err.println("Trouble writing display list array list"); } } else if (e.getSource() == restoreBtn) { // Read a new display list array list from an object input stream. try { FileInputStream fis = new FileInputStream(pathname); ObjectInputStream ois = new ObjectInputStream(fis); displayList = (ArrayList) (ois.readObject()); ois.close(); fis.close(); repaint(); } catch (ClassNotFoundException ex) { System.err.println("Trouble reading display list array list"); } catch (IOException ex) { System.err.println("Trouble reading display list array list"); } } else if (e.getSource() == quitBtn) { System.exit(0); } }
From source file:de.scoopgmbh.copper.persistent.StandardJavaSerializer.java
private String serialize(final Object o) throws IOException { if (o == null) return null; final ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); final ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(o);//from w ww . j av a 2s .c o m oos.close(); baos.close(); byte[] data = baos.toByteArray(); boolean isCompressed = false; if (compress && compressThresholdSize <= data.length && data.length <= compressorMaxSize) { data = compressorTL.get().compress(data); isCompressed = true; } final String encoded = new Base64().encodeToString(data); final StringBuilder sb = new StringBuilder(encoded.length() + 4); sb.append(isCompressed ? 'C' : 'U').append(encoded); return sb.toString(); }
From source file:edu.uga.cs.fluxbuster.clustering.hierarchicalclustering.Dendrogram.java
/** * Creates a deep copy of any object.//from w ww.j a v a2 s. c o m * * @param o the object to copy * @return the copy */ public Object deepCopy(Object o) { try { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(byteOut); out.writeObject(o); out.close(); byte[] data = byteOut.toByteArray(); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data)); Object copy = in.readObject(); in.close(); return copy; } catch (Exception e) { if (log.isErrorEnabled()) { log.error("Error performing deep copy.", e); } } return null; }
From source file:net.sf.ehcache.distribution.EventMessageTest.java
/** * test serialization and deserialization of EventMessage. *//*w w w. java2s . co m*/ public void testSerialization() throws IOException, ClassNotFoundException { EventMessage eventMessage = new EventMessage(EventMessage.PUT, "key", new Element("key", "element")); ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bout); oos.writeObject(eventMessage); byte[] serializedValue = bout.toByteArray(); oos.close(); EventMessage eventMessage2 = null; ByteArrayInputStream bin = new ByteArrayInputStream(serializedValue); ObjectInputStream ois = new ObjectInputStream(bin); eventMessage2 = (EventMessage) ois.readObject(); ois.close(); //Check after Serialization assertEquals("key", eventMessage2.getSerializableKey()); assertEquals("element", eventMessage2.getElement().getObjectValue()); assertEquals(EventMessage.PUT, eventMessage2.getEvent()); assertTrue(eventMessage2.isValid()); }
From source file:slina.mb.controller.ObjectSaverFactory.java
@Ignore public void createObjects() throws IOException, ClassNotFoundException { try {//from w w w .j a v a 2s. co m OutputStream os = new FileOutputStream("logEvent.ser"); List<String> stringList = Arrays.asList(samples); ArrayList<LogEvent> resultaArray = (ArrayList<LogEvent>) stdLogParser.createLogEvents(1, stringList); assertEquals(7, resultaArray.size()); ObjectOutputStream om = new ObjectOutputStream(os); om.writeObject(resultaArray); os.flush(); os.close(); om.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
From source file:natalia.dymnikova.cluster.scheduler.impl.Codec.java
public byte[] packObject(final Object funct) { try {//from ww w. j a va 2 s . co m // TODO make sense to reuse Codec and inner ObjectOutputStream at least for a single RemoteObservable final ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); final ObjectOutputStream os = new ObjectOutputStream(arrayOutputStream); os.writeObject(funct); os.close(); return arrayOutputStream.toByteArray(); } catch (final IOException e) { throw propagateUnchecked(e); } }
From source file:com.hp.autonomy.hod.sso.HodAuthenticationTest.java
private <T extends Serializable> T writeAndReadObject(final T object) throws IOException, ClassNotFoundException { final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); final ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(object); objectOutputStream.close(); final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( byteArrayOutputStream.toByteArray()); final ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); //noinspection unchecked return (T) objectInputStream.readObject(); }