本文整理汇总了Java中java.io.ObjectOutputStream.writeObject方法的典型用法代码示例。如果您正苦于以下问题:Java ObjectOutputStream.writeObject方法的具体用法?Java ObjectOutputStream.writeObject怎么用?Java ObjectOutputStream.writeObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.ObjectOutputStream
的用法示例。
在下文中一共展示了ObjectOutputStream.writeObject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doReps
import java.io.ObjectOutputStream; //导入方法依赖的package包/类
/**
* 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();
}
}
}
示例2: doReps
import java.io.ObjectOutputStream; //导入方法依赖的package包/类
/**
* 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();
}
}
}
示例3: serialize
import java.io.ObjectOutputStream; //导入方法依赖的package包/类
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;
}
示例4: writeObject
import java.io.ObjectOutputStream; //导入方法依赖的package包/类
/**
* @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);
}
示例5: object2Byte
import java.io.ObjectOutputStream; //导入方法依赖的package包/类
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;
}
示例6: saveHistogramToFile
import java.io.ObjectOutputStream; //导入方法依赖的package包/类
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());
}
示例7: object2bytes
import java.io.ObjectOutputStream; //导入方法依赖的package包/类
/**
* 对象序列化转换为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;
}
示例8: MutablePeriod
import java.io.ObjectOutputStream; //导入方法依赖的package包/类
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);
}
}
示例9: testSubscriptionAttributesAreSerializable
import java.io.ObjectOutputStream; //导入方法依赖的package包/类
/**
* 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);
}
示例10: writeObject
import java.io.ObjectOutputStream; //导入方法依赖的package包/类
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());
}
示例11: serialClone
import java.io.ObjectOutputStream; //导入方法依赖的package包/类
@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);
}
}
示例12: flush
import java.io.ObjectOutputStream; //导入方法依赖的package包/类
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.");
}
}
示例13: savePrefs
import java.io.ObjectOutputStream; //导入方法依赖的package包/类
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();
}
}
示例14: writeSpectralAtomsAndEnvelopes
import java.io.ObjectOutputStream; //导入方法依赖的package包/类
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();
}
}
示例15: copy
import java.io.ObjectOutputStream; //导入方法依赖的package包/类
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();
}