Java ReadableByteChannel Read readObjFromChannel(ReadableByteChannel chan, ByteBuffer buffer, AtomicBoolean endPointCrashed)

Here you can find the source of readObjFromChannel(ReadableByteChannel chan, ByteBuffer buffer, AtomicBoolean endPointCrashed)

Description

Returns the next object to be read from this channel, or null if no data can be read at this moment.

License

Open Source License

Declaration

public static Object readObjFromChannel(ReadableByteChannel chan, ByteBuffer buffer,
        AtomicBoolean endPointCrashed) throws Exception 

Method Source Code


//package com.java2s;
/* Copyright 2012 Cornell University
 * Copyright 2013 Cornell University/*from w  w w . j a  v  a2s  .  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;
import java.nio.channels.ReadableByteChannel;

import java.util.concurrent.atomic.AtomicBoolean;

public class Main {
    /**
     * The number of bytes of an int.
     */
    public static final int BYTE_COUNT_INT = Integer.SIZE / 8;

    /**
     * Returns the next object to be read from this channel, or null if
     * no data can be read at this moment. This method assumes that the
     * channel is in non-blocking mode and uses the provided buffer to
     * read the object in serialized form from the channel. It is assumed
     * that the buffer is big enough to hold the entire object in serialized form.
     */
    public static Object readObjFromChannel(ReadableByteChannel chan, ByteBuffer buffer,
            AtomicBoolean endPointCrashed) throws Exception {

        buffer.clear();
        buffer.limit(BYTE_COUNT_INT);

        if (chan.read(buffer) > 0) {
            while (buffer.hasRemaining()) {
                chan.read(buffer);
            }
            buffer.flip();
            int nbBytes = buffer.getInt();

            buffer.clear();
            buffer.limit(nbBytes);

            while (buffer.hasRemaining()) {
                if (chan.read(buffer) <= 0 && endPointCrashed.get()) {
                    return null;
                }

            }
            buffer.flip();
            return deserializeObject(buffer);
        } else {
            return null;
        }
    }

    /** 
     * 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. readFully(final ReadableByteChannel channel, final ByteBuffer buf)
  2. readFully(ReadableByteChannel channel, ByteBuffer b)
  3. readFully(ReadableByteChannel channel, ByteBuffer buffer)
  4. readLengthAndString(ReadableByteChannel channel, ByteBuffer buffer)
  5. readMap(ReadableByteChannel channel, ByteBuffer buffer)
  6. readToBuffer(ReadableByteChannel src, ByteBuffer dst)
  7. retryRead(ReadableByteChannel channel, ByteBuffer buffer)
  8. transfer(final ReadableByteChannel source, final long count, final ByteBuffer throughBuffer, final WritableByteChannel sink)
  9. transfer(final ReadableByteChannel source, final long count, final ByteBuffer throughBuffer, final WritableByteChannel sink)