本文整理汇总了Java中java.io.ObjectOutputStream类的典型用法代码示例。如果您正苦于以下问题:Java ObjectOutputStream类的具体用法?Java ObjectOutputStream怎么用?Java ObjectOutputStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ObjectOutputStream类属于java.io包,在下文中一共展示了ObjectOutputStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSerialization
import java.io.ObjectOutputStream; //导入依赖的package包/类
private static void testSerialization(File testFile) {
String path = testFile.getPath();
try {
// serialize test file
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(testFile);
oos.close();
// deserialize test file
byte[] bytes = baos.toByteArray();
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(is);
File newFile = (File) ois.readObject();
// test
String newPath = newFile.getPath();
if (!path.equals(newPath)) {
throw new RuntimeException(
"Serialization should not change file path");
}
test(newFile, false);
} catch (IOException | ClassNotFoundException ex) {
System.err.println("Exception happens in testSerialization");
System.err.println(ex.getMessage());
}
}
示例2: writeObject
import java.io.ObjectOutputStream; //导入依赖的package包/类
/**
* Serializes a {@link ModelMBeanConstructorInfo} to an {@link ObjectOutputStream}.
*/
private void writeObject(ObjectOutputStream out)
throws IOException {
if (compat)
{
// Serializes this instance in the old serial form
//
ObjectOutputStream.PutField fields = out.putFields();
fields.put("consDescriptor", consDescriptor);
fields.put("currClass", currClass);
out.writeFields();
}
else
{
// Serializes this instance in the new serial form
//
out.defaultWriteObject();
}
}
示例3: saveDeviceData
import java.io.ObjectOutputStream; //导入依赖的package包/类
/**
* 将对象储存到sharepreference
*
* @param key
* @param device
* @param <T>
*/
public static <T> boolean saveDeviceData(Context context, String key, T device) {
if (mSharedPreferences == null) {
mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try { //Device为自定义类
// 创建对象输出流,并封装字节流
ObjectOutputStream oos = new ObjectOutputStream(baos);
// 将对象写入字节流
oos.writeObject(device);
// 将字节流编码成base64的字符串
String oAuth_Base64 = new String(Base64.encode(baos
.toByteArray(), Base64.DEFAULT));
mSharedPreferences.edit().putString(key, oAuth_Base64).commit();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例4: run
import java.io.ObjectOutputStream; //导入依赖的package包/类
/**
* Write and read long arrays to/from a stream. The benchmark is run in
* batches, with each batch consisting of a fixed number of read/write
* cycles. The ObjectOutputStream is reset after each batch of cycles has
* completed.
* Arguments: <array size> <# batches> <# cycles per batch>
*/
public long run(String[] args) throws Exception {
int size = Integer.parseInt(args[0]);
int nbatches = Integer.parseInt(args[1]);
int ncycles = Integer.parseInt(args[2]);
long[][] arrays = new long[ncycles][size];
StreamBuffer sbuf = new StreamBuffer();
ObjectOutputStream oout =
new ObjectOutputStream(sbuf.getOutputStream());
ObjectInputStream oin =
new ObjectInputStream(sbuf.getInputStream());
doReps(oout, oin, sbuf, arrays, 1); // warmup
long start = System.currentTimeMillis();
doReps(oout, oin, sbuf, arrays, nbatches);
return System.currentTimeMillis() - start;
}
示例5: run
import java.io.ObjectOutputStream; //导入依赖的package包/类
public void run() {
try {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(vector);
oos.close();
} catch (final IOException ioe) {
addException(ioe);
} finally {
try {
testEnd.await();
} catch (Exception e) {
addException(e);
}
}
}
示例6: writeObject
import java.io.ObjectOutputStream; //导入依赖的package包/类
/**
* Serialize this {@code CertificateRevokedException} instance.
*
* @serialData the size of the extensions map (int), followed by all of
* the extensions in the map, in no particular order. For each extension,
* the following data is emitted: the OID String (Object), the criticality
* flag (boolean), the length of the encoded extension value byte array
* (int), and the encoded extension value bytes.
*/
private void writeObject(ObjectOutputStream oos) throws IOException {
// Write out the non-transient fields
// (revocationDate, reason, authority)
oos.defaultWriteObject();
// Write out the size (number of mappings) of the extensions map
oos.writeInt(extensions.size());
// For each extension in the map, the following are emitted (in order):
// the OID String (Object), the criticality flag (boolean), the length
// of the encoded extension value byte array (int), and the encoded
// extension value byte array. The extensions themselves are emitted
// in no particular order.
for (Map.Entry<String, Extension> entry : extensions.entrySet()) {
Extension ext = entry.getValue();
oos.writeObject(ext.getId());
oos.writeBoolean(ext.isCritical());
byte[] extVal = ext.getValue();
oos.writeInt(extVal.length);
oos.write(extVal);
}
}
示例7: serialize
import java.io.ObjectOutputStream; //导入依赖的package包/类
/**
* Serializes the given collection.
* <p>
* First writes a <code>int</code> indicating the number of all
* serializable elements (implements <code>Serializable</code>, then
* objects are writtern one by one.</p>
*
* @param oos the stream where the collection is writtern to
* @param collection the collection to serialize
* @throws IOException if I/O exception occurs
*/
protected final void serialize(ObjectOutputStream oos, Collection collection)
throws IOException {
Object array[] = collection.toArray();
int serCount = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] instanceof Serializable) {
serCount++;
}
}
oos.writeInt(serCount);
for (int i = 0; i < array.length; i++) {
if (array[i] instanceof Serializable) {
oos.writeObject(array[i]);
}
}
}
示例8: setParameter
import java.io.ObjectOutputStream; //导入依赖的package包/类
public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException
{
if (parameter == null)
{
ps.setNull(i, SerializableTypeHandler.serializableType);
}
else
{
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(parameter);
byte[] bytes = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ps.setBinaryStream(i, bais, bytes.length);
}
catch (Throwable e)
{
throw new SerializationException(e);
}
}
}
示例9: writeObject
import java.io.ObjectOutputStream; //导入依赖的package包/类
private void writeObject(ObjectOutputStream s) throws IOException {
Vector<Object> values = new Vector<Object>();
s.defaultWriteObject();
// Save the invoker, if its Serializable.
if(invoker != null && invoker instanceof Serializable) {
values.addElement("invoker");
values.addElement(invoker);
}
// Save the popup, if its Serializable.
if(popup != null && popup instanceof Serializable) {
values.addElement("popup");
values.addElement(popup);
}
s.writeObject(values);
if (getUIClassID().equals(uiClassID)) {
byte count = JComponent.getWriteObjCounter(this);
JComponent.setWriteObjCounter(this, --count);
if (count == 0 && ui != null) {
ui.installUI(this);
}
}
}
示例10: writeObject
import java.io.ObjectOutputStream; //导入依赖的package包/类
/**
* Serializes a {@link ModelMBeanAttributeInfo} to an {@link ObjectOutputStream}.
*/
private void writeObject(ObjectOutputStream out)
throws IOException {
if (compat)
{
// Serializes this instance in the old serial form
//
ObjectOutputStream.PutField fields = out.putFields();
fields.put("attrDescriptor", attrDescriptor);
fields.put("currClass", currClass);
out.writeFields();
}
else
{
// Serializes this instance in the new serial form
//
out.defaultWriteObject();
}
}
示例11: testNullUpValueStaticSerialization
import java.io.ObjectOutputStream; //导入依赖的package包/类
@Test
public void testNullUpValueStaticSerialization() throws LdapException, IOException, ClassNotFoundException
{
Ava atav = new Ava( schemaManager, "DC", ( String ) null );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream( baos );
try
{
atav.writeExternal( out );
fail();
}
catch ( IOException ioe )
{
String message = ioe.getMessage();
assertEquals( "Cannot serialize a wrong ATAV, the value should not be null", message );
}
}
示例12: testNullAtavSerialization
import java.io.ObjectOutputStream; //导入依赖的package包/类
/**
* Test serialization of a simple ATAV
*/
@Test
public void testNullAtavSerialization() throws LdapException, IOException, ClassNotFoundException
{
Ava atav = new Ava();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream( baos );
try
{
atav.writeExternal( out );
fail();
}
catch ( IOException ioe )
{
assertTrue( true );
}
}
示例13: testSerializationAndCloning
import java.io.ObjectOutputStream; //导入依赖的package包/类
private final void testSerializationAndCloning(Tree node) throws Exception {
// Serialize
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(node);
oos.flush();
byte[] bytes = baos.toByteArray();
// System.out.println(new String(bytes));
// Deserialize
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
Tree copy = (Tree) ois.readObject();
String txtOriginal = node.toString("debug");
String txtCopy = copy.toString("debug");
assertEquals(txtOriginal, txtCopy);
// Cloning
txtCopy = node.clone().toString("debug");
assertEquals(txtOriginal, txtCopy);
}
示例14: testSerialization
import java.io.ObjectOutputStream; //导入依赖的package包/类
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
// test a default instance
DialValueIndicator i1 = new DialValueIndicator(0, "Label");
DialValueIndicator i2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(i1);
out.close();
ObjectInput in = new ObjectInputStream(
new ByteArrayInputStream(buffer.toByteArray()));
i2 = (DialValueIndicator) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
assertEquals(i1, i2);
// test a custom instance
}
示例15: deepClone
import java.io.ObjectOutputStream; //导入依赖的package包/类
public XViewBody deepClone() {
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(this);
ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi = new ObjectInputStream(bi);
Object o = oi.readObject();
oi.close();
bi.close();
return (XViewBody) o;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}