Java ByteBuffer from Byte Array readByteArray(ByteBuffer logBuf)

Here you can find the source of readByteArray(ByteBuffer logBuf)

Description

Read a byte array from the log.

License

Open Source License

Declaration

public static byte[] readByteArray(ByteBuffer logBuf) 

Method Source Code


//package com.java2s;
/*-//from   ww  w.  j a  v a2  s.c  om
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002,2007 Oracle.  All rights reserved.
 *
 * $Id: LogUtils.java,v 1.50.2.1 2007/02/01 14:49:47 cwl Exp $
 */

import java.nio.ByteBuffer;

public class Main {
    private static final boolean DEBUG = false;
    public static final byte[] ZERO_LENGTH_BYTE_ARRAY = new byte[0];

    /**
     * Read a byte array from the log. The size is stored first as an integer.
     */
    public static byte[] readByteArray(ByteBuffer logBuf) {
        int size = readInt(logBuf); // how long is it?
        if (DEBUG) {
            System.out.println("pos = " + logBuf.position() + " byteArray is " + size + " on read");
        }
        if (size == 0) {
            return ZERO_LENGTH_BYTE_ARRAY;
        }
        byte[] b = new byte[size];
        logBuf.get(b); // read it out
        return b;
    }

    /**
     * Read a int from the log.
     */
    public static int readInt(ByteBuffer logBuf) {
        int ret = (logBuf.get() & 0xFF) << 0;
        ret += (logBuf.get() & 0xFF) << 8;
        ret += (logBuf.get() & 0xFF) << 16;
        ret += (logBuf.get() & 0xFF) << 24;
        return ret;
    }
}

Related

  1. bufToArray(ByteBuffer b)
  2. bufToArray(ByteBuffer b)
  3. byteBuffer(byte[] a)
  4. readByteArray(ByteBuffer byteBuffer, int length)
  5. readByteArray(ByteBuffer in)
  6. readByteAsInt(ByteBuffer buffer)
  7. readBytes(ByteBuffer bb, int length)
  8. readBytes(ByteBuffer bb, int length)
  9. readBytes(ByteBuffer buf, int length)