Java ByteBuffer Get getObject(ByteBuffer buffer)

Here you can find the source of getObject(ByteBuffer buffer)

Description

Reads a Object (Serializable) from a ByteBuffer.

License

Open Source License

Declaration

public static Object getObject(ByteBuffer buffer) 

Method Source Code


//package com.java2s;
/* *********************************************************************** *
 * project: org.matsim.*//from www . j a v  a2 s . c o m
 * ByteBufferUtils.java
 *                                                                         *
 * *********************************************************************** *
 *                                                                         *
 * copyright       : (C) 2009 by the members listed in the COPYING,        *
 *                   LICENSE and WARRANTY file.                            *
 * email           : info at matsim dot org                                *
 *                                                                         *
 * *********************************************************************** *
 *                                                                         *
 *   This program is 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *   See also COPYING, LICENSE and WARRANTY file                           *
 *                                                                         *
 * *********************************************************************** */

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.nio.ByteBuffer;

public class Main {
    /**
     * Reads a Object (Serializable) from a ByteBuffer. Reads first an int for the length of the Object,
     * and then the corresponding number of bytes. Increments the position of the
     * ByteBuffer according to the length of the object's byte array. 
     */
    public static Object getObject(ByteBuffer buffer) {
        int length = buffer.getInt();
        byte[] bytes = new byte[length];
        for (int i = 0; i < length; i++) {
            bytes[i] = buffer.get();
        }
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        Object o = null;
        try (ObjectInputStream oin = new ObjectInputStream(bis)) {
            o = oin.readObject();
            bis.close();
            oin.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return o;
    }
}

Related

  1. getNaluStartLength(ByteBuffer buffer)
  2. getNextTagNum(ByteBuffer message)
  3. getNTString(ByteBuffer buffer)
  4. getNullTerminatedByte(ByteBuffer buffer)
  5. getNumeric(java.nio.ByteBuffer buffer, int len)
  6. getObject(ByteBuffer byteBuffer)
  7. getProfileIdc(ByteBuffer spsBuffer)
  8. getQuickchatParam(ByteBuffer buf, int size)
  9. getRel(ByteBuffer bb, int rel)