Example usage for java.io ObjectOutputStream close

List of usage examples for java.io ObjectOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the stream.

Usage

From source file:de.tudarmstadt.ukp.dkpro.wsd.si.dictionary.GoogleDictionaryInventory.java

public GoogleDictionaryInventory(String inputPath, String serializiblePath, String neededMentionsPath)
        throws FileNotFoundException, IOException {
    // Open the serialized version or create it from scratch
    try {/* ww w  .j a  v a 2  s.  c om*/
        logger.debug("Trying to load dictionary " + this.getClass().getSimpleName() + " from serializable.");
        ObjectInputStream dictionaryReader = new ObjectInputStream(
                new BZip2CompressorInputStream(new FileInputStream(serializiblePath)));
        dictionary = (GoogleDictionary) dictionaryReader.readObject();
        dictionaryReader.close();
        logger.debug("Loaded dictionary " + this.getClass().getSimpleName() + " from serializable.");
    } catch (Exception e) {
        logger.debug("Trying to load dictionary " + this.getClass().getSimpleName() + " from input.");
        dictionary = new GoogleDictionary(inputPath, neededMentionsPath);
        System.out.println("Loaded dictionary " + this.getClass().getSimpleName() + " from input.");

        ObjectOutputStream dictionaryWriter = new ObjectOutputStream(
                new BZip2CompressorOutputStream(new FileOutputStream(serializiblePath)));
        dictionaryWriter.writeObject(dictionary);
        dictionaryWriter.close();
        logger.debug("Stored dictionary " + this.getClass().getSimpleName() + " in serializable.");

    }
}

From source file:com.thoughtworks.acceptance.WriteReplaceTest.java

public void testReplacesAndResolves() throws IOException, ClassNotFoundException {
    Thing thing = new Thing(3, 6);

    // ensure that Java serialization does not cause endless loop for a Thing
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(thing);//from w ww .j  a  v a 2  s .  c o m
    oos.close();

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ios = new ObjectInputStream(bais);
    assertEquals(thing, ios.readObject());
    ios.close();

    // ensure that XStream does not cause endless loop for a Thing
    xstream.alias("thing", Thing.class);

    String expectedXml = "" + "<thing>\n" + "  <a>3000</a>\n" + "  <b>6000</b>\n" + "</thing>";

    assertBothWays(thing, expectedXml);
}

From source file:de.javakaffee.web.msm.serializer.jackson.JacksonTranscoderTest.java

private StandardSession javaRoundtrip(final StandardSession session,
        final MemcachedBackupSessionManager manager) throws IOException, ClassNotFoundException {

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(bos);
    session.writeObjectData(oos);/*from   www .j ava 2 s  .  c o  m*/
    oos.close();
    bos.close();

    final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    final ObjectInputStream ois = new ObjectInputStream(bis);
    final StandardSession readSession = manager.createEmptySession();
    readSession.readObjectData(ois);
    ois.close();
    bis.close();

    return readSession;
}

From source file:com.servioticy.queueclient.KestrelThriftClient.java

protected byte[] serialize(Object o) {
    if (o == null) {
        throw new NullPointerException("Can't serialize null");
    }//from  w w  w  . ja  v  a 2s  .  c o m
    byte[] rv = null;
    ByteArrayOutputStream bos = null;
    ObjectOutputStream os = null;
    try {
        bos = new ByteArrayOutputStream();
        os = new ObjectOutputStream(bos);
        os.writeObject(o);
        os.close();
        bos.close();
        rv = bos.toByteArray();
    } catch (IOException e) {
        logger.warn("Non-serializable object", e);
    }
    return rv;
}

From source file:net.sf.ehcache.ElementTest.java

License:asdf

/**
 * Tests the deserialization performance of an element containing a large byte[]
 *
 * @throws IOException//from w  w  w .  j a  v a  2  s .  c o m
 * @throws ClassNotFoundException
 */
