List of usage examples for java.io ObjectInputStream ObjectInputStream
public ObjectInputStream(InputStream in) throws IOException
From source file:Main.java
public static Object readSeriObject(Context context, String fileName) throws Exception { String path = context.getFilesDir() + "/"; File dir = new File(path); dir.mkdirs();/*w ww. j av a 2 s . c o m*/ File file = new File(dir, fileName); InputStream is = new FileInputStream(file); ObjectInputStream objectInputStream = new ObjectInputStream(is); Object o = objectInputStream.readObject(); return o; }
From source file:Main.java
/** * Convert bytes array to object./*from w ww . jav a2 s . com*/ * * @param bytes object bytes * @param <T> object class * @return object */ public static <T> T fromBytes(byte[] bytes) { Object result = null; ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInput in = null; try { in = new ObjectInputStream(bis); result = in.readObject(); } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); } finally { if (in != null) { try { in.close(); } catch (Exception e) { } } if (bis != null) { try { bis.close(); } catch (Exception e) { } } } return (T) result; }
From source file:Main.java
public static List copyBySerialize(List src) throws IOException, ClassNotFoundException { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(byteOut); out.writeObject(src);//from www.j a v a2 s. c o m ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray()); ObjectInputStream in = new ObjectInputStream(byteIn); List dest = (List) in.readObject(); return dest; }
From source file:MainClass.java
public static void desEncrypt(String f1, String f2) throws Exception { SecretKey key = null;//from w w w . j av a 2 s.c o m ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("DESKey.ser")); key = (SecretKey) keyFile.readObject(); keyFile.close(); KeyGenerator keygen = KeyGenerator.getInstance("DES"); key = keygen.generateKey(); ObjectOutputStream keyFileout = new ObjectOutputStream(new FileOutputStream("DESKey.ser")); keyFileout.writeObject(key); keyFileout.close(); Cipher cipher = null; cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); BufferedInputStream in = new BufferedInputStream(new FileInputStream(f1)); CipherOutputStream out = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(f2)), cipher); int i; do { i = in.read(); if (i != -1) out.write(i); } while (i != -1); in.close(); out.close(); }
From source file:Main.java
public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(byteOut); out.writeObject(src);//from w w w . ja v a 2 s . c om ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray()); ObjectInputStream in = new ObjectInputStream(byteIn); @SuppressWarnings("unchecked") List<T> dest = (List<T>) in.readObject(); return dest; }
From source file:Main.java
public static Object getAsObject(String key) { byte[] data = getAsBinary(key); if (data != null) { ByteArrayInputStream bais = null; ObjectInputStream ois = null; try {/*from ww w . java 2 s . c o m*/ bais = new ByteArrayInputStream(data); ois = new ObjectInputStream(bais); Object reObject = ois.readObject(); return reObject; } catch (Exception e) { e.printStackTrace(); return null; } finally { try { if (bais != null) bais.close(); } catch (IOException e) { e.printStackTrace(); } try { if (ois != null) ois.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
From source file:Main.java
public static <T> List<T> readList(String root, String filename, Class<T> type) { List<T> objects = new ArrayList<>(); File file = new File(root, filename); try {//from w w w . j a v a 2 s. c o m if (file.exists()) { FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis); Object object = ois.readObject(); if (object instanceof List) { for (Object it : (List) object) { objects.add(type.cast(it)); } } ois.close(); return Collections.unmodifiableList(objects); } } catch (Exception e) { Log.e(TAG, String.format("Failed to read [%s]", file), e); } return Collections.emptyList(); }
From source file:Main.java
public static final Object RestoreObject(final String path) { FileInputStream fileInputStream = null; ObjectInputStream objectInputStream = null; Object object = null;// w w w. j a v a 2s. c o m File file = new File(path); if (!file.exists()) { return null; } try { fileInputStream = new FileInputStream(file); objectInputStream = new ObjectInputStream(fileInputStream); object = objectInputStream.readObject(); return object; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (objectInputStream != null) { objectInputStream.close(); } if (fileInputStream != null) { fileInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return object; }
From source file:Main.java
public static Object toObject(byte[] bytes) { Object obj = null;/*from w w w. j a v a 2 s.c om*/ try { ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bis); obj = ois.readObject(); ois.close(); bis.close(); } catch (Exception ex) { ex.printStackTrace(); } return obj; }
From source file:Main.java
public static Object readObject(File file) { Object obj = null;// www . ja va2 s. c om if (file != null && file.exists()) { FileInputStream fileInputStream = null; ObjectInputStream objectInputStream = null; try { fileInputStream = new FileInputStream(file); objectInputStream = new ObjectInputStream(fileInputStream); obj = objectInputStream.readObject(); } catch (IOException | ClassNotFoundException e) { Log.d("readObject", e.getMessage()); } finally { try { if (objectInputStream != null) { objectInputStream.close(); } if (fileInputStream != null) { fileInputStream.close(); } } catch (IOException e) { Log.d("readObject", e.getMessage()); } } } return obj; }