Java ByteBuffer to Long readLong(ByteBuffer buf, int length)

Here you can find the source of readLong(ByteBuffer buf, int length)

Description

read Long

License

Open Source License

Declaration

public static long readLong(ByteBuffer buf, int length) 

Method Source Code

//package com.java2s;
/**//from w  ww  . j a va 2 s .  c  o m
 * Project: ${puma-common.aid}
 * <p/>
 * File Created at 2012-6-6 $Id$
 * <p/>
 * Copyright 2010 dianping.com. All rights reserved.
 * <p/>
 * This software is the confidential and proprietary information of Dianping
 * Company. ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with dianping.com.
 */

import java.nio.ByteBuffer;

public class Main {
    public static long readLong(ByteBuffer buf, int length) {
        if ((buf.position() + length) <= buf.limit() && length <= 8) {
            long r = 0;
            for (int i = 0; i < length; i++) {
                r |= (((long) buf.get() & 0xff) << (i << 3));
            }
            return r;
        }
        return 0;
    }

    public static long readLong(ByteBuffer buf, int length, boolean isLittleEndian) {
        if ((buf.position() + length) <= buf.limit() && length <= 8) {
            long r = 0;
            for (int i = 0; i < length; i++) {
                if (isLittleEndian) {
                    r |= (((long) buf.get() & 0xff) << (i << 3));
                } else {
                    r = (r << 8) | (buf.get() & 0xff);
                }
            }
            return r;
        }
        return 0;
    }
}

Related

  1. getUInt16(ByteBuffer buffer)
  2. getUInt16(ByteBuffer buffer)
  3. getUInt32(ByteBuffer b)
  4. getUInt8(ByteBuffer b, int n)
  5. getUInteger(ByteBuffer bb)
  6. readLong(ByteBuffer buffer)
  7. readLong(ByteBuffer buffer)
  8. readLong(ByteBuffer in)
  9. readLong(ByteBuffer in, final int fitInBytes)