Example usage for java.io ObjectOutputStream writeInt

List of usage examples for java.io ObjectOutputStream writeInt

Introduction

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

Prototype

public void writeInt(int val) throws IOException 

Source Link

Document

Writes a 32 bit int.

Usage

From source file:org.hibernate.engine.StatefulPersistenceContext.java

/**
 * Used by the owning session to explicitly control serialization of the
 * persistence context./*  ww w .  ja  v  a  2 s  . co m*/
 *
 * @param oos The stream to which the persistence context should get written
 * @throws IOException serialization errors.
 */
public void serialize(ObjectOutputStream oos) throws IOException {
    log.trace("serializing persistent-context");

    oos.writeBoolean(defaultReadOnly);
    oos.writeBoolean(hasNonReadOnlyEntities);

    oos.writeInt(entitiesByKey.size());
    log.trace("starting serialization of [" + entitiesByKey.size() + "] entitiesByKey entries");
    Iterator itr = entitiesByKey.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        ((EntityKey) entry.getKey()).serialize(oos);
        oos.writeObject(entry.getValue());
    }

    oos.writeInt(entitiesByUniqueKey.size());
    log.trace("starting serialization of [" + entitiesByUniqueKey.size() + "] entitiesByUniqueKey entries");
    itr = entitiesByUniqueKey.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        ((EntityUniqueKey) entry.getKey()).serialize(oos);
        oos.writeObject(entry.getValue());
    }

    oos.writeInt(proxiesByKey.size());
    log.trace("starting serialization of [" + proxiesByKey.size() + "] proxiesByKey entries");
    itr = proxiesByKey.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        ((EntityKey) entry.getKey()).serialize(oos);
        oos.writeObject(entry.getValue());
    }

    oos.writeInt(entitySnapshotsByKey.size());
    log.trace("starting serialization of [" + entitySnapshotsByKey.size() + "] entitySnapshotsByKey entries");
    itr = entitySnapshotsByKey.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        ((EntityKey) entry.getKey()).serialize(oos);
        oos.writeObject(entry.getValue());
    }

    oos.writeInt(entityEntries.size());
    log.trace("starting serialization of [" + entityEntries.size() + "] entityEntries entries");
    itr = entityEntries.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        oos.writeObject(entry.getKey());
        ((EntityEntry) entry.getValue()).serialize(oos);
    }

    oos.writeInt(collectionsByKey.size());
    log.trace("starting serialization of [" + collectionsByKey.size() + "] collectionsByKey entries");
    itr = collectionsByKey.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        ((CollectionKey) entry.getKey()).serialize(oos);
        oos.writeObject(entry.getValue());
    }

    oos.writeInt(collectionEntries.size());
    log.trace("starting serialization of [" + collectionEntries.size() + "] collectionEntries entries");
    itr = collectionEntries.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        oos.writeObject(entry.getKey());
        ((CollectionEntry) entry.getValue()).serialize(oos);
    }

    oos.writeInt(arrayHolders.size());
    log.trace("starting serialization of [" + arrayHolders.size() + "] arrayHolders entries");
    itr = arrayHolders.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        oos.writeObject(entry.getKey());
        oos.writeObject(entry.getValue());
    }

    oos.writeInt(nullifiableEntityKeys.size());
    log.trace("starting serialization of [" + nullifiableEntityKeys.size() + "] nullifiableEntityKeys entries");
    itr = nullifiableEntityKeys.iterator();
    while (itr.hasNext()) {
        EntityKey entry = (EntityKey) itr.next();
        entry.serialize(oos);
    }
}

From source file:org.hibernate.engine.internal.StatefulPersistenceContext.java

/**
 * Used by the owning session to explicitly control serialization of the
 * persistence context.//from  www. j ava2  s  . co  m
 *
 * @param oos The stream to which the persistence context should get written
 * @throws IOException serialization errors.
 */
