Example usage for java.io ObjectOutputStream writeObject

List of usage examples for java.io ObjectOutputStream writeObject

Introduction

In this page you can find the example usage for java.io ObjectOutputStream writeObject.

Prototype

public final void writeObject(Object obj) throws IOException 

Source Link

Document

Write the specified object to the ObjectOutputStream.

Usage

From source file:net.schweerelos.parrot.ui.NodeWrapperPersistentLayoutImpl.java

@Override
public synchronized void persist(String fileName) throws IOException {
    uriToNodeLocation.clear();// w  ww  .  j  a va  2s .  co m

    for (NodeWrapper vertex : getGraph().getVertices()) {
        Point p = new Point(transform(vertex));
        uriToNodeLocation.put(vertex.getOntResource().getURI(), p);
    }

    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));
    oos.writeObject(uriToNodeLocation);
    oos.close();
}

From source file:com.adobe.acs.commons.httpcache.config.impl.keys.GroupCacheKey.java

/** For Serialization **/
private void writeObject(ObjectOutputStream o) throws IOException {
    parentWriteObject(o);/*from   w ww. j  a  v  a  2  s  .  c  om*/
    final Object[] userGroupArray = cacheKeyUserGroups.toArray();
    o.writeObject(StringUtils.join(userGroupArray, ","));
}

From source file:com.google.u2f.gaedemo.impl.DataStoreImpl.java

@Override
public String storeSessionData(EnrollSessionData sessionData) {

    SecretKey key = new SecretKeySpec(SecretKeys.get().sessionEncryptionKey(), "AES");
    byte[] ivBytes = new byte[16];
    random.nextBytes(ivBytes);/*from www. j a v a 2  s  . c  o m*/
    final IvParameterSpec IV = new IvParameterSpec(ivBytes);
    Cipher cipher;
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, IV);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | InvalidAlgorithmParameterException e) {
        throw new RuntimeException(e);
    }

    SealedObject sealed;
    try {
        sealed = new SealedObject(sessionData, cipher);
    } catch (IllegalBlockSizeException | IOException e) {
        throw new RuntimeException(e);
    }

    ByteArrayOutputStream out;
    try {
        out = new ByteArrayOutputStream();
        ObjectOutputStream outer = new ObjectOutputStream(out);

        outer.writeObject(sealed);
        outer.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return Base64.encodeBase64URLSafeString(out.toByteArray());
}

From source file:de.scoopgmbh.copper.test.versioning.compatibility.TestJavaSerializer.java

private String serialize(final Object o) throws IOException {
    if (o == null)
        return null;
    final ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    final ObjectOutputStream oos = new ObjectOutputStream(baos);

    oos.writeObject(o);
    oos.close();//from   ww  w.  j  a va2 s . c om
    baos.close();
    byte[] data = baos.toByteArray();
    final ByteArrayOutputStream baos2 = new ByteArrayOutputStream(1024);
    baos2.write(data, 0, 8);
    baos2.write(classNameReplacement.getBytes("UTF-8"));
    int offset = classNameReplacement.length() + 8;
    baos2.write(data, offset, data.length - offset);
    baos2.flush();
    baos2.close();
    data = baos2.toByteArray();
    boolean isCompressed = false;
    if (compress && compressThresholdSize <= data.length && data.length <= compressorMaxSize) {
        data = compressorTL.get().compress(data);
        isCompressed = true;
    }
    final String encoded = new Base64().encodeToString(data);
    final StringBuilder sb = new StringBuilder(encoded.length() + 4);
    sb.append(isCompressed ? 'C' : 'U').append(encoded);
    return sb.toString();
}

From source file:com.newrelic.agent.deps.org.apache.http.impl.client.BasicAuthCache.java

@Override
public void put(final HttpHost host, final AuthScheme authScheme) {
    Args.notNull(host, "HTTP host");
    if (authScheme == null) {
        return;//w  ww.j  a v  a 2 s.  c o m
    }
    if (authScheme instanceof Serializable) {
        try {
            final ByteArrayOutputStream buf = new ByteArrayOutputStream();
            final ObjectOutputStream out = new ObjectOutputStream(buf);
            out.writeObject(authScheme);
            out.close();
            this.map.put(getKey(host), buf.toByteArray());
        } catch (IOException ex) {
            if (log.isWarnEnabled()) {
                log.warn("Unexpected I/O error while serializing auth scheme", ex);
            }
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Auth scheme " + authScheme.getClass() + " is not serializable");
        }
    }
}

