Java ByteBuffer Serialize deserializeObject(ByteBuffer obj)

Here you can find the source of deserializeObject(ByteBuffer obj)

Description

This method deserializes an object stored in byte buffer form.

License

Open Source License

Declaration

public static Object deserializeObject(ByteBuffer obj) throws Exception 

Method Source Code


//package com.java2s;
/* Copyright 2012 Cornell University
 * Copyright 2013 Cornell University/*ww w .  j  a va2s. 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.ByteArrayInputStream;

import java.io.ObjectInputStream;

import java.nio.ByteBuffer;

public class Main {
    /** 
     * This method deserializes an object stored in byte buffer form.
     */
    public static Object deserializeObject(ByteBuffer obj) throws Exception {
        byte[] objAsByteArray = new byte[obj.limit()];
        obj.get(objAsByteArray);

        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(objAsByteArray);
        ObjectInputStream objInputStream = new ObjectInputStream(byteInputStream);
        Object result = objInputStream.readObject();
        objInputStream.close();

        return result;
    }
}

Related

  1. deserialize(ByteBuffer b)
  2. deserialize(ByteBuffer byteBuffer)
  3. deserializeBoolean(ByteBuffer buf)
  4. deserializeFromByteBufferNoHeader(ByteBuffer bytes)
  5. serializeBigDecimal(BigDecimal bd, ByteBuffer buf)
  6. serializeNull(ByteBuffer buf)
  7. serializeString(final String value, ByteBuffer buffer)
  8. serializeStringArray(ByteBuffer buf, String[] strArray)