List of usage examples for java.io ObjectInputStream readObject
public final Object readObject() throws IOException, ClassNotFoundException
From source file:com.github.fcannizzaro.prefs.Prefs.java
/** * Get Object Value//ww w .j a v a2 s .c o m * * @param key of value * @param defaultValue if absent */ public static <T> T getObject(String key, T defaultValue) { T result = null; try { FileInputStream is = new FileInputStream(objects.getString(key)); ObjectInputStream object = new ObjectInputStream(is); result = (T) object.readObject(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } return result != null ? result : defaultValue; }
From source file:Main.java
@SuppressWarnings("unchecked") static boolean loadSharedPreferencesFromFile(Context context, File src) { boolean res = false; ObjectInputStream input = null; try {/*from www . j a v a2 s . c o m*/ GZIPInputStream inputGZIP = new GZIPInputStream(new FileInputStream(src)); input = new ObjectInputStream(inputGZIP); Editor prefEdit = PreferenceManager.getDefaultSharedPreferences(context).edit(); prefEdit.clear(); Map<String, Object> entries = (Map<String, Object>) input.readObject(); for (Entry<String, ?> entry : entries.entrySet()) { Object v = entry.getValue(); String key = entry.getKey(); if (v instanceof Boolean) prefEdit.putBoolean(key, ((Boolean) v).booleanValue()); else if (v instanceof Float) prefEdit.putFloat(key, ((Float) v).floatValue()); else if (v instanceof Integer) prefEdit.putInt(key, ((Integer) v).intValue()); else if (v instanceof Long) prefEdit.putLong(key, ((Long) v).longValue()); else if (v instanceof String) prefEdit.putString(key, ((String) v)); } prefEdit.commit(); res = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (input != null) { input.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return res; }
From source file:Main.java
/** * Attempts to decode Base64 data and deserialize a Java Object within. * Returns <tt>null if there was an error. * /*from w w w . j a v a 2 s . com*/ * @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:Main.java
/** * Reads a <code>Stroke</code> object that has been serialised by the * {@link SerialUtilities#writeStroke(Stroke, ObjectOutputStream)} method. * * @param stream the input stream (<code>null</code> not permitted). * * @return The stroke object (possibly <code>null</code>). * * @throws IOException if there is an I/O problem. * @throws ClassNotFoundException if there is a problem loading a class. *//*ww w .j a va2s . com*/ public static Stroke readStroke(final ObjectInputStream stream) throws IOException, ClassNotFoundException { if (stream == null) { throw new IllegalArgumentException("Null 'stream' argument."); } Stroke result = null; final boolean isNull = stream.readBoolean(); if (!isNull) { final Class c = (Class) stream.readObject(); if (c.equals(BasicStroke.class)) { final float width = stream.readFloat(); final int cap = stream.readInt(); final int join = stream.readInt(); final float miterLimit = stream.readFloat(); final float[] dash = (float[]) stream.readObject(); final float dashPhase = stream.readFloat(); result = new BasicStroke(width, cap, join, miterLimit, dash, dashPhase); } else { result = (Stroke) stream.readObject(); } } return result; }
From source file:Main.java
/** * Reads a <code>Paint</code> object that has been serialised by the * {@link SerialUtilities#writePaint(Paint, ObjectOutputStream)} method. * * @param stream the input stream (<code>null</code> not permitted). * * @return The paint object (possibly <code>null</code>). * * @throws IOException if there is an I/O problem. * @throws ClassNotFoundException if there is a problem loading a class. */// w ww. j av a 2 s.co m public static Paint readPaint(final ObjectInputStream stream) throws IOException, ClassNotFoundException { if (stream == null) { throw new IllegalArgumentException("Null 'stream' argument."); } Paint result = null; final boolean isNull = stream.readBoolean(); if (!isNull) { final Class c = (Class) stream.readObject(); if (isSerializable(c)) { result = (Paint) stream.readObject(); } else if (c.equals(GradientPaint.class)) { final float x1 = stream.readFloat(); final float y1 = stream.readFloat(); final Color c1 = (Color) stream.readObject(); final float x2 = stream.readFloat(); final float y2 = stream.readFloat(); final Color c2 = (Color) stream.readObject(); final boolean isCyclic = stream.readBoolean(); result = new GradientPaint(x1, y1, c1, x2, y2, c2, isCyclic); } } return result; }
From source file:com.joliciel.talismane.extensions.corpus.PosTaggerStatistics.java
public static PosTaggerStatistics loadFromFile(File inFile) { try {/*from ww w. j ava 2 s . c om*/ ZipInputStream zis = new ZipInputStream(new FileInputStream(inFile)); zis.getNextEntry(); ObjectInputStream in = new ObjectInputStream(zis); PosTaggerStatistics stats = null; try { stats = (PosTaggerStatistics) in.readObject(); } catch (ClassNotFoundException e) { LogUtils.logError(LOG, e); throw new RuntimeException(e); } return stats; } catch (IOException ioe) { LogUtils.logError(LOG, ioe); throw new RuntimeException(ioe); } }
From source file:at.ac.tuwien.dsg.comot.m.common.Utils.java
public static Object deepCopy(Object oldObj) throws IOException, ClassNotFoundException { ObjectOutputStream oos = null; ObjectInputStream ois = null; try {/* w ww .ja v a 2s. c o m*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); oos.writeObject(oldObj); oos.flush(); ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray())); return ois.readObject(); } finally { if (oos != null) oos.close(); if (ois != null) ois.close(); } }
From source file:org.pgptool.gui.config.impl.ConfigRepositoryImpl.java
@SuppressWarnings("unchecked") public static <T extends DtoBase> T readObject(String sourceFile) { ObjectInputStream ois = null; try {/*from ww w.j a v a 2 s .co m*/ File file = new File(sourceFile); if (!file.exists()) { return null; } FileInputStream fis = new FileInputStream(file); ois = new ObjectInputStream(fis); T ret = (T) ois.readObject(); ois.close(); return ret; } catch (Throwable t) { log.warn("Failed to read " + sourceFile, t); return null; } finally { safeClose(ois); } }
From source file:com.hs.mail.smtp.message.SmtpMessage.java
public static SmtpMessage readMessage(String name) throws SmtpException { File dir = Config.getSpoolDirectory(); File file = new File(dir, name + ".control"); ObjectInputStream is = null; try {//from ww w . jav a 2 s . com InputStream in = new BufferedInputStream(new FileInputStream(file), 1024); is = new ObjectInputStream(in); SmtpMessage message = (SmtpMessage) is.readObject(); return message; } catch (Exception e) { throw new SmtpException("Error while reading message file: " + file); } finally { IOUtils.closeQuietly(is); } }
From source file:de.alpharogroup.io.SerializedObjectExtensions.java
/** * The Method toObject() converts the given byte array into an Object. * * @param byteArray//w w w. j ava2 s.com * The byte array to convert into an Object. * @return The Object the was converted from the byte array. * @throws IOException * Signals that an I/O exception has occurred. * @throws ClassNotFoundException * is thrown when a class is not found in the classloader or no definition for the * class with the specified name could be found. */ public static Object toObject(final byte[] byteArray) throws IOException, ClassNotFoundException { Object object = null; ByteArrayInputStream byteArrayInputStream = null; ObjectInputStream objectInputStream = null; try { byteArrayInputStream = new ByteArrayInputStream(byteArray); objectInputStream = new ObjectInputStream(byteArrayInputStream); object = objectInputStream.readObject(); objectInputStream.close(); } finally { StreamExtensions.closeInputStream(byteArrayInputStream); StreamExtensions.closeInputStream(objectInputStream); } return object; }