List of usage examples for java.io ObjectInputStream close
public void close() throws IOException
From source file:com.amazon.sqs.javamessaging.message.SQSObjectMessage.java
/** * Deserialize the <code>String</code> into <code>Serializable</code> * object.// ww w .java 2 s.co m */ protected static Serializable deserialize(String serialized) throws JMSException { if (serialized == null) { return null; } Serializable deserializedObject; ObjectInputStream objectInputStream = null; try { byte[] bytes = Base64.decode(serialized); objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes)); deserializedObject = (Serializable) objectInputStream.readObject(); } catch (IOException e) { LOG.error("IOException: Message cannot be written", e); throw convertExceptionToMessageFormatException(e); } catch (Exception e) { LOG.error("Unexpected exception: ", e); throw convertExceptionToMessageFormatException(e); } finally { if (objectInputStream != null) { try { objectInputStream.close(); } catch (IOException e) { LOG.warn(e.getMessage()); } } } return deserializedObject; }
From source file:de.tudarmstadt.ukp.dkpro.tc.core.util.TaskUtils.java
/** * Loads serialized Object from disk. File can be uncompressed, gzipped or bz2-compressed. * Compressed files must have a .gz or .bz2 suffix. * * @param serializedFile//from w w w . j a v a 2s .co m * @return the deserialized Object * @throws IOException */ @SuppressWarnings({ "unchecked" }) public static <T> T deserialize(File serializedFile) throws IOException { FileInputStream fis = new FileInputStream(serializedFile); BufferedInputStream bufStr = new BufferedInputStream(fis); InputStream underlyingStream = null; if (serializedFile.getName().endsWith(".gz")) { underlyingStream = new GZIPInputStream(bufStr); } else if (serializedFile.getName().endsWith(".bz2")) { // skip bzip2 prefix that we added manually fis.read(); fis.read(); underlyingStream = new CBZip2InputStream(bufStr); } else { underlyingStream = bufStr; } ObjectInputStream deserializer = new ObjectInputStream(underlyingStream); Object deserializedObject = null; try { deserializedObject = deserializer.readObject(); } catch (ClassNotFoundException e) { throw new IOException("The serialized file was probably corrupted.", e); } finally { deserializer.close(); } return (T) deserializedObject; }
From source file:com.soundcloud.playerapi.ApiWrapper.java
/** * Read wrapper from a file/* w ww . j av a 2 s. c om*/ * @param f the file * @return the wrapper * @throws IOException IO problems * @throws ClassNotFoundException class not found */ public static ApiWrapper fromFile(File f) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f)); try { return (ApiWrapper) ois.readObject(); } finally { ois.close(); } }
From source file:com.beetle.framework.util.ObjectUtil.java
/** * // w ww . j a v a 2s.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:net.librec.util.FileUtil.java
public static Object deserialize(String filePath) throws Exception { FileInputStream fis = new FileInputStream(filePath); ObjectInputStream ois = new ObjectInputStream(fis); Object obj = ois.readObject(); ois.close(); fis.close();//from w w w.j a va2 s. com return obj; }
From source file:SystemUtils.java
public static Object LoadObject(String filePath) throws IOException, ClassNotFoundException { Object target = null;//from w w w . j a v a 2 s.co m FileInputStream fis = null; ObjectInputStream ois = null; try { fis = new FileInputStream(filePath); ois = new ObjectInputStream(fis); target = ois.readObject(); } finally { if (ois != null) { try { ois.close(); } catch (Exception e) { } ois = null; } if (fis != null) { try { fis.close(); } catch (Exception e) { } fis = null; } } return target; }
From source file:com.asquareb.kaaval.MachineKey.java
/** * Method to decrypt a string. Accepts the string to be decrypted and the * name of the file which stores the key values *//* w w w.j a v a2 s . c om*/ public static String decrypt(String property, String app) throws IOException, KaavalException { SecretKey key = null; ObjectInputStream is = null; MachineKey mKey = null; int eti = 0; Cipher pbeCipher = null; InetAddress ip = InetAddress.getLocalHost(); NetworkInterface macAddress = NetworkInterface.getByInetAddress(ip); byte[] macId = macAddress.getHardwareAddress(); try { is = new ObjectInputStream(new BufferedInputStream(new FileInputStream(app))); mKey = (MachineKey) is.readObject(); key = mKey.yek; salt = mKey.tlas; eti = mKey.eti; String ipa = ip.getHostAddress(); if (!ipa.equals(mKey.api) || !new String(macId).equals(mKey.macad)) throw new KaavalException(5, "Key file is not for this machine"); is.close(); pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(salt, eti)); return new String(pbeCipher.doFinal(base64Decode(property))); } catch (IOException e) { throw new KaavalException(3, "Error in reading key file during decryption", e); } catch (KaavalException e) { throw e; } catch (Exception e) { throw new KaavalException(4, "Error during decryption", e); } finally { if (is != null) is.close(); } }
From source file:de.julielab.jtbd.TokenizerApplication.java
/** * Entry point for compare validation mode * * @param args/*from w w w. j a v a2s . c o m*/ * the command line arguments */ private static void startCompareValidationMode(final String[] args) { if (args.length != 6) { System.err.println("usage: JTBD e <modelFile> <sent-file> <tok-file> <predout-file> <errout-file>"); System.exit(-1); } ObjectInputStream in; CRF crf = null; try { // load model in = new ObjectInputStream(new GZIPInputStream(new FileInputStream(args[1]))); crf = (CRF) in.readObject(); in.close(); } catch (final Exception e) { e.printStackTrace(); } final File orgSentencesFile = new File(args[2]); final File tokSentencesFile = new File(args[3]); final ArrayList<String> orgSentences = readFile(orgSentencesFile); final ArrayList<String> tokSentences = readFile(tokSentencesFile); final File predOutFile = new File(args[4]); final File errOutFile = new File(args[5]); final ArrayList<String> errors = new ArrayList<String>(); final ArrayList<String> predictions = new ArrayList<String>(); doEvaluation(crf, orgSentences, tokSentences, predictions, errors); writeFile(predictions, predOutFile); writeFile(errors, errOutFile); }
From source file:Main.java
/** * Attempts to decode Base64 data and deserialize a Java Object within. * Returns <tt>null if there was an error. * // w w w . j a va2s . c o m * @param encodedObject * The Base64 data to decode * @return The decoded and deserialized object * @since 1.4 */ public static Object decodeToObject(String encodedObject) throws Exception { byte[] objBytes = decode(encodedObject); java.io.ByteArrayInputStream bais = null; java.io.ObjectInputStream ois = null; try { bais = new java.io.ByteArrayInputStream(objBytes); ois = new java.io.ObjectInputStream(bais); return ois.readObject(); } // end try catch (java.io.IOException e) { e.printStackTrace(); return null; } // end catch catch (java.lang.ClassNotFoundException e) { e.printStackTrace(); return null; } // end catch finally { try { bais.close(); } catch (Exception e) { // ignore it } try { ois.close(); } catch (Exception e) { // ignore it } } // end finally }
From source file:at.ofai.music.util.WormFileParseException.java
public static EventList readBinary(String fileName) { try {/* ww w . j av a 2 s . c o m*/ ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName)); EventList e = (EventList) ois.readObject(); ois.close(); return e; } catch (IOException e) { System.err.println(e); return null; } catch (ClassNotFoundException e) { System.err.println(e); return null; } }