Example usage for java.io ObjectOutput close

List of usage examples for java.io ObjectOutput close

Introduction

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

Prototype

public void close() throws IOException;

Source Link

Document

Closes the stream.

Usage

From source file:net.brtly.monkeyboard.api.plugin.Bundle.java

/**
 * Convert a Bundle object to a byte array that can be deserialized by
 * {@link #fromByteArray(byte[])}/*w w w  .j av a 2 s . c o  m*/
 * 
 * @param b
 * @return
 * @throws IOException
 */
public static byte[] toByteArray(Bundle b) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(b);
        return bos.toByteArray();
    } finally {
        out.close();
        bos.close();
    }
}

From source file:com.splicemachine.mrio.api.SpliceTableMapReduceUtil.java

/**
 * Writes the given scan into a Base64 encoded string.
 *
 * @param scan  The scan to write out./*from  w  ww  .j a  v a  2  s  . c o m*/
 * @return The scan saved in a Base64 encoded string.
 * @throws IOException When writing the scan fails.
 */
public static String convertScanToString(Scan scan) throws IOException {
    ObjectOutput dos = null;
    try {
        byte[] bytes = ProtobufUtil.toScan(scan).toByteArray();
        String stringy = Base64.encodeBytes(bytes);
        return stringy;
    } finally {
        if (dos != null)
            dos.close();
    }
}

From source file:io.bitsquare.common.util.Utilities.java

public static byte[] serialize(Serializable object) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    byte[] result = null;
    try {//  w ww.  j  a  v  a 2 s. c  om
        out = new ObjectOutputStream(bos);
        out.writeObject(object);
        out.flush();
        result = bos.toByteArray().clone();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException ignore) {
        }
        try {
            bos.close();
        } catch (IOException ignore) {
        }
    }
    return result;
}

From source file:org.apache.stratos.cloud.controller.persist.Serializer.java

public static void serializeToFile(Object serializableObj, String filePath) throws IOException {

    File outFile = new File(filePath);
    ObjectOutput objOut = null;
    FileOutputStream fileOutputStream = null;

    try {//w w  w.  jav a  2s. c  om

        if (outFile.createNewFile()) {
            log.debug("Serialization file is created at " + filePath);
        } else {
            log.debug("Serialization file is already existing at " + filePath);
        }
        fileOutputStream = new FileOutputStream(outFile);
        objOut = new ObjectOutputStream(fileOutputStream);
        objOut.writeObject(serializableObj);

    } catch (IOException e) {
        log.error("Failed to serialize the object " + serializableObj.toString() + " to file " + filePath, e);
        throw e;

    } finally {
        if (objOut != null) {
            objOut.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
    }

}

From source file:com.swingtech.commons.util.ClassUtil.java

public static byte[] serializeObject(final Object object) {
    ObjectOutput out = null;
    byte[] buf = null;

    if (object == null) {
        return null;
    }// www.  j  a v a2s .  c om

    try {
        // Serialize to a byte array
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        out = new ObjectOutputStream(bos);
        out.writeObject(object);
        out.close();

        // Get the bytes of the serialized object
        buf = bos.toByteArray();
    } catch (final Exception e) {
        throw new RuntimeException("Error trying to serializeObject the following object:  " + object, e);
    }

    return buf;
}

From source file:net.carinae.dev.async.util.SerializerJavaImpl.java

/**
 * {@inheritDoc}//w w  w. j av a 2 s .co  m
 */
@Override
public byte[] serializeObject(Object obj) {

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutput out;
        out = new ObjectOutputStream(bos);
        out.writeObject(obj);
        out.close();

        byte[] buf = bos.toByteArray();
        return buf;
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Could not serialize object " + obj, e);
    }

}

From source file:org.mule.transport.legstar.test.lsfileae.LsfileaeHttpClientTest.java

/**
 * @return a serialized java request object in a byte array.
 * @throws IOException  if serialization fails
 *//*  ww w .  j a va2 s .  c o m*/
private byte[] getSerializedJavaRequest() throws IOException {
    ObjectFactory of = new ObjectFactory();
    Dfhcommarea dfhcommarea = of.createDfhcommarea();
    dfhcommarea.setComNumber(100L);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(bos);
    out.writeObject(dfhcommarea);
    out.close();
    return bos.toByteArray();
}

From source file:com.lhy.commons.encrypt.service.EncryptService.java

@Override
public File createLicenseFile(License license, String licenseFilePath) {
    License licObj = new License();
    licObj.setIpAddress(DigestUtils.sha512Hex(license.getIpAddress()));
    licObj.setLicenseID(license.getLicenseID());
    licObj.setLicenseType(license.getLicenseType());
    licObj.setStopTime(//  w  w  w. j av  a2s.c o m
            license.getStopTime() == null ? DateUtils.addDays(new Date(), 30) : license.getStopTime());
    File licenseFile = null;
    try {
        licenseFile = new File(licenseFilePath + File.separator + LicenseFileName);
        ObjectOutput out = new ObjectOutputStream(new FileOutputStream(licenseFile));
        out.writeObject(licObj);
        out.close();
    } catch (FileNotFoundException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    }
    return licenseFile;
}

From source file:com.conwet.silbops.model.ContextFunctionTest.java

@Test
public void shouldExternalize() throws Exception {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutput output = new ObjectOutputStream(baos);

    output.writeObject(function);/*www.j a  va  2 s. c o  m*/
    output.close();

    ObjectInput input = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));

    ContextFunction cxtFun = (ContextFunction) input.readObject();
    assertThat(cxtFun).isEqualTo(function);
}

From source file:com.gsma.mobileconnect.discovery.DiscoveryResponseTest.java

private DiscoveryResponse roundTripSerialize(DiscoveryResponse in) throws IOException, ClassNotFoundException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutput output = new ObjectOutputStream(baos);

    output.writeObject(in);/*  w ww . j a v  a2  s .co  m*/

    output.close();

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInput input = new ObjectInputStream(bais);

    DiscoveryResponse out = (DiscoveryResponse) input.readObject();

    input.close();

    return out;
}