From source file:edu.caltechUcla.sselCassel.projects.jMarkets.server.network.ServletReceiver.java

/** Send the given reponse to the client waiting for the given HttpServletResponse */
protected void respond(HttpServletResponse r, Response res) {
    try {//from   w w  w .j av a  2 s .co  m
        OutputStream out = r.getOutputStream();
        ObjectOutputStream outputToApplet = new ObjectOutputStream(out);
        outputToApplet.writeObject(res);

        outputToApplet.flush();
        outputToApplet.close();
    } catch (IOException e) {
        log.error("Failed to respond to request", e);
    }
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.comments.pipeline.VocabularyCollector.java

@Override
public void collectionProcessComplete() throws AnalysisEngineProcessException {
    super.collectionProcessComplete();

    getLogger().info("Original vocabulary size: " + this.vocabulary.size());

    // remove all with low occurrence
    Iterator<Map.Entry<String, Integer>> iterator = vocabulary.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, Integer> next = iterator.next();

        // remove
        if (next.getValue() < this.minimalOccurrence) {
            iterator.remove();/*  www .  ja v  a  2s. c o  m*/
        }
    }

    getLogger().info("Filtered vocabulary size: " + this.vocabulary.size());

    // serialize to file
    try {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(this.modelLocation));
        oos.writeObject(vocabulary);
    } catch (IOException e) {
        throw new AnalysisEngineProcessException(e);
    }
}

From source file:geva.Main.State.java

/**
 * Save the State//  www.j  av  a  2  s . c  om
 */
@SuppressWarnings({ "IOResourceOpenedButNotSafelyClosed" })
public void save() {
    FileOutputStream fOut;
    ObjectOutputStream oOut;
    try {
        fOut = new FileOutputStream("saveFile");
        oOut = new ObjectOutputStream(fOut);
        oOut.writeObject(this);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:gov.nih.nci.caarray.util.CaArrayUtilsTest.java

@Test
public void testSerializeDeserialize2() throws IOException, ClassNotFoundException {
    QuantitationType qt = new QuantitationType();
    qt.setDataType(DataType.FLOAT);//  w w  w  .ja  va  2 s. co m
    qt.setName("Foo");
    FloatColumn fc = new FloatColumn();
    fc.setQuantitationType(qt);
    fc.setValues(TEST_FLOAT_ARRAY);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(fc);
    oos.flush();
    byte[] serialized = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(serialized);
    ObjectInputStream ois = new ObjectInputStream(bais);
    FloatColumn in = (FloatColumn) ois.readObject();
    assertEquals(qt, in.getQuantitationType());
    float[] deserialized = in.getValues();
    assertEquals(TEST_FLOAT_ARRAY.length, deserialized.length);
    for (int i = 0; i < TEST_FLOAT_ARRAY.length; i++) {
        assertEquals(TEST_FLOAT_ARRAY[i], deserialized[i], 0);
    }
}

From source file:com.smartitengineering.cms.content.api.impl.IdTest.java

public void testContentTypeIdSerialization() throws IOException, ClassNotFoundException {
    ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
    ObjectOutputStream outputStream = new ObjectOutputStream(baoStream);
    outputStream.writeObject(contentTypeId);
    IOUtils.closeQuietly(outputStream);/*  w ww. java  2  s . com*/
    ByteArrayInputStream baiStream = new ByteArrayInputStream(baoStream.toByteArray());
    ObjectInputStream inputStream = new ObjectInputStream(baiStream);
    assertEquals(contentTypeId, inputStream.readObject());
    baoStream = new ByteArrayOutputStream();
    DataOutputStream dataOutputStream = new DataOutputStream(baoStream);
    contentTypeId.writeExternal(dataOutputStream);
    IOUtils.closeQuietly(dataOutputStream);
    assertEquals(contentTypeId.toString(), baoStream.toString());
    ContentTypeIdImpl idImpl = new ContentTypeIdImpl();
    idImpl.readExternal(new DataInputStream(new ByteArrayInputStream(baoStream.toByteArray())));
    assertEquals(contentTypeId, idImpl);
}