Example usage for java.io ObjectOutputStream write

List of usage examples for java.io ObjectOutputStream write

Introduction

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

Prototype

public void write(byte[] buf) throws IOException 

Source Link

Document

Writes an array of bytes.

Usage

From source file:mitm.application.djigzo.james.Certificates.java

private void writeObject(ObjectOutputStream out) throws IOException {
    try {/*  ww w.  ja va 2  s . c o  m*/
        out.writeLong(serialVersionUID);

        /*
         * Write the number of certificates so we know how many we have to read when deserializing.
         */
        out.writeInt(certificates.size());

        for (X509Certificate certificate : certificates) {
            byte[] encoded = certificate.getEncoded();
            /* 
             * write the size of the encoded certificate so we can restore it 
             */
            out.writeInt(encoded.length);
            out.write(certificate.getEncoded());
        }
    } catch (CertificateEncodingException e) {
        throw new IOException(e);
    }
}

From source file:com.github.jsonj.JsonObject.java

void writeObject(java.io.ObjectOutputStream out) throws IOException {
    // when using object serialization, write the json bytes
    byte[] bytes = toString().getBytes(UTF8);
    out.writeInt(bytes.length);//from w  ww  .j a  v a  2 s .  c om
    out.write(bytes);

}

From source file:net.sf.jasperreports.engine.base.ElementsBlock.java

private void writeObject(java.io.ObjectOutputStream out) throws IOException {
    lockContext();//from w w w  .  j  ava  2s  .c om
    try {
        ensureDataAndTouch();
        beforeExternalization();

        try {
            // maybe we should no longer serialize the id, as a new one is
            // generated on deserialization
            out.writeObject(uid);
            out.writeObject(context);

            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            VirtualizationObjectOutputStream stream = new VirtualizationObjectOutputStream(bout, context);
            stream.writeObject(elements);
            stream.flush();

            byte[] bytes = bout.toByteArray();
            out.writeInt(bytes.length);
            out.write(bytes);
        } finally {
            afterExternalization();
        }
    } finally {
        unlockContext();
    }
}

From source file:org.mule.DefaultMuleMessage.java

private void writeObject(ObjectOutputStream out) throws Exception {
    out.defaultWriteObject();/*from w  ww  .j  av a  2  s  .c  o m*/
    if (payload instanceof Serializable) {
        out.writeBoolean(true);
        out.writeObject(payload);
    } else {
        out.writeBoolean(false);
        byte[] serializablePayload = getPayloadAsBytes();
        out.writeInt(serializablePayload.length);
        out.write(serializablePayload);
    }
    out.writeObject(serializeAttachments(inboundAttachments));
    out.writeObject(serializeAttachments(outboundAttachments));

    // TODO: we don't serialize the originalPayload for now
}

From source file:com.healthmarketscience.rmiio.DirectRemoteInputStream.java

/**
 * Serializes this object and all of the underlying stream's data directly
 * to the given ObjectOutputStream./*from  ww  w .  jav a 2  s .  c o m*/
 * 
 * @serialData the compression status of the stream, followed by the default
 *             chunk size for the serialized stream data (int), followed by
 *             chunks of the underlying stream. each chunk has a chunk code
 *             which indicates how to handle it's length (either default,
 *             explicit as int, or EOF), and then the specified number of
 *             bytes if not EOF.
 */
private void writeObject(ObjectOutputStream out) throws IOException {
    switch (_consumptionState) {
    case NONE:
        // this is the required state
        break;
    case LOCAL:
    case SERIAL:
        throw new NotSerializableException(getClass().getName()
                + " (underlying stream has already been consumed, type: " + _consumptionState + ")");
    default:
        throw new RuntimeException("unknown state " + _consumptionState);
    }

    out.defaultWriteObject();

    // once we start consuming the inputstream, we can't rewrite it
    _consumptionState = ConsumptionState.SERIAL;

    final int defaultChunkSize = RemoteInputStreamServer.DEFAULT_CHUNK_SIZE;

    // note, we create RemoteInputStreamServer instances, but we do not
    // actually export them.
    RemoteInputStreamServer server = null;
    try {
        if (_compress && (_tmpFile == null)) {
            // this is the first time the data is being read, and we need to
            // compress it as we read it.
            server = new GZIPRemoteInputStream(_in, _monitor, defaultChunkSize);
        } else {
            // we are re-serializing a previously serialized stream, so the data
            // is already compressed (if compression was desired)
            server = new SimpleRemoteInputStream(_in, _monitor, defaultChunkSize);
        }

        // record the default chunk size
        out.writeInt(defaultChunkSize);

        int packetId = RemoteStreamServer.INITIAL_VALID_SEQUENCE_ID;
        while (true) {

            byte[] packet = server.readPacket(packetId++);

            if (packet != null) {
                if (packet.length > 0) {
                    // we have a packet with data, write it to the output stream. if
                    // the packet is a different length, record the length.
                    if (packet.length == defaultChunkSize) {
                        out.write(DEFAULT_CHUNK_CODE);
                    } else {
                        out.write(CUSTOM_CHUNK_CODE);
                        out.writeInt(packet.length);
                    }
                    out.write(packet);
                }
            } else {
                // reached end of stream, indicate this
                out.write(EOF_CODE);
                break;
            }

        }

        // local stream is exhausted
        _gotEOF = true;

        // indicate successful read
        try {
            server.close(true);
        } catch (IOException e) {
            // log, but ignore failures here
            if (LOG.isDebugEnabled()) {
                LOG.debug("Failed closing server", e);
            }
        }

    } finally {
        RmiioUtil.closeQuietly(server);
        RmiioUtil.closeQuietly(this);
    }
}