Java ByteBuffer Get getObject(ByteBuffer byteBuffer)

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

Description

get Object

License

Open Source License

Declaration

public static Object getObject(ByteBuffer byteBuffer) throws IOException, ClassNotFoundException 

Method Source Code


//package com.java2s;
/*/*from  ww  w.ja  v  a2  s.com*/
 * Copyright (c) 2007, 2011-2013 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */

import java.io.ByteArrayInputStream;

import java.io.IOException;
import java.io.ObjectInputStream;

import java.nio.ByteBuffer;

public class Main {
    private static final byte TRUE = (byte) 1;

    public static Object getObject(ByteBuffer byteBuffer) throws IOException, ClassNotFoundException {
        boolean nonNull = byteBuffer.get() == TRUE;
        if (nonNull) {
            byte[] array = getByteArray(byteBuffer);
            ByteArrayInputStream bais = new ByteArrayInputStream(array);
            ObjectInputStream stream = new ObjectInputStream(bais);
            return stream.readObject();
        }

        return null;
    }

    public static byte[] getByteArray(ByteBuffer byteBuffer) {
        short length = byteBuffer.getShort();
        byte[] array = new byte[length];
        if (length != 0) {
            byteBuffer.get(array);
        }

        return array;
    }
}

Related

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