Java ByteBuffer to Int Array readIntArray(ByteBuffer bb)

Here you can find the source of readIntArray(ByteBuffer bb)

Description

read Int Array

License

Open Source License

Declaration

static public int[] readIntArray(ByteBuffer bb) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2007(c) G?nome Qu?bec. All rights reserved.
 * //w w w .j  av  a 2  s  .  co  m
 * This file is part of GenoByte.
 * 
 * GenoByte 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 3
 * of the License, or (at your option) any later version.
 * 
 * GenoByte is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *******************************************************************************/

import java.nio.ByteBuffer;

public class Main {
    static public int[] readIntArray(ByteBuffer bb) {
        int ints[] = null;
        int size = bb.getInt();
        if (size >= 0) {
            ints = new int[size];

            byte[] b = new byte[size * 4];
            bb.get(b);
            int l = 0;

            // Transform the array of bytes into an array of ints. This is a BIG_ENDIAN transform, if STORED_BYTE_ORDER is not BIG_ENDIAN, this will break.
            for (int i = 0; i < b.length; i += 4) {
                ints[l++] = ((((int) b[i] & 0xff) << 24) | (((int) b[i + 1] & 0xff) << 16)
                        | (((int) b[i + 2] & 0xff) << 8) | (((int) b[i + 3] & 0xff) << 0));
            }
        }
        return ints;
    }
}

Related

  1. getIntByteBuffer(int id)
  2. getIntegerFromBuffer(ByteBuffer buffer, int offset, int size)
  3. getIntFromBack(ByteBuffer bb)
  4. getIntFromByteBuffer(ByteBuffer data)
  5. getIntWithChecksum(ByteBuffer buffer, Adler32 checksum)
  6. readIntArray(ByteBuffer buf)
  7. readIntArray(ByteBuffer in)
  8. readIntegerArray(ByteBuffer bb, int length)