Java ByteBuffer Write write(ByteBuffer bb, int elementWidth, long value)

Here you can find the source of write(ByteBuffer bb, int elementWidth, long value)

Description

write

License

Open Source License

Declaration

public static void write(ByteBuffer bb, int elementWidth, long value) 

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 void write(ByteBuffer bb, int elementWidth, long value) {
        if (bb.order() == ByteOrder.BIG_ENDIAN) {
            value = swap(elementWidth, value);
        }//from  ww  w  . j a  v  a  2s. c  o m
        writeBE(bb, elementWidth, value);
    }

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

    private static void writeBE(ByteBuffer bb, int elementWidth, long value) {
        final int p = bb.position();
        for (int j = 0; j < elementWidth; j++) {
            bb.put(p + j, (byte) value);
            value >>>= 8;
        }
        bb.position(p + elementWidth);
    }
}

Related

  1. write(ByteBuffer data, String filename, boolean append)
  2. write(final byte[] array, final int offset, final int end, final ByteBuffer outBB)
  3. write(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length)
  4. write(MappedByteBuffer buffer, int pos, String asciString)