Java ByteBuffer Write writeLength(ByteBuffer buffer, long l)

Here you can find the source of writeLength(ByteBuffer buffer, long l)

Description

write Length

License

Open Source License

Declaration

public static final void writeLength(ByteBuffer buffer, long l) 

Method Source Code


//package com.java2s;
import java.nio.ByteBuffer;

public class Main {
    public static final void writeLength(ByteBuffer buffer, long l) {
        if (l < 251) {
            buffer.put((byte) l);
        } else if (l < 0x10000L) {
            buffer.put((byte) 252);
            writeUB2(buffer, (int) l);
        } else if (l < 0x1000000L) {
            buffer.put((byte) 253);
            writeUB3(buffer, (int) l);
        } else {/* ww w.jav a 2s  . c  o m*/
            buffer.put((byte) 254);
            writeLong(buffer, l);
        }
    }

    public static final void writeUB2(ByteBuffer buffer, int i) {
        buffer.put((byte) (i & 0xff));
        buffer.put((byte) (i >>> 8));
    }

    public static final void writeUB3(ByteBuffer buffer, int i) {
        buffer.put((byte) (i & 0xff));
        buffer.put((byte) (i >>> 8));
        buffer.put((byte) (i >>> 16));
    }

    public static final void writeLong(ByteBuffer buffer, long l) {
        buffer.put((byte) (l & 0xff));
        buffer.put((byte) (l >>> 8));
        buffer.put((byte) (l >>> 16));
        buffer.put((byte) (l >>> 24));
        buffer.put((byte) (l >>> 32));
        buffer.put((byte) (l >>> 40));
        buffer.put((byte) (l >>> 48));
        buffer.put((byte) (l >>> 56));
    }
}

Related

  1. writeInt8(final ByteBuffer bb, int value)
  2. writeIntArray(ByteBuffer buf, int[] arr)
  3. writeInts4(final ByteBuffer bb, final int... values)
  4. writeL(ByteBuffer to, ByteBuffer from, int count)
  5. writeLen(int len, ByteBuffer dest, int dOff)
  6. writeLong(ByteBuffer buf, long value, int len)
  7. writeLong(ByteBuffer logBuf, long l)
  8. writeLong(long num, ByteBuffer out)
  9. writeLong(long v, ByteBuffer buffer)