Java ByteBuffer to Long Array getLongFromBuffer(ByteBuffer buffer, int offset, int size)

Here you can find the source of getLongFromBuffer(ByteBuffer buffer, int offset, int size)

Description

Gets long from buffer

License

Open Source License

Parameter

Parameter Description
buffer dataBuffer
offset offset of the data
size size of the data

Return

long read from buffer

Declaration

static long getLongFromBuffer(ByteBuffer buffer, int offset, int size) 

Method Source Code


//package com.java2s;
/*//  w  ww .j av a 2s  .c  o  m
 * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). 
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description:
 *
 * Decode Utils
 *
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Gets long from buffer
     * 
     * @param buffer
     *            dataBuffer
     * @param offset
     *            offset of the data
     * @param size
     *            size of the data
     * @return long read from buffer
     */
    static long getLongFromBuffer(ByteBuffer buffer, int offset, int size) {
        long data = 0;
        try {
            if (size == 1) {
                data |= buffer.get(offset) & 0xFF;
            } else if (size == 2) {
                data |= buffer.getShort(offset) & 0xFFFF;
                data = Short.reverseBytes((short) data);
            } else if (size == 4) {
                data = buffer.getInt(offset);
                data = Integer.reverseBytes((int) data);
            } else if (size == 8) {
                data |= buffer.getLong(offset) & 0xFFFFFFFFFFFFFFFFL;
                data = Long.reverseBytes(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
}

Related

  1. getLong(ByteBuffer in)
  2. getLong(ByteBuffer longCalculator, byte[] bytes)
  3. getLongB(ByteBuffer bb, int index)
  4. getLongBE(ByteBuffer b, int start, int end)
  5. getLongByteBuffer(long id)
  6. getLongFromByteBuffer(ByteBuffer data)
  7. getLongLE(final ByteBuffer b, final int start, final int end)
  8. getLongWithChecksum(ByteBuffer buffer, Adler32 checksum)