List of usage examples for java.nio ByteBuffer hasArray
public final boolean hasArray()
From source file:gobblin.util.io.StreamUtils.java
/** * Reads the full contents of a ByteBuffer and writes them to an OutputStream. The ByteBuffer is * consumed by this operation; eg in.remaining() will be 0 after it completes successfully. * @param in ByteBuffer to write into the OutputStream * @param out Destination stream//from w w w . j a va2 s . co m * @throws IOException If there is an error writing into the OutputStream */ public static void byteBufferToOutputStream(ByteBuffer in, OutputStream out) throws IOException { final int BUF_SIZE = 8192; if (in.hasArray()) { out.write(in.array(), in.arrayOffset() + in.position(), in.remaining()); } else { final byte[] b = new byte[Math.min(in.remaining(), BUF_SIZE)]; while (in.remaining() > 0) { int bytesToRead = Math.min(in.remaining(), BUF_SIZE); in.get(b, 0, bytesToRead); out.write(b, 0, bytesToRead); } } }
From source file:io.mycat.util.ByteBufferUtil.java
/** trims size of bytebuffer to exactly number of bytes in it, to do not hold too much memory */ public static ByteBuffer minimalBufferFor(ByteBuffer buf) { return buf.capacity() > buf.remaining() || !buf.hasArray() ? ByteBuffer.wrap(getArray(buf)) : buf; }
From source file:io.mycat.util.ByteBufferUtil.java
/** * You should almost never use this. Instead, use the write* methods to avoid copies. *//*from w w w.j a v a 2s . co m*/ public static byte[] getArray(ByteBuffer buffer) { int length = buffer.remaining(); if (buffer.hasArray()) { int boff = buffer.arrayOffset() + buffer.position(); return Arrays.copyOfRange(buffer.array(), boff, boff + length); } // else, DirectByteBuffer.get() is the fastest route byte[] bytes = new byte[length]; buffer.duplicate().get(bytes); return bytes; }
From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueServices.java
public static byte[] getBytesFromByteBuffer(ByteBuffer buffer) { // Be careful *NOT* to perform anything that will modify the buffer's position or limit byte[] bytes = new byte[buffer.limit() - buffer.position()]; if (buffer.hasArray()) { System.arraycopy(buffer.array(), buffer.position(), bytes, 0, bytes.length); } else {//ww w .j ava 2 s . com buffer.duplicate().get(bytes, buffer.position(), bytes.length); } return bytes; }
From source file:de.dfki.kiara.jsonrpc.JsonRpcMessage.java
private static JsonNode readFromBuffer(JsonRpcProtocol protocol, ByteBuffer data) throws IOException { byte[] array; int arrayOffset; int arrayLength; int oldPos = data.position(); if (data.hasArray()) { array = data.array();//from w ww . j ava2s . co m arrayOffset = data.arrayOffset(); arrayLength = data.remaining(); } else { array = new byte[data.remaining()]; data.get(array); arrayOffset = 0; arrayLength = array.length; } data.position(oldPos); JsonNode node; try (JsonParser parser = protocol.getObjectReader().getFactory().createParser(array, arrayOffset, arrayLength)) { node = parser.readValueAsTree(); } return node; }
From source file:com.glaf.core.util.ByteBufferUtils.java
public static void write(ByteBuffer buffer, DataOutput out) throws IOException { if (buffer.hasArray()) { out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } else {/* www .jav a2 s . c om*/ for (int i = buffer.position(); i < buffer.limit(); i++) { out.writeByte(buffer.get(i)); } } }
From source file:io.mycat.util.ByteBufferUtil.java
/** * @return a new copy of the data in @param buffer * USUALLY YOU SHOULD USE ByteBuffer.duplicate() INSTEAD, which creates a new Buffer * (so you can mutate its position without affecting the original) without copying the underlying array. *///from w w w. java 2 s .co m public static ByteBuffer clone(ByteBuffer buffer) { assert buffer != null; if (buffer.remaining() == 0) { return EMPTY_BYTE_BUFFER; } ByteBuffer clone = ByteBuffer.allocate(buffer.remaining()); if (buffer.hasArray()) { System.arraycopy(buffer.array(), buffer.arrayOffset() + buffer.position(), clone.array(), 0, buffer.remaining()); } else { clone.put(buffer.duplicate()); clone.flip(); } return clone; }
From source file:com.glaf.core.util.ByteBufferUtils.java
/** * buffer?// w ww.j a va 2 s. c o m * * @param buffer * @return */ public static byte[] toArray(ByteBuffer buffer) { // ?heap buffer if (buffer.hasArray()) { byte[] array = buffer.array(); int from = buffer.arrayOffset() + buffer.position(); return Arrays.copyOfRange(array, from, from + buffer.remaining()); } // direct buffer else { byte[] to = new byte[buffer.remaining()]; buffer.slice().get(to); return to; } }
From source file:com.glaf.core.util.ByteBufferUtils.java
public static void arrayCopy(ByteBuffer buffer, int position, byte[] bytes, int offset, int length) { if (buffer.hasArray()) System.arraycopy(buffer.array(), buffer.arrayOffset() + position, bytes, offset, length); else// w ww. jav a2 s . co m ((ByteBuffer) buffer.duplicate().position(position)).get(bytes, offset, length); }
From source file:com.glaf.core.util.ByteBufferUtils.java
/** * You should almost never use this. Instead, use the write* methods to * avoid copies.//w w w .j a v a 2 s . co m */ public static byte[] getArray(ByteBuffer buffer) { int length = buffer.remaining(); if (buffer.hasArray()) { int boff = buffer.arrayOffset() + buffer.position(); if (boff == 0 && length == buffer.array().length) return buffer.array(); else return Arrays.copyOfRange(buffer.array(), boff, boff + length); } // else, DirectByteBuffer.get() is the fastest route byte[] bytes = new byte[length]; buffer.duplicate().get(bytes); return bytes; }