List of usage examples for java.nio ByteBuffer isReadOnly
public abstract boolean isReadOnly();
From source file:Main.java
public static void main(String[] argv) throws Exception { ByteBuffer bbuf = ByteBuffer.allocate(10); int capacity = bbuf.capacity(); // 10 System.out.println(capacity); bbuf.putShort(2, (short) 123); System.out.println(Arrays.toString(bbuf.array())); System.out.println(bbuf.isReadOnly()); }
From source file:net.darkmist.alib.io.BufferUtil.java
public static byte[] asBytes(ByteBuffer buf) { buf = buf.duplicate();//from ww w . j a v a 2s .c o m /* To use buf.array() the buffer must: * be writable as the array will be writable * have arrayOffset() == 0 or the array will not start at the right location * the returned array must be the same length as the buffer's limit or it will be the wrong size. */ if (!buf.isReadOnly() && buf.hasArray() && buf.arrayOffset() == 0) { logger.debug("!read-only, hasArray && offset is 0"); byte[] ret = buf.array(); if (ret.length == buf.limit()) return ret; logger.debug("length of array !=limit, doing copy..."); } byte[] bytes = new byte[buf.limit()]; buf.get(bytes, 0, buf.limit()); return bytes; }