Java ByteBuffer Put putObject(ByteBuffer byteBuffer, Object object)

Here you can find the source of putObject(ByteBuffer byteBuffer, Object object)

Description

put Object

License

Open Source License

Declaration

public static void putObject(ByteBuffer byteBuffer, Object object) throws IOException 

Method Source Code

//package com.java2s;
/*//  www .  ja  v a2s . co m
 * 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.ByteArrayOutputStream;
import java.io.IOException;

import java.io.ObjectOutputStream;

import java.nio.ByteBuffer;

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

    public static void putObject(ByteBuffer byteBuffer, Object object) throws IOException {
        if (object != null) {
            byteBuffer.put(TRUE);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream stream = new ObjectOutputStream(baos);
            stream.writeObject(object);

            byte[] array = baos.toByteArray();
            putByteArray(byteBuffer, array);
        } else {
            byteBuffer.put(FALSE);
        }
    }

    public static void putByteArray(ByteBuffer byteBuffer, byte[] array) {
        short length = array == null ? 0 : (short) array.length;
        byteBuffer.putShort(length); // BYTE_ARRAY_PREFIX
        if (length != 0) {
            byteBuffer.put(array);
        }
    }
}

Related

  1. putIntByteBuffer(ByteBuffer buf, int b)
  2. putIntByteBuffer(ByteBuffer buf, int b)
  3. putIntLSBMSB(ByteBuffer byteBuffer, int value)
  4. putNAL(ByteBuffer codecPrivate, ByteBuffer byteBuffer, int nalType)
  5. putNullTerminal(ByteBuffer buffer)
  6. putObject(final ByteBuffer buffer, Serializable o)
  7. putQuickchatParam(ByteBuffer buf, long param, int size)
  8. putRange(ByteBuffer buffer, int start, int end, byte b)
  9. putRGBfromHSB(final ByteBuffer pixels, final float[] hsb, int pixelSize)