Here you can find the source of getObject(ByteBuffer buffer)
public static Object getObject(ByteBuffer buffer)
//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; } }