當前位置: 首頁>>代碼示例>>Java>>正文


Java ObjectOutputStream類代碼示例

本文整理匯總了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());
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:26,代碼來源:NulFile.java

示例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();
  }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:22,代碼來源:ModelMBeanConstructorInfo.java

示例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;
    }
}
 
開發者ID:snowwolf10285,項目名稱:PicShow-zhaipin,代碼行數:28,代碼來源:DataHelper.java

示例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;
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:25,代碼來源:LongArrays.java

示例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);
        }
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:18,代碼來源:SerializationDeadlock.java

示例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);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:32,代碼來源:CertificateRevokedException.java

示例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]);
        }
    }
}
 
開發者ID:yippeesoft,項目名稱:NotifyTools,代碼行數:29,代碼來源:BeanContextSupport.java

示例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);
        }
    }
}
 
開發者ID:Alfresco,項目名稱:alfresco-core,代碼行數:24,代碼來源:SerializableTypeHandler.java

示例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);
        }
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:25,代碼來源:JPopupMenu.java

示例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();
  }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:22,代碼來源:ModelMBeanAttributeInfo.java

示例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 );
    }
}
 
開發者ID:apache,項目名稱:directory-ldap-api,代碼行數:20,代碼來源:AvaSerializationTest.java

示例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 );
    }
}
 
開發者ID:apache,項目名稱:directory-ldap-api,代碼行數:22,代碼來源:AvaSerializationTest.java

示例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);
	}
 
開發者ID:berkesa,項目名稱:datatree,代碼行數:25,代碼來源:TreeTest.java

示例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
}
 
開發者ID:parabuild-ci,項目名稱:parabuild-ci,代碼行數:27,代碼來源:DialValueIndicatorTests.java

示例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;

    }
 
開發者ID:Aarthas,項目名稱:android_Json2view,代碼行數:21,代碼來源:XViewBody.java


注:本文中的java.io.ObjectOutputStream類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。