List of usage examples for java.io ObjectOutputStream flush
public void flush() throws IOException
From source file:com.oprisnik.semdroid.utils.FileUtils.java
public static void writeObjectToFile(Object object, File file) throws IOException { if (!file.exists()) { file.getParentFile().mkdirs();// w ww . j a va 2 s. co m } ObjectOutputStream out = null; try { out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file))); out.writeObject(object); out.flush(); } finally { IOUtils.closeQuietly(out); } }
From source file:pt.minha.calibration.Calibrator.java
private static void runServer() throws Exception { // Reality server ServerSocket ss = new ServerSocket(12345); logger.info("server: started at {}", ss.getLocalSocketAddress()); while (true) { Socket s = ss.accept();/*from w w w . j ava2s . c om*/ logger.info("server: accepted {}", s.getRemoteSocketAddress()); try { ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream())); oos.flush(); ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(s.getInputStream())); while (!s.isClosed()) { Map<String, Object> p = (Map<String, Object>) ois.readObject(); Benchmark next = (Benchmark) Class.forName((String) p.get("bench")).newInstance(); next.setParameters(p); logger.info("server: running {}", p); Object result = next.server(); logger.info("server: running {} done", p); oos.writeObject(result); oos.flush(); } logger.info("server: disconnected {}", s.getRemoteSocketAddress()); } catch (IOException ioe) { logger.info("server: disconnected {} on {}", s.getRemoteSocketAddress(), ioe); } } }
From source file:bencoding.securely.Converters.java
public static String serializeObjectToString(Object object) throws Exception { ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); GZIPOutputStream gzipOutputStream = new GZIPOutputStream(arrayOutputStream); ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOutputStream); objectOutputStream.writeObject(object); objectOutputStream.flush(); objectOutputStream.close();// w ww.j a va2 s .c om gzipOutputStream.close(); arrayOutputStream.close(); String objectString = new String(Base64.encodeToString(arrayOutputStream.toByteArray(), Base64.DEFAULT)); return objectString; }
From source file:com.mirth.connect.connectors.jms.JmsMessageUtils.java
/** * @param message/* w w w. j a va 2 s . com*/ * the message to receive the bytes from. Note this only works * for TextMessge, ObjectMessage, StreamMessage and BytesMessage. * @return a byte array corresponding with the message payload * @throws JMSException * if the message can't be read or if the message passed is a * MapMessage * @throws java.io.IOException * if a failiare occurs while stream and converting the message * data */ public static byte[] getBytesFromMessage(Message message) throws JMSException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 2]; int len; if (message instanceof BytesMessage) { BytesMessage bMsg = (BytesMessage) message; // put message in read-only mode bMsg.reset(); while ((len = bMsg.readBytes(buffer)) != -1) { baos.write(buffer, 0, len); } } else if (message instanceof StreamMessage) { StreamMessage sMsg = (StreamMessage) message; sMsg.reset(); while ((len = sMsg.readBytes(buffer)) != -1) { baos.write(buffer, 0, len); } } else if (message instanceof ObjectMessage) { ObjectMessage oMsg = (ObjectMessage) message; ByteArrayOutputStream bs = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(bs); os.writeObject(oMsg.getObject()); os.flush(); baos.write(bs.toByteArray()); os.close(); bs.close(); } else if (message instanceof TextMessage) { TextMessage tMsg = (TextMessage) message; baos.write(tMsg.getText().getBytes()); } else { throw new JMSException("Cannot get bytes from Map Message"); } baos.flush(); byte[] bytes = baos.toByteArray(); baos.close(); return bytes; }
From source file:org.apache.hadoop.hbase.hbql.filter.RecordFilter.java
public static void testFilter(final RecordFilter origFilter) throws HBqlException, IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(baos); origFilter.write(oos);/*from w w w . j a v a2 s . com*/ oos.flush(); oos.close(); final byte[] b = baos.toByteArray(); final ByteArrayInputStream bais = new ByteArrayInputStream(b); final ObjectInputStream ois = new ObjectInputStream(bais); RecordFilter filter = new RecordFilter(); filter.readFields(ois); filter.reset(); final String family = "family1"; final String column = "author"; final String[] vals = { "An author value-81252702162528282000", "An author value-812527021593753270002009", "An author value-81252702156610125000", "An author value-812527021520532270002009", "An author value-81252702147337884000" }; for (String val : vals) { filter.getHRecord().setCurrentValue(family, column, 100, val); filter.getHRecord().setVersionValue(family, column, 100, val, true); } boolean v = filter.filterRow(); }
From source file:org.polarsys.reqcycle.traceability.utils.SerializationUtils.java
/** * Returns a serialized version of the given {@link TType}. The resulting {@link String} is url safe. * /*from w w w.ja va 2 s . c o m*/ * @param object * @return null if an error occurs */ public static String serialize(final Object object) { ObjectOutputStream objOutStrm = null; try { final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); objOutStrm = new ObjectOutputStream(byteArrayOutputStream); objOutStrm.writeObject(object); objOutStrm.flush(); final byte[] bytes = byteArrayOutputStream.toByteArray(); return encode(bytes); } catch (final IOException e) { e.printStackTrace(); // TODO Error management } finally { if (objOutStrm != null) { try { objOutStrm.close(); } catch (final IOException e) { e.printStackTrace(); // TODO Error management } } } return null; }
From source file:it.polito.elite.dog.core.library.model.DeviceStatus.java
/** * Serialize a DeviceStatus into a String using a byte encoding * @param object//from ww w . ja v a2 s .c o m * a DeviceStatus ready for serialization * @return a byte encoded String of a DeviceStatus * @throws IOException */ public static String serializeToString(DeviceStatus object) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream encoder = new ObjectOutputStream(baos); //serialize the DeviceStatus encoder.writeObject(object); //flush and close everything encoder.flush(); baos.flush(); encoder.close(); baos.close(); return new String(Base64.encodeBase64(baos.toByteArray())); }
From source file:com.pongasoft.util.io.IOUtils.java
public static <T> void serialize(T object, OutputStream outputStream) throws IOException { ObjectOutputStream oos = new ObjectOutputStream(outputStream); oos.writeObject(object);//from w ww . j a va 2 s . c o m oos.flush(); }
From source file:qa.util.FileUtil.java
public static byte[] serialize(Object o) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(o);/*from www . j a v a2s . c om*/ oos.flush(); oos.close(); bos.close(); byte[] byteData = bos.toByteArray(); return byteData; }
From source file:org.kuali.coeus.common.budget.framework.query.ObjectCloner.java
static public Object deepCopy(Object oldObj) { ObjectOutputStream oos = null; ObjectInputStream ois = null; try {/*from w w w. jav a 2 s . c om*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(); // A oos = new ObjectOutputStream(bos); // B // serialize and pass the object oos.writeObject(oldObj); // C oos.flush(); // D ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); // E ois = new ObjectInputStream(bin); // F // return the new object Object object = ois.readObject(); oos.close(); ois.close(); return object; // G } catch (Exception e) { return null; } finally { try { if (oos != null) oos.close(); if (ois != null) ois.close(); } catch (IOException e) { LOG.error(e.getMessage(), e); } } }