Java Code Examples for java.io.ObjectOutputStream.writeObject()

The following are Jave code examples for showing how to use writeObject() of the java.io.ObjectOutputStream class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: openjdk-jdk10   File: ProxyClassDesc.java   View Source Code Vote up 6 votes
/**
 * Run benchmark for given number of cycles.
 */
void doReps(ObjectOutputStream oout, ObjectInputStream oin,
            StreamBuffer sbuf, ObjectStreamClass[] descs, int ncycles)
    throws Exception
{
    int ndescs = descs.length;
    for (int i = 0; i < ncycles; i++) {
        sbuf.reset();
        oout.reset();
        for (int j = 0; j < ndescs; j++) {
            oout.writeObject(descs[j]);
        }
        oout.flush();
        for (int j = 0; j < ndescs; j++) {
            oin.readObject();
        }
    }
}
 
Example 2
Project: jdk8u-jdk   File: CustomObjTrees.java   View Source Code Vote up 6 votes
/**
 * Run benchmark for given number of batches, with each batch containing
 * the given number of cycles.
 */
void doReps(ObjectOutputStream oout, ObjectInputStream oin,
            StreamBuffer sbuf, Node[] trees, int nbatches)
    throws Exception
{
    int ncycles = trees.length;
    for (int i = 0; i < nbatches; i++) {
        sbuf.reset();
        oout.reset();
        for (int j = 0; j < ncycles; j++) {
            oout.writeObject(trees[j]);
        }
        oout.flush();
        for (int j = 0; j < ncycles; j++) {
            oin.readObject();
        }
    }
}
 
Example 3
Project: OpenJSharp   File: PropertyChangeSupport.java   View Source Code Vote up 5 votes
/**
 * @serialData Null terminated list of <code>PropertyChangeListeners</code>.
 * <p>
 * At serialization time we skip non-serializable listeners and
 * only serialize the serializable listeners.
 */
private void writeObject(ObjectOutputStream s) throws IOException {
    Hashtable<String, PropertyChangeSupport> children = null;
    PropertyChangeListener[] listeners = null;
    synchronized (this.map) {
        for (Entry<String, PropertyChangeListener[]> entry : this.map.getEntries()) {
            String property = entry.getKey();
            if (property == null) {
                listeners = entry.getValue();
            } else {
                if (children == null) {
                    children = new Hashtable<>();
                }
                PropertyChangeSupport pcs = new PropertyChangeSupport(this.source);
                pcs.map.set(null, entry.getValue());
                children.put(property, pcs);
            }
        }
    }
    ObjectOutputStream.PutField fields = s.putFields();
    fields.put("children", children);
    fields.put("source", this.source);
    fields.put("propertyChangeSupportSerializedDataVersion", 2);
    s.writeFields();

    if (listeners != null) {
        for (PropertyChangeListener l : listeners) {
            if (l instanceof Serializable) {
                s.writeObject(l);
            }
        }
    }
    s.writeObject(null);
}
 
Example 4
Project: quickhybrid-android   File: IOUtil.java   View Source Code Vote up 5 votes
public static byte[] object2Byte(Object obj) {
    byte[] bytes = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        bytes = bos.toByteArray();
        oos.close();
        bos.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return bytes;
}
 
Example 5
Project: jaer   File: Histogram2DFilter.java   View Source Code Vote up 5 votes
public void saveHistogramToFile(File file) throws IOException, FileNotFoundException {
    log.info("Saving histogram data to " + file.getName());
    FileOutputStream fos = new FileOutputStream(file);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(histogram.length);
    oos.writeObject(histogram);
    oos.writeObject(bitmap);
    oos.writeFloat(threshold);
    oos.close();
    fos.close();
    log.info("histogram saved to file " + file.getPath());
    setFilePath(file.getPath());
}
 
Example 6
Project: datatree   File: JavaBuiltin.java   View Source Code Vote up 5 votes
public static final byte[] serialize(Object value, Object meta, boolean insertMeta) {
	if (value == null) {
		return new byte[0];
	}

	// Try to serialize content (type-safe serialization)
	if ((value == null || value instanceof Serializable) && (meta == null || meta instanceof Serializable)) {
		Map<Object, Object> map = insertMeta(value, meta, insertMeta);
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
			baos.write(1);
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			oos.writeObject(value);
			oos.flush();
			return baos.toByteArray();
		} catch (Throwable ignored) {
		} finally {
			removeMeta(map);
		}
	}

	// Write content as JSON
	byte[] bytes = TreeWriterRegistry.getWriter(null).toString(value, meta, false, insertMeta)
			.getBytes(StandardCharsets.UTF_8);
	byte[] copy = new byte[bytes.length + 1];
	System.arraycopy(bytes, 0, copy, 1, bytes.length);
	return copy;
}
 
