Java ByteBuffer Read read(ByteBuffer bb, int elementWidth)

Here you can find the source of read(ByteBuffer bb, int elementWidth)

Description

read

License

Open Source License

Declaration

public static long read(ByteBuffer bb, int elementWidth) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    public static long read(ByteBuffer bb, int elementWidth) {
        final long value = readBE(bb, elementWidth);
        if (bb.order() == ByteOrder.BIG_ENDIAN) {
            return value;
        }//from  w  ww.  ja  va 2s.  c o  m
        return swap(elementWidth, value);
    }

    private static long readBE(ByteBuffer bb, int elementWidth) {
        final int p = bb.position();
        long value = 0;
        for (int j = 0; j < elementWidth; j++) {
            value = (value << 8) | (bb.get(p + j) & 0xff);
        }
        bb.position(p + elementWidth);
        return value;
    }

    private static long swap(int elementWidth, long value) {
        return Long.reverseBytes(value) >>> (8 * (8 - elementWidth));
    }
}

Related

  1. enlargeThreadLocalByteBuffer()
  2. parseEsInfo(ByteBuffer read)
  3. read(@Nonnull final FileChannel src, @Nonnull final ByteBuffer dst, @Nonnegative final long position)
  4. read(ByteBuffer b)
  5. read(ByteBuffer bb, FileChannel ch)
  6. read(ByteBuffer buffer)
  7. read(ByteBuffer dest, ByteBuffer src)
  8. read(ByteBuffer src, int len)
  9. read(final ByteBuffer buffer, final byte marker)