List of usage examples for java.io ObjectOutputStream ObjectOutputStream
public ObjectOutputStream(OutputStream out) throws IOException
From source file:de.xwic.appkit.core.cluster.impl.ClientHandler.java
@Override public void run() { try {/* w w w.jav a2 s .c om*/ //socket.setSoTimeout(10000); // set the timeout to 10 seconds. ObjectInputStream oIn = new ObjectInputStream(socket.getInputStream()); ObjectOutputStream oOut = new ObjectOutputStream(socket.getOutputStream()); while (true) { Message msgIn; try { msgIn = (Message) oIn.readObject(); } catch (IOException e) { // this occurs when the connection is closed log.info("Client " + socket.getInetAddress() + " disconnected."); break; } catch (ClassNotFoundException e) { log.error("Can not handle incoming message - stream might be corrupted..", e); break; } Response response = null; //System.out.println("Received: " + msgIn.getCommand() + " ('" + msgIn.getArgument() + "')"); // as long as the client is not identified, no protocol is selected. if (protocol == null) { if (Message.CMD_IDENTIFY_CLIENT.equals(msgIn.getCommand())) { // argument must contain client type, which causes the selection of the right protocol // hard-coded here right now, but might get externalized into some configuration file // later on... if ("ClusterNode".equals(msgIn.getArgument())) { protocol = new ClusterNodeClientProtocol(cluster); } else if ("Console".equals(msgIn.getArgument())) { protocol = new ConsoleClientProtocol(cluster); } else { log.debug("Invalid protocol selected."); response = new Response(false, "Invalid protocol selected."); } if (protocol != null) { log.info("Selected protocol " + protocol.getClass().getName()); } } else { log.debug("Invalid command received - awaiting client identification.."); } } else { response = protocol.handleMessage(socket, msgIn); } if (response == null) { response = new Response(true); // send success response } oOut.writeObject(response); } } catch (IOException e) { log.error("Communication error with client", e); // will cause an exit.. } if (protocol != null) { protocol.onConnectionLost(); } // close the socket. try { socket.close(); } catch (IOException e) { log.warn("Error closing socket.", e); } // handle exiting properly (i.e. notify Cluster instance) }
From source file:com.weibo.api.motan.protocol.restful.support.RestfulUtil.java
public static Response serializeError(Exception ex) { try {//from w ww. java 2 s.c om ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(ex); oos.flush(); String info = new String(Base64.encodeBytesToBytes(baos.toByteArray()), MotanConstants.DEFAULT_CHARACTER); return Response.status(Status.EXPECTATION_FAILED).header(EXCEPTION_HEADER, ex.getClass()).entity(info) .build(); } catch (IOException e) { LoggerUtil.error("serialize " + ex.getClass() + " error", e); return Response.status(Status.INTERNAL_SERVER_ERROR).entity("serialization " + ex.getClass() + " error") .build(); } }
From source file:de.codecentric.boot.admin.event.ClientApplicationEventTest.java
@SuppressWarnings("unchecked") /**//from w ww . j av a 2s. c o m * yeah nasty but we need exact the same timestamp */ private <T extends Serializable> T cloneBySerialization(T obj) throws IOException, ClassNotFoundException { try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) { try (ObjectOutputStream oos = new ObjectOutputStream(buf)) { oos.writeObject(obj); } try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray()))) { return (T) ois.readObject(); } } }
From source file:com.hp.autonomy.hod.client.api.resource.ResourceTest.java
@Test public void testSerializeAndDeserialize() throws IOException, ClassNotFoundException { final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); final Resource input = new Resource(UUID.randomUUID(), "name", "domain"); try (final ObjectOutput objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) { objectOutputStream.writeObject(input); }/* w w w .j a v a 2 s . co m*/ try (final ObjectInputStream objectInputStream = new ObjectInputStream( new ByteArrayInputStream(byteArrayOutputStream.toByteArray()))) { @SuppressWarnings("CastToConcreteClass") final Resource output = (Resource) objectInputStream.readObject(); assertThat(output, is(input)); } }
From source file:ddf.camel.component.catalog.content.FileSystemDataAccessObject.java
public void store(String storePath, String suffix, String key, Object value) { OutputStream file = null;/*from ww w. java2s . com*/ ObjectOutputStream output = null; try { File dir = new File(storePath); if (!dir.exists()) { if (!dir.mkdir()) { LOGGER.debug("Unable to create directory: {}", dir.getAbsolutePath()); } } file = new FileOutputStream(storePath + key + suffix); OutputStream buffer = new BufferedOutputStream(file); output = new ObjectOutputStream(buffer); output.writeObject(value); } catch (IOException e) { LOGGER.debug("IOException storing value in cache with key = " + key, e); } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(file); } }
From source file:BckgrndServiceThreads.fileListingTH.java
void saveSerializedObject(String Objectname, Object serialisableObject) throws Exception { FileOutputStream outputFile = new FileOutputStream(Objectname); ObjectOutputStream outputObject = new ObjectOutputStream(outputFile); outputObject.writeObject(serialisableObject); outputObject.close();//from www . j a v a 2 s. co m }
From source file:com.facebook.infrastructure.utils.FBUtilities.java
public static byte[] serializeToStream(Object o) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] bytes = ArrayUtils.EMPTY_BYTE_ARRAY; try {//from www. j a v a 2 s . c o m ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(o); oos.flush(); bytes = bos.toByteArray(); oos.close(); bos.close(); } catch (IOException e) { LogUtil.getLogger(FBUtilities.class.getName()).info(LogUtil.throwableToString(e)); } return bytes; }
From source file:com.btoddb.trellis.common.serialization.JavaSerializer.java
private void serializeToOutputStream(Serializable obj, OutputStream os) { try {// ww w . ja v a2s. co m ObjectOutputStream oos = new ObjectOutputStream(os); oos.writeObject(obj); oos.close(); } catch (IOException e) { throw new TrellisException("exception while serializing Java Serializable object", e); } }
From source file:edu.uga.cs.fluxbuster.clustering.hierarchicalclustering.Dendrogram.java
/** * Creates a deep copy of any object./*from www. jav a 2 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:de.betterform.session.SessionSerializerTest.java
/** * first serializes the initialized XFormsProcessorImpl and then reads it back again. * @throws Exception/*from w ww .j av a 2 s . c o m*/ */ public void testSerialization() throws Exception { String fullPath = getClass().getResource("session.xhtml").getPath(); String strippedPath = fullPath.substring(0, fullPath.lastIndexOf("session.xhtml")); File outputFile = new File(strippedPath, "foo.xml"); this.processor.writeExternal(new ObjectOutputStream(new FileOutputStream(outputFile))); XFormsProcessorImpl xformsProcesssorImpl = new XFormsProcessorImpl(); assertTrue(xformsProcesssorImpl.getContainer() == null); File inFile = new File(strippedPath, "foo.xml"); xformsProcesssorImpl.readExternal(new ObjectInputStream(new FileInputStream(inFile))); xformsProcesssorImpl.init(); assertNotNull(xformsProcesssorImpl.getContainer()); }