Example 7
Project: weixin_api   File: Storage.java   View Source Code Vote up 5 votes
/**
 * 对象序列化转换为byte数组
 * 
 * @param obj
 * @return
 * @throws IOException
 */
byte[] object2bytes(Object obj) throws IOException {
	if (obj == null) {
		return null;
	}
	// 值对象转换为byte[]
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	oos.writeObject(obj);
	oos.close();
	byte[] bytes = baos.toByteArray();
	baos.close();
	return bytes;
}
 
Example 8
Project: effectiveJava   File: MutablePeriod.java   View Source Code Vote up 5 votes
public MutablePeriod() {
	try {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream out = new ObjectOutputStream(bos);

		// Serialize a valid Period instance
		out.writeObject(new Period(new Date(), new Date()));

		/*
		 * Append rogue "previous object refs" for internal Date fields in
		 * Period. For details, see "Java Object Serialization
		 * Specification," Section 6.4.
		 */
		byte[] ref = { 0x71, 0, 0x7e, 0, 5 }; // Ref #5
		bos.write(ref); // The start field
		ref[4] = 4; // Ref # 4
		bos.write(ref); // The end field

		// Deserialize Period and "stolen" Date references
		ObjectInputStream in = new ObjectInputStream(
				new ByteArrayInputStream(bos.toByteArray()));
		period = (Period) in.readObject();
		start = (Date) in.readObject();
		end = (Date) in.readObject();
	} catch (Exception e) {
		throw new AssertionError(e);
	}
}
 
Example 9
Project: monarch   File: MembershipAttributesAreSerializableTest.java   View Source Code Vote up 5 votes
/**
 * Assert that SubscriptionAttributes are serializable.
 */
@Test
public void testSubscriptionAttributesAreSerializable() throws Exception {
  SubscriptionAttributes outSA = new SubscriptionAttributes();
  ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
  ObjectOutputStream oos = new ObjectOutputStream(baos);
  oos.writeObject(outSA);

  byte[] data = baos.toByteArray();

  ByteArrayInputStream bais = new ByteArrayInputStream(data);
  ObjectInputStream ois = new ObjectInputStream(bais);
  SubscriptionAttributes inSA = (SubscriptionAttributes) ois.readObject();
  assertEquals(outSA, inSA);
}
 
Example 10
Project: boohee_v5.6   File: SerializableCookie.java   View Source Code Vote up 5 votes
private void writeObject(ObjectOutputStream out) throws IOException {
    out.writeObject(this.cookie.getName());
    out.writeObject(this.cookie.getValue());
    out.writeObject(this.cookie.getComment());
    out.writeObject(this.cookie.getDomain());
    out.writeObject(this.cookie.getExpiryDate());
    out.writeObject(this.cookie.getPath());
    out.writeInt(this.cookie.getVersion());
    out.writeBoolean(this.cookie.isSecure());
}
 
Example 11
Project: openjdk-jdk10   File: MapFactories.java   View Source Code Vote up 5 votes
@SuppressWarnings("unchecked")
static <T> T serialClone(T obj) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        oos.close();
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        return (T) ois.readObject();
    } catch (IOException | ClassNotFoundException e) {
        throw new AssertionError(e);
    }
}
 
Example 12
Project: smaph   File: WATRelatednessComputer.java   View Source Code Vote up 5 votes
public static synchronized void flush() throws FileNotFoundException,
		IOException {
	if (flushCounter > 0 && resultsCacheFilename != null) {
		LOG.info("Flushing relatedness cache... ");
		new File(resultsCacheFilename).createNewFile();
		ObjectOutputStream oos = new ObjectOutputStream(
				new FileOutputStream(resultsCacheFilename));
		oos.writeObject(instance);
		oos.close();
		LOG.info("Flushing relatedness cache done.");
	}
}
 
