Java ByteBuffer Write writeWithLength(ByteBuffer buffer, byte[] src)

Here you can find the source of writeWithLength(ByteBuffer buffer, byte[] src)

Description

write With Length

License

Open Source License

Declaration

public static final void writeWithLength(ByteBuffer buffer, byte[] src) 

Method Source Code


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

public class Main {
    public static final void writeWithLength(ByteBuffer buffer, byte[] src) {
        int length = src.length;
        if (length < 251) {
            buffer.put((byte) length);
        } else if (length < 0x10000L) {
            buffer.put((byte) 252);
            writeUB2(buffer, length);/*  ww w.  j a  v  a2s  .  co  m*/
        } else if (length < 0x1000000L) {
            buffer.put((byte) 253);
            writeUB3(buffer, length);
        } else {
            buffer.put((byte) 254);
            writeLong(buffer, length);
        }
        buffer.put(src);
    }

    public static final void writeWithLength(ByteBuffer buffer, byte[] src, byte nullValue) {
        if (src == null) {
            buffer.put(nullValue);
        } else {
            writeWithLength(buffer, src);
        }
    }

    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. writeVarLong(ByteBuffer buff, long x)
  2. writeVarLong(ByteBuffer buff, long x)
  3. writeVarLong(long n, ByteBuffer buff)
  4. writeVarLong(long value, ByteBuffer buf)
  5. writeVInt(ByteBuffer bb, int i)
  6. writeXid(ByteBuffer logBuf, Xid xid)