public void testDeserializationPerformance() throws IOException, ClassNotFoundException {

    byte[] value = getTestByteArray();
    Element element = new Element("test", value);

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bout);
    oos.writeObject(element);
    byte[] serializedValue = bout.toByteArray();
    oos.close();
    StopWatch stopWatch = new StopWatch();
    for (int i = 0; i < 100; i++) {
        ByteArrayInputStream bin = new ByteArrayInputStream(serializedValue);
        ObjectInputStream ois = new ObjectInputStream(bin);
        ois.readObject();
        ois.close();
    }
    long elapsed = stopWatch.getElapsedTime() / 100;
    LOG.info("In-memory size in bytes: " + serializedValue.length + " time to deserialize in ms: " + elapsed);
    assertTrue(elapsed < 30);
}

From source file:FileUserAccess.java

@SuppressWarnings("resource")
@Override// w w w  .j ava 2 s .  c  om
public boolean saveNewUser(User newUser) {
    FileOutputStream fout;
    try {
        fout = new FileOutputStream(
                System.getProperty("user.dir") + File.separator + newUser.getUsername() + ".userobj", false);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(FileUserAccess.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }
    ObjectOutputStream oos;
    try {
        oos = new ObjectOutputStream(fout);
    } catch (IOException ex) {
        Logger.getLogger(FileUserAccess.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }

    try {
        oos.writeObject(newUser);
        oos.close();
    } catch (IOException ex) {
        Logger.getLogger(FileUserAccess.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }
    return true;
}

From source file:ch.rgw.tools.StringTool.java

static private String ObjectToString(final Object o) {
    if (o instanceof String) {
        return "A" + (String) o;
    }/*  w w  w. j a v  a2  s .c om*/
    if (o instanceof Integer) {
        return "B" + ((Integer) o).toString();
    }
    if (o instanceof Serializable) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos;
        try {
            oos = new ObjectOutputStream(baos);
            oos.writeObject(o);
            oos.close();
            byte[] ret = baos.toByteArray();
            return "Z" + enPrintable(ret);

        } catch (IOException e) {
            ExHandler.handle(e);
            return null;
        }
    }
    return null;
}

From source file:com.npower.unicom.sync.AbstractExportDaemonPlugIn.java

/**
 * //from   w w  w .  java 2 s. c  o m
 * @param date
 */
private void updateLastSyncTimeStamp(Date date) {
    File requestDir = this.getRequestDir();
    File file = new File(requestDir, AbstractExportDaemonPlugIn.FILENAME_LAST_SYNC_TIME_STAMP);
    try {
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
        out.writeObject(date);
        out.close();
    } catch (FileNotFoundException e) {
        log.error("failure to update last sync time stamp: " + e.getMessage(), e);
    } catch (IOException e) {
        log.error("failure to update last sync time stamp: " + e.getMessage(), e);
    }
}

From source file:com.taobao.metamorphosis.utils.codec.impl.JavaSerializer.java

/**
 * @see com.taobao.notify.codec.Serializer#encodeObject(Object)
 */// w  w  w.ja  va2  s .co  m
@Override
public byte[] encodeObject(final Object objContent) throws IOException {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream output = null;
    try {
        baos = new ByteArrayOutputStream(1024);
        output = new ObjectOutputStream(baos);
        output.writeObject(objContent);
    } catch (final IOException ex) {
        throw ex;

    } finally {
        if (output != null) {
            try {
                output.close();
                if (baos != null) {
                    baos.close();
                }
            } catch (final IOException ex) {
                this.logger.error("Failed to close stream.", ex);
            }
        }
    }
    return baos != null ? baos.toByteArray() : null;
}

From source file:net.nicholaswilliams.java.licensing.ObjectSerializer.java

/**
 * Serializes the {@link Serializable} object passed and returns it as a byte array.
 *
 * @param object The object to serialize
 * @return the byte stream with the object serialized in it.
 * @throws ObjectSerializationException if an I/O exception occurs while serializing the object.
 *//*from   w  ww  .  j  a  v  a  2s. c  om*/
public final byte[] writeObject(Serializable object) throws ObjectSerializationException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();

    ObjectOutputStream stream = null;
    try {
        stream = new ObjectOutputStream(bytes);
        stream.writeObject(object);
    } catch (IOException e) {
        throw new ObjectSerializationException(e);
    } finally {
        try {
            if (stream != null)
                stream.close();
        } catch (IOException ignore) {
        }
    }

    return bytes.toByteArray();
}