Example 13
Project: parabuild-ci   File: TransferCommon.java   View Source Code Vote up 5 votes
static void savePrefs(String f, DataAccessPoint sourceDb,
                      DataAccessPoint targetDb, Traceable tracer,
                      Vector tTable) {

    TransferTable t;

    try {
        FileOutputStream   fos = new FileOutputStream(f);
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        for (int i = 0; i < tTable.size(); i++) {
            t          = (TransferTable) tTable.elementAt(i);
            t.sourceDb = null;
            t.destDb   = null;
            t.tracer   = null;
        }

        oos.writeObject(tTable);

        for (int i = 0; i < tTable.size(); i++) {
            t          = (TransferTable) tTable.elementAt(i);
            t.tracer   = tracer;
            t.sourceDb = (TransferDb) sourceDb;
            t.destDb   = targetDb;
        }
    } catch (IOException e) {
        System.out.println("pb in SavePrefs : " + e.toString());
        e.printStackTrace();
    }
}
 
Example 14
Project: klavier   File: DatasetIO.java   View Source Code Vote up 5 votes
public static void writeSpectralAtomsAndEnvelopes(SpectralAtomsAndEnvelopes spectralAtomsAndEnvelopes, String path) {
	try {
		FileOutputStream fileOut = new FileOutputStream(path);
		ObjectOutputStream out = new ObjectOutputStream(fileOut);
		out.writeObject(spectralAtomsAndEnvelopes);
		out.close();
		fileOut.close();
	} catch(IOException i) {
		i.printStackTrace();
	}
}
 
Example 15
Project: logistimo-web-service   File: EventSummaryConfigModel.java   View Source Code Vote up 5 votes
public EventSummaryConfigModel copy() throws IOException, ClassNotFoundException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    objectOutputStream.writeObject(this);
    ByteArrayInputStream
        byteArrayInputStream =
        new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
    return (EventSummaryConfigModel) objectInputStream.readObject();

}
 
Example 16
Project: jdk8u-jdk   File: Scrollbar.java   View Source Code Vote up 3 votes
/**
 * Writes default serializable fields to stream.  Writes
 * a list of serializable <code>AdjustmentListeners</code>
 * as optional data. The non-serializable listeners are
 * detected and no attempt is made to serialize them.
 *
 * @param s the <code>ObjectOutputStream</code> to write
 * @serialData <code>null</code> terminated sequence of 0
 *   or more pairs; the pair consists of a <code>String</code>
 *   and an <code>Object</code>; the <code>String</code> indicates
 *   the type of object and is one of the following:
 *   <code>adjustmentListenerK</code> indicating an
 *     <code>AdjustmentListener</code> object
 *
 * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
 * @see java.awt.Component#adjustmentListenerK
 * @see #readObject(ObjectInputStream)
 */
private void writeObject(ObjectOutputStream s)
  throws IOException
{
  s.defaultWriteObject();

  AWTEventMulticaster.save(s, adjustmentListenerK, adjustmentListener);
  s.writeObject(null);
}
 
Example 17
Project: HCFCore   File: UnmodifiableSortedBag.java   View Source Code Vote up 2 votes
/**
 * Write the collection out using a custom routine.
 *
 * @param out  the output stream
 * @throws IOException
 */
private void writeObject(final ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();
    out.writeObject(decorated());
}
 
Example 18
Project: HCFCore   File: MultiKeyMap.java   View Source Code Vote up 2 votes
/**
 * Write the map out using a custom routine.
 *
 * @param out  the output stream
 * @throws IOException
 */
private void writeObject(final ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();
    out.writeObject(map);
}
 
Example 19
Project: HCFCore   File: TransformedMap.java   View Source Code Vote up 2 votes
/**
 * Write the map out using a custom routine.
 *
 * @param out  the output stream
 * @throws IOException
 * @since 3.1
 */
private void writeObject(final ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();
    out.writeObject(map);
}
 
Example 20
Project: openjdk-jdk10   File: Rdn.java   View Source Code Vote up 2 votes
/**
 * Serializes only the unparsed RDN, for compactness and to avoid
 * any implementation dependency.
 *
 * @serialData      The RDN string
 */
private void writeObject(ObjectOutputStream s)
        throws java.io.IOException {
    s.defaultWriteObject();
    s.writeObject(toString());
}