public void serialize(ObjectOutputStream oos) throws IOException {
    LOG.trace("Serializing persistent-context");

    oos.writeBoolean(defaultReadOnly);
    oos.writeBoolean(hasNonReadOnlyEntities);

    oos.writeInt(entitiesByKey.size());
    LOG.trace("Starting serialization of [" + entitiesByKey.size() + "] entitiesByKey entries");
    Iterator itr = entitiesByKey.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        ((EntityKey) entry.getKey()).serialize(oos);
        oos.writeObject(entry.getValue());
    }

    oos.writeInt(entitiesByUniqueKey.size());
    LOG.trace("Starting serialization of [" + entitiesByUniqueKey.size() + "] entitiesByUniqueKey entries");
    itr = entitiesByUniqueKey.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        ((EntityUniqueKey) entry.getKey()).serialize(oos);
        oos.writeObject(entry.getValue());
    }

    oos.writeInt(proxiesByKey.size());
    LOG.trace("Starting serialization of [" + proxiesByKey.size() + "] proxiesByKey entries");
    itr = proxiesByKey.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        ((EntityKey) entry.getKey()).serialize(oos);
        oos.writeObject(entry.getValue());
    }

    oos.writeInt(entitySnapshotsByKey.size());
    LOG.trace("Starting serialization of [" + entitySnapshotsByKey.size() + "] entitySnapshotsByKey entries");
    itr = entitySnapshotsByKey.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        ((EntityKey) entry.getKey()).serialize(oos);
        oos.writeObject(entry.getValue());
    }

    oos.writeInt(entityEntries.size());
    LOG.trace("Starting serialization of [" + entityEntries.size() + "] entityEntries entries");
    itr = entityEntries.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        oos.writeObject(entry.getKey());
        ((EntityEntry) entry.getValue()).serialize(oos);
    }

    oos.writeInt(collectionsByKey.size());
    LOG.trace("Starting serialization of [" + collectionsByKey.size() + "] collectionsByKey entries");
    itr = collectionsByKey.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        ((CollectionKey) entry.getKey()).serialize(oos);
        oos.writeObject(entry.getValue());
    }

    oos.writeInt(collectionEntries.size());
    LOG.trace("Starting serialization of [" + collectionEntries.size() + "] collectionEntries entries");
    itr = collectionEntries.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        oos.writeObject(entry.getKey());
        ((CollectionEntry) entry.getValue()).serialize(oos);
    }

    oos.writeInt(arrayHolders.size());
    LOG.trace("Starting serialization of [" + arrayHolders.size() + "] arrayHolders entries");
    itr = arrayHolders.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        oos.writeObject(entry.getKey());
        oos.writeObject(entry.getValue());
    }

    oos.writeInt(nullifiableEntityKeys.size());
    LOG.trace("Starting serialization of [" + nullifiableEntityKeys.size() + "] nullifiableEntityKey entries");
    itr = nullifiableEntityKeys.iterator();
    while (itr.hasNext()) {
        EntityKey entry = (EntityKey) itr.next();
        entry.serialize(oos);
    }
}

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 w  w w .j a  v  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);
    }
}

From source file:org.mule.DefaultMuleMessage.java

private void writeObject(ObjectOutputStream out) throws Exception {
    out.defaultWriteObject();/*from   w  w  w .  ja  v 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:org.bigtextml.topics.ParallelTopicModel.java

private void writeObject(ObjectOutputStream out) throws IOException {
    out.writeInt(CURRENT_SERIAL_VERSION);

    //This part needs to be re-written
    /*//from w  w  w .  j  a  v  a2  s  . co m
    out.writeObject(data);
    out.writeObject(alphabet);
    out.writeObject(topicAlphabet);
    */

    out.writeObject(data);
    out.writeObject(alphabet);
    out.writeObject(topicAlphabet);

    out.writeInt(numTopics);

    out.writeInt(topicMask);
    out.writeInt(topicBits);

    out.writeInt(numTypes);

    out.writeObject(alpha);
    out.writeDouble(alphaSum);
    out.writeDouble(beta);
    out.writeDouble(betaSum);

    out.writeObject(typeTopicCounts);
    out.writeObject(tokensPerTopic);

    out.writeObject(docLengthCounts);
    out.writeObject(topicDocCounts);

    out.writeInt(numIterations);
    out.writeInt(burninPeriod);
    out.writeInt(saveSampleInterval);
    out.writeInt(optimizeInterval);
    out.writeInt(showTopicsInterval);
    out.writeInt(wordsPerTopic);

    out.writeInt(saveStateInterval);
    out.writeObject(stateFilename);

    out.writeInt(saveModelInterval);
    out.writeObject(modelFilename);

    out.writeInt(randomSeed);
    out.writeObject(formatter);
    out.writeBoolean(printLogLikelihood);

    out.writeInt(numThreads);
}

From source file:bftsmart.tom.core.TOMLayer.java

private void catch_up(int regency) {

    Logger.println("(TOMLayer.catch_up) verify STOPDATA info");
    ObjectOutputStream out = null;
    ByteArrayOutputStream bos = null;

    LastEidData lastHighestEid = lcManager.getHighestLastEid(regency);

    int currentEid = lastHighestEid.getEid() + 1;
    HashSet<SignedObject> signedCollects = null;
    byte[] propose = null;
    int batchSize = -1;

    // normalize the collects and apply to them the predicate "sound"
    if (lcManager.sound(lcManager.selectCollects(regency, currentEid))) {

        Logger.println("(TOMLayer.catch_up) sound predicate is true");

        signedCollects = lcManager.getCollects(regency); // all original collects that the replica has received

        Consensus cons = new Consensus(-1); // the only purpose of this object is to obtain the batchsize,
        // using code inside of createPropose()

        propose = createPropose(cons);/* www . j  av a  2  s.  c  o m*/
        batchSize = cons.batchSize;

        try { // serialization of the CATCH-UP message
            bos = new ByteArrayOutputStream();
            out = new ObjectOutputStream(bos);

            out.writeObject(lastHighestEid);

            //TODO: Missing: serialization of the proof

            out.writeInt(currentEid);
            out.writeObject(signedCollects);
            out.writeObject(propose);
            out.writeInt(batchSize);

            out.flush();
            bos.flush();

            byte[] payload = bos.toByteArray();
            out.close();
            bos.close();

            Logger.println("(TOMLayer.catch_up) sending SYNC message for regency " + regency);

            // send the CATCH-UP message
            communication.send(this.controller.getCurrentViewOtherAcceptors(), new LCMessage(
                    this.controller.getStaticConf().getProcessId(), TOMUtil.SYNC, regency, payload));

            finalise(regency, lastHighestEid, currentEid, signedCollects, propose, batchSize, true);

        } catch (IOException ex) {
            ex.printStackTrace();
            java.util.logging.Logger.getLogger(TOMLayer.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                out.close();
                bos.close();
            } catch (IOException ex) {
                ex.printStackTrace();
                java.util.logging.Logger.getLogger(TOMLayer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:CSSDFarm.UserInterface.java

private void saveUserData(String filePath, int item) {
    int userSettings;
    ObjectOutputStream outstream;
    try {/*from   www .  ja  v a 2  s  .  c  o m*/
        outstream = new ObjectOutputStream(new FileOutputStream(filePath));
        userSettings = item;
        outstream.writeInt(userSettings);
        outstream.close();
    } catch (IOException io) {
        //System.out.println(io);
    }
}

From source file:bftsmart.tom.core.Synchronizer.java

private void catch_up(int regency) {

    Logger.println("(Synchronizer.catch_up) verify STOPDATA info");
    ObjectOutputStream out = null;
    ByteArrayOutputStream bos = null;

    CertifiedDecision lastHighestCID = lcManager.getHighestLastCID(regency);

    int currentCID = lastHighestCID.getCID() + 1;
    HashSet<SignedObject> signedCollects = null;
    byte[] propose = null;
    int batchSize = -1;

    // normalize the collects and apply to them the predicate "sound"
    if (lcManager.sound(lcManager.selectCollects(regency, currentCID))) {

        Logger.println("(Synchronizer.catch_up) sound predicate is true");

        signedCollects = lcManager.getCollects(regency); // all original collects that the replica has received

        Decision dec = new Decision(-1); // the only purpose of this object is to obtain the batchsize,
                                         // using code inside of createPropose()

        propose = tom.createPropose(dec);
        batchSize = dec.batchSize;/*from  w ww . j av  a 2 s  .c  o  m*/

        try { // serialization of the CATCH-UP message
            bos = new ByteArrayOutputStream();
            out = new ObjectOutputStream(bos);

            out.writeObject(lastHighestCID);

            //TODO: Missing: serialization of the proof?
            out.writeObject(signedCollects);
            out.writeObject(propose);
            out.writeInt(batchSize);

            out.flush();
            bos.flush();

            byte[] payload = bos.toByteArray();
            out.close();
            bos.close();

            System.out.println("(Synchronizer.catch_up) sending SYNC message for regency " + regency);

            // send the CATCH-UP message
            communication.send(this.controller.getCurrentViewOtherAcceptors(), new LCMessage(
                    this.controller.getStaticConf().getProcessId(), TOMUtil.SYNC, regency, payload));

            finalise(regency, lastHighestCID, signedCollects, propose, batchSize, true);

        } catch (IOException ex) {
            ex.printStackTrace();
            java.util.logging.Logger.getLogger(TOMLayer.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                out.close();
                bos.close();
            } catch (IOException ex) {
                ex.printStackTrace();
                java.util.logging.Logger.getLogger(TOMLayer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:com.projity.pm.task.NormalTask.java

protected void doWriteObject(ObjectOutputStream s) throws IOException {
    s.defaultWriteObject();//from  ww  w. j  ava 2 s  .  c o  m
    hasKey.serialize(s);
    customFields.serialize(s);
    if (version < 1)
        currentSchedule.serialize(s);
    else {
        int sCount = 0;
        for (int i = 0; i < Settings.numBaselines(); i++) {
            TaskSnapshot snapshot = (TaskSnapshot) getSnapshot(new Integer(i));
            if (snapshot != null)
                sCount++;
        }
        s.writeInt(sCount);
        for (int i = 0; i < Settings.numBaselines(); i++) {
            TaskSnapshot snapshot = (TaskSnapshot) getSnapshot(new Integer(i));
            if (snapshot == null)
                continue;
            s.writeInt(i);
            snapshot.serialize(s);
        }
    }
}