List of usage examples for java.io ObjectOutputStream close
public void close() throws IOException
From source file:org.pgptool.gui.config.impl.ConfigRepositoryImpl.java
public static void writeObject(Object o, String destinationFile) { ObjectOutputStream oos = null; try {/*w w w. j a v a2s .c o m*/ log.trace(String.format("Persisting %s to %s", o, destinationFile)); File file = new File(destinationFile); if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) { throw new RuntimeException("Failed to create all parent directories"); } FileOutputStream fout = new FileOutputStream(file); oos = new ObjectOutputStream(fout); oos.writeObject(o); oos.flush(); oos.close(); fout.close(); } catch (Throwable t) { throw new RuntimeException("Failed to write config: " + destinationFile, t); } finally { safeClose(oos); } }
From source file:de.tudarmstadt.ukp.dkpro.tc.core.util.TaskUtils.java
/** * Saves a serializable object of type <T> to disk. Output file may be uncompressed, gzipped or * bz2-compressed. Compressed files must have a .gz or .bz2 suffix. * * @param serializedFile/*from ww w . j av a2 s. c o m*/ * model output file * @param serializableObject * the object to serialize * @throws IOException */ public static void serialize(File serializedFile, Object serializableObject) throws IOException { FileOutputStream fos = new FileOutputStream(serializedFile); BufferedOutputStream bufStr = new BufferedOutputStream(fos); OutputStream underlyingStream = null; if (serializedFile.getName().endsWith(".gz")) { underlyingStream = new GZIPOutputStream(bufStr); } else if (serializedFile.getName().endsWith(".bz2")) { underlyingStream = new CBZip2OutputStream(bufStr); // manually add bz2 prefix to make it compatible to normal bz2 tools // prefix has to be skipped when reading the stream with CBZip2 fos.write("BZ".getBytes("UTF-8")); } else { underlyingStream = bufStr; } ObjectOutputStream serializer = new ObjectOutputStream(underlyingStream); try { serializer.writeObject(serializableObject); } finally { serializer.flush(); serializer.close(); } }
From source file:com.conwet.silbops.model.JSONvsRMIPerfT.java
public static void sizeExternalizeSeveralWithoutTypes(Externalizable[] objects) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {//www .j a v a 2 s .c o m ObjectOutputStream out = new ObjectOutputStream(baos); for (Externalizable object : objects) { object.writeExternal(out); } out.close(); } catch (IOException ex) { ex.printStackTrace(); } int size = baos.size(); int sizePerMessage = size / objects.length; System.out.println(" Size no indicating types " + objects.length + " messages - RMI: " + size + " bytes (" + sizePerMessage + " bytes/msg)"); }
From source file:com.sec.ose.osi.sdk.protexsdk.component.ComponentAPIWrapper.java
public static void save() { ObjectOutputStream oos; try {/*from w w w.j av a 2 s. co m*/ oos = new ObjectOutputStream(new FileOutputStream(new File(COMPONENT_FILE_PATH))); for (ComponentInfo tmp : globalComponentManager.getComponentList()) { oos.writeObject(tmp); } oos.flush(); oos.close(); } catch (FileNotFoundException e) { } catch (IOException e) { } }
From source file:com.titilink.camel.rest.util.CommonUtils.java
/** * clone ? ?//w w w. j a va2 s . c o m * * @param obj ? * @return ? */ public static Object clone(Object obj) { if (obj == null) { return null; } Object anotherObj = null; byte[] bytes = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(baos); oos.writeObject(obj); bytes = baos.toByteArray(); } catch (Exception ex) { LOGGER.error("CloneObjectUtil cloneexception ", ex); return null; } finally { if (oos != null) { try { oos.close(); } catch (Exception e) { LOGGER.error("CloneObjectUtil cloneexception ", e); } } } ByteArrayInputStream bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = null; try { ois = new ObjectInputStream(bais); anotherObj = ois.readObject(); } catch (Exception ex) { LOGGER.error("CloneObjectUtil cloneexception ", ex); return null; } finally { if (ois != null) { try { ois.close(); } catch (Exception e) { LOGGER.error("CloneObjectUtil cloneexception ", e); } } } return anotherObj; }
From source file:com.beetle.framework.util.ObjectUtil.java
/** * // ww w . j a v a 2 s. c o m * * @param originObj * @return */ public final static Object objectClone(Object originObj) { ByteArrayOutputStream bao = null; ByteArrayInputStream bai = null; ObjectOutputStream oos; ObjectInputStream ois; try { bao = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bao); oos.writeObject(originObj); oos.flush(); oos.close(); bai = new ByteArrayInputStream(bao.toByteArray()); ois = new ObjectInputStream(bai); Object obj = ois.readObject(); ois.close(); oos = null; ois = null; return obj; } catch (Exception e) { e.printStackTrace(); return null; } finally { try { if (bao != null) { bao.close(); bao = null; } if (bai != null) { bai.close(); bai = null; } } catch (IOException e) { } } }
From source file:de.tudarmstadt.ukp.dkpro.tc.mallet.util.MalletUtils.java
public static void runTrainCRF(File trainingFile, File modelFile, double var, int iterations, String defaultLabel, boolean fullyConnected, int[] orders, boolean denseFeatureValues) throws FileNotFoundException, IOException, ClassNotFoundException { Reader trainingFileReader = null; InstanceList trainingData = null;/*from ww w. j av a2s . c om*/ //trainingFileReader = new FileReader(trainingFile); trainingFileReader = new InputStreamReader(new GZIPInputStream(new FileInputStream(trainingFile)), "UTF-8"); Pipe p = null; CRF crf = null; p = new ConversionToFeatureVectorSequence(denseFeatureValues); //uses first line of file to identify DKProInstanceID feature and discard p.getTargetAlphabet().lookupIndex(defaultLabel); p.setTargetProcessing(true); trainingData = new InstanceList(p); trainingData.addThruPipe(new LineGroupIterator(trainingFileReader, Pattern.compile("^\\s*$"), true)); //if you want to skip the line containing feature names, add "|^[A-Za-z]+.*$" // logger.info // ("Number of features in training data: "+p.getDataAlphabet().size()); // logger.info ("Number of predicates: "+p.getDataAlphabet().size()); if (p.isTargetProcessing()) { Alphabet targets = p.getTargetAlphabet(); StringBuffer buf = new StringBuffer("Labels:"); for (int i = 0; i < targets.size(); i++) { buf.append(" ").append(targets.lookupObject(i).toString()); // logger.info(buf.toString()); } } crf = trainCRF(trainingData, crf, var, iterations, defaultLabel, fullyConnected, orders); ObjectOutputStream s = new ObjectOutputStream(new FileOutputStream(modelFile)); s.writeObject(crf); s.close(); }
From source file:cascading.util.Util.java
public static String serializeBase64(Object object, boolean compress) throws IOException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(compress ? new GZIPOutputStream(bytes) : bytes); try {//from w w w . j a v a 2 s . co m out.writeObject(object); } finally { out.close(); } return new String(Base64.encodeBase64(bytes.toByteArray())); }
From source file:com.conwet.silbops.model.JSONvsRMIPerfT.java
public static void sizeExternalizeSeveralWithType(String type, Externalizable[] objects) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {/* www .j a v a 2 s.co m*/ ObjectOutputStream out = new ObjectOutputStream(baos); for (Externalizable object : objects) { Message msg = new MessageImpl(type, object, null); out.writeObject(msg); } out.close(); } catch (IOException ex) { ex.printStackTrace(); } int size = baos.size(); int sizePerMessage = size / objects.length; System.out.println(" Size indicating types " + objects.length + " messages - RMI: " + size + " bytes (" + sizePerMessage + " bytes/msg)"); }
From source file:jade.mtp.http.XMLCodec.java
/** * A user-defined property (String name, Object value) is encoded the following way: * <user-defined href="name" type="type">value</user-defined> * //from w ww . j a v a2s. c o m */ private static void encodeProp(StringBuffer sb, Property p) { String v = null; Object o = p.getValue(); String type = PROP_STRING_TYPE; if (o instanceof String) { v = (String) o; } else if (o instanceof byte[]) { type = PROP_BYTE_TYPE; try { v = new String(Base64.encodeBase64((byte[]) o), "US-ASCII"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } else if (o instanceof Serializable) { type = PROP_SER_TYPE; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(o); oos.close(); byte[] bytes = bos.toByteArray(); if (bytes != null) v = new String(Base64.encodeBase64(bytes), "US-ASCII"); } catch (IOException ioe) { return; } } else { return; } sb.append(OT).append(PROP_TAG).append(" "); sb.append(PROP_ATTR).append("=\"").append(p.getName()).append("\" "); sb.append(PROP_ATTR_TYPE).append("=\"").append(type).append("\""); sb.append(CT); sb.append(v); sb.append(ET).append(PROP_TAG).append(CT); }