/** Serializes and deserializes the specified object. */ @SuppressWarnings("unchecked") static <T> T reserialize(T object) { checkNotNull(object); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try { ObjectOutputStream out = new ObjectOutputStream(bytes); out.writeObject(object); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())); return (T) in.readObject(); } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); } }
private void save() throws IOException { ObjectOutputStream stream = null; try { stream = new ObjectOutputStream(new FileOutputStream(fHistoryStore)); stream.writeObject(this); } finally { if (stream != null) { stream.close(); } } }
/** * Writes the source object to an output stream using Java serialization. * The source object must implement {@link Serializable}. * @see ObjectOutputStream#writeObject(Object) */ @Override public void serialize(Object object, OutputStream outputStream) throws IOException { if (!(object instanceof Serializable)) { throw new IllegalArgumentException(getClass().getSimpleName() + " requires a Serializable payload " + "but received an object of type [" + object.getClass().getName() + "]"); } ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeObject(object); objectOutputStream.flush(); }
/** * The serial form currently mimics Android's java.util.HashMap version, e.g. see * http://omapzoom.org/?p=platform/libcore.git;a=blob;f=luni/src/main/java/java/util/HashMap.java */ private void writeObject(ObjectOutputStream stream) throws IOException { stream.defaultWriteObject(); stream.writeInt(size); for (int i = 0; i < size; i++) { stream.writeObject(keys[i]); stream.writeObject(values[i]); } }
/** @serialData the ConcurrentMap of elements and their counts. */ private void writeObject(ObjectOutputStream stream) throws IOException { stream.defaultWriteObject(); stream.writeObject(countMap); }
private static Bug1234 writeRead(final Bug1234 original) { try { final ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(fileName)); outputStream.writeObject(original); outputStream.close(); final ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(fileName)); final Bug1234 result = (Bug1234)inputStream.readObject(); inputStream.close(); return result; } catch (final Throwable e) { throw new RuntimeException("Error: " + e); } }
@Test public void testDeserializeStreamOfNull() throws Exception { final ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(null); oos.flush(); oos.close(); final ByteArrayInputStream inTest = new ByteArrayInputStream(streamReal.toByteArray()); final Object test = SerializationUtils.deserialize(inTest); assertNull(test); }
if (fileCacheEnabled && file.canRead()) { fireDatatasetStatusUpdate(FULL_DATASET_LOAD_START); try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) { logger.info("Loading dataset from " + file.getAbsolutePath()); long bNum = ois.readLong(); if (bNum == blockNumber) { fullData = (int[]) ois.readObject(); logger.info("Dataset loaded."); fireDatatasetStatusUpdate(FULL_DATASET_LOADED); try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) { logger.info("Writing dataset to " + file.getAbsolutePath()); oos.writeLong(blockNumber); oos.writeObject(fullData); } catch (IOException e) { throw new RuntimeException(e);
@org.junit.Test public void testPrimitiveLong() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(baos); final long input = 12345L; oos.writeLong(input); oos.close(); final InputStream bais = new ByteArrayInputStream(baos.toByteArray()); final ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais); final long result = clois.readLong(); assertEquals(input, result); clois.close(); }
Socket sock = new Socket(servername, port); OutputStream out = sock.getOutputStream(); out.write(lookupCommand); out.write(endofline); out.write(endofline); ObjectOutputStream dout = new ObjectOutputStream(out); dout.writeUTF(name); dout.flush(); InputStream in = new BufferedInputStream(sock.getInputStream()); skipHeader(in); ObjectInputStream din = new ObjectInputStream(in); int n = din.readInt(); String classname = din.readUTF(); din.close(); dout.close(); sock.close();
public static void main(String[] args) throws Exception { LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); File file = new File("cookie.file"); ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); Set<Cookie> cookies = (Set<Cookie>) in.readObject(); in.close(); WebClient wc = new WebClient(); Iterator<Cookie> i = cookies.iterator(); while (i.hasNext()) { wc.getCookieManager().addCookie(i.next()); } HtmlPage p = wc.getPage("http://google.com"); ObjectOutput out = new ObjectOutputStream(new FileOutputStream("cookie.file")); out.writeObject(wc.getCookieManager().getCookies()); out.close(); }
@GwtIncompatible // java serialization not supported in GWT. private static byte[] serializeWithBackReference(Object original, int handleOffset) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(original); byte[] handle = toByteArray(baseWireHandle + handleOffset); byte[] ref = prepended(TC_REFERENCE, handle); bos.write(ref); return bos.toByteArray(); }
Socket sock = new Socket(servername, port); OutputStream out = new BufferedOutputStream( sock.getOutputStream()); out.write(rmiCommand); out.write(endofline); out.write(endofline); ObjectOutputStream dout = new ObjectOutputStream(out); dout.writeInt(objectid); dout.writeInt(methodid); writeParameters(dout, args); dout.flush(); InputStream ins = new BufferedInputStream(sock.getInputStream()); skipHeader(ins); ObjectInputStream din = new ObjectInputStream(ins); result = din.readBoolean(); rvalue = null; errmsg = null; if (result) rvalue = din.readObject(); else errmsg = din.readUTF(); din.close(); dout.close(); sock.close();
@Override public <T extends Serializable> boolean write(String id, T object) { try { try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File(dir, id)))) { out.writeObject(object); return true; } } catch (IOException e) { return false; } } }
public T removeFirst(Class<T> clazz) throws IOException, ClassNotFoundException { if ( elementsInFile > 0 ) { if ( is == null ) { os.flush(); is = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file))); } Object object = is.readObject(); T element = clazz.cast(object); elementsInFile--; return element; } else { return list.removeFirst(); } }
throws IOException { try { Socket s = new Socket(host, port); try { ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream()); ObjectInputStream in = new ObjectInputStream(s.getInputStream()); int protocolVersion = in.readInt(); if (protocolVersion > 220) { throw new IOException( ". At most 220 was expected."); byte[] challenge = (byte[]) in.readObject(); MessageDigest md = MessageDigest.getInstance("SHA"); md.update(password.getBytes("UTF-8")); md.update(challenge); out.writeObject(md.digest()); return new LocalDebuggerProxy((Debugger) in.readObject());
public static ObjectOutputStream writeStreamFromString(String serializePath) throws IOException { ObjectOutputStream oos; if (serializePath.endsWith(".gz")) { oos = new ObjectOutputStream(new BufferedOutputStream( new GZIPOutputStream(new FileOutputStream(serializePath)))); } else { oos = new ObjectOutputStream(new BufferedOutputStream( new FileOutputStream(serializePath))); } return oos; }
public static void testSerialization(Object o) throws IOException { OutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(o); }
public void run() { try { ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream()); ObjectInputStream in = new ObjectInputStream(s.getInputStream()); byte[] challenge = new byte[512]; R.nextBytes(challenge); out.writeInt(220); // protocol version out.writeObject(challenge); MessageDigest md = MessageDigest.getInstance("SHA"); md.update(password); md.update(challenge); byte[] response = (byte[]) in.readObject(); if (Arrays.equals(response, md.digest())) { out.writeObject(debuggerStub); } else { out.writeObject(null); } } catch (Exception e) { LOG.warn("Connection to " + s.getInetAddress().getHostAddress() + " abruply broke", e); } }