Java ByteBuffer Put serializeObject(Object obj, ByteBuffer buffer, ByteArrayOutputStream baos, boolean markEndOfHeader)

Here you can find the source of serializeObject(Object obj, ByteBuffer buffer, ByteArrayOutputStream baos, boolean markEndOfHeader)

Description

This method serializes the given object and stores it in the provided byte buffer.

License

Open Source License

Declaration

public static boolean serializeObject(Object obj, ByteBuffer buffer, ByteArrayOutputStream baos,
        boolean markEndOfHeader) throws IOException 

Method Source Code

//package com.java2s;
/* Copyright 2012 Cornell University
 * Copyright 2013 Cornell University/*from  w  ww  . ja va  2  s .  c  o  m*/
 *
 *
 * This file is part of ShadowDB - a replicated database based on
 * an formally-verified implementation of an atomic broadcast service.
 * The normal case processing is written in Java, while failure-handling is taken
 * care of by an atomic broadcast service called Aneris. Aneris contains
 * two formally-verified implementations of atomic broadcast, one is
 * based on Paxos, the other one is based on two-third consensus.
 * 
 * The three code contributors of this project are:
 * Vincent Rahli (Aneris)
 * Nicolas Schiper (ShadowDB)
 * Mark Bickford (Aneris)
 * 
 * ShadowDB was implemented at Cornell University, Ithaca, NY.
 *
 * ShadowDB is a free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ShadowDB is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ShadowDB.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  o Authors:     Nicolas Schiper
 *  o Affiliation: Cornell University
 *  o Date:        13 May 2013
 *  o File name:   NIOUtils.java
 *  o Description: A utility class to interact with non-blocking channels.
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;

public class Main {
    /**
     * The number of bytes of an int.
     */
    public static final int BYTE_COUNT_INT = Integer.SIZE / 8;
    /**
     * A byte marking the end of a header. This is used when communication
     * with Aneris.
     */
    public static byte END_BYTE = (byte) (((int) '!') >> 24);

    /**
     * This method serializes the given object and stores it in the provided
     * byte buffer. It is assumed that the buffer is big enough to hold
     * the entire object.
     * 
     * When the boolean markEndOfHeader is true, the header of the message (the size
     * of the message) is followed by a '!'. This is used to send messages to Aneris.
     */
    public static boolean serializeObject(Object obj, ByteBuffer buffer, ByteArrayOutputStream baos,
            boolean markEndOfHeader) throws IOException {

        //baos.reset();
        baos = new ByteArrayOutputStream();
        ObjectOutputStream objOutputStream = new ObjectOutputStream(baos);

        objOutputStream.writeObject(obj);
        objOutputStream.flush();
        byte[] objAsByteArray = baos.toByteArray();
        objOutputStream.close();

        if (markEndOfHeader) {
            if ((objAsByteArray.length + BYTE_COUNT_INT + Byte.SIZE) > buffer.capacity()) {
                return false;
            }
        } else {
            if ((objAsByteArray.length + BYTE_COUNT_INT) > buffer.capacity()) {
                return false;
            }
        }

        // First write the number of bytes the object takes in serialized form.
        buffer.clear();
        buffer.putInt(objAsByteArray.length);
        if (markEndOfHeader) {
            buffer.put(END_BYTE);
        }
        buffer.put(objAsByteArray);

        buffer.flip();
        return true;
    }
}

Related

  1. readInto(ByteBuffer buf, InputStream inputStream)
  2. readNextLine(InputStream in, ByteBuffer buff, boolean acceptEOF, boolean requireCR)
  3. readPackedLong(ByteBuffer input)
  4. readToByteBuffer(InputStream inStream)
  5. serializeNullableString(ByteBuffer outputBuffer, String value)
  6. setBit(ByteBuffer input, int pos)
  7. skipBlank(ByteBuffer input)
  8. toByteArrayInputStream(ByteBuffer buffer)
  9. toByteBuffer(InputStream inputStream)