List of usage examples for java.nio ByteBuffer position
public final int position()
From source file:io.mycat.util.ByteBufferUtil.java
public static ByteBuffer readBytes(ByteBuffer bb, int length) { ByteBuffer copy = bb.duplicate(); copy.limit(copy.position() + length); bb.position(bb.position() + length); return copy;/*w w w . j a v a 2 s . c om*/ }
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 {// w ww .j av a 2 s. c o m buffer.duplicate().get(bytes, buffer.position(), bytes.length); } return bytes; }
From source file:com.glaf.core.util.BinaryUtils.java
/** * Returns a copy of all the bytes from the given <code>ByteBuffer</code>, * from the beginning to the buffer's limit; or null if the input is null. * <p>/*from www .j a v a 2s.c om*/ * The internal states of the given byte buffer will be restored when this * method completes execution. * <p> * When handling <code>ByteBuffer</code> from user's input, it's typical to * call the {@link #copyBytesFrom(ByteBuffer)} instead of * {@link #copyAllBytesFrom(ByteBuffer)} so as to account for the position * of the input <code>ByteBuffer</code>. The opposite is typically true, * however, when handling <code>ByteBuffer</code> from withint the * unmarshallers of the low-level clients. */ public static byte[] copyAllBytesFrom(ByteBuffer bb) { if (bb == null) return null; if (bb.hasArray()) return Arrays.copyOf(bb.array(), bb.limit()); bb.mark(); // the default ByteBuffer#mark() and reset() won't work, as the // rewind would discard the mark position final int marked = bb.position(); try { byte[] dst = new byte[bb.rewind().remaining()]; bb.get(dst); return dst; } finally { bb.position(marked); } }
From source file:com.icloud.framework.core.util.FBUtilities.java
public static ByteBuffer readByteArray(DataInput in) throws IOException { int length = in.readInt(); if (length < 0) { throw new IOException("Corrupt (negative) value length encountered"); }// ww w . j av a 2s . com ByteBuffer bb = ByteBuffer.allocate(length); if (length > 0) { in.readFully(bb.array(), bb.position(), bb.remaining()); } return bb; }
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();/* ww w. j a v a 2 s. c o 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.icloud.framework.core.util.FBUtilities.java
public static void writeByteArray(ByteBuffer bytes, DataOutput out) throws IOException { out.writeInt(bytes.remaining());//from w w w . j a v a2 s .com out.write(bytes.array(), bytes.position() + bytes.arrayOffset(), bytes.remaining()); }
From source file:topoos.APIAccess.mime.HttpMultipart.java
/** * Encode.//w w w . ja v a 2 s.co m * * @param charset the charset * @param string the string * @return the byte array buffer */ private static ByteArrayBuffer encode(final Charset charset, final String string) { ByteBuffer encoded = charset.encode(CharBuffer.wrap(string)); ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining()); bab.append(encoded.array(), encoded.position(), encoded.remaining()); return bab; }
From source file:io.mycat.util.ByteBufferUtil.java
/** * ByteBuffer adaptation of org.apache.commons.lang3.ArrayUtils.lastIndexOf method * * @param buffer the array to traverse for looking for the object, may be <code>null</code> * @param valueToFind the value to find//www.ja v a 2 s .com * @param startIndex the start index (i.e. BB position) to travers backwards from * @return the last index (i.e. BB position) of the value within the array * [between buffer.position() and buffer.limit()]; <code>-1</code> if not found. */ public static int lastIndexOf(ByteBuffer buffer, byte valueToFind, int startIndex) { assert buffer != null; if (startIndex < buffer.position()) { return -1; } else if (startIndex >= buffer.limit()) { startIndex = buffer.limit() - 1; } for (int i = startIndex; i >= buffer.position(); i--) { if (valueToFind == buffer.get(i)) { return i; } } return -1; }
From source file:com.l2jfree.security.NewCipher.java
/** * Calculates and embeds a packet's checksum.<BR> * Buffer's position will not be changed. <BR> * <BR>//from w w w.ja v a 2s. c om * It is assumed that the packet's body starts at current position. * * @param buf byte buffer * @param size packet's body size */ public static void appendChecksum(ByteBuffer buf, final int size) { appendChecksum(buf, buf.position(), size, false); }
From source file:com.l2jfree.security.NewCipher.java
/** * Verifies a packet's checksum. <BR> * <BR>/*from www. ja v a 2 s . com*/ * It is assumed that the packet's body starts at current position. * * @param buf byte buffer * @param size packet's body size * @return whether packet integrity is OK or not */ public static boolean verifyChecksum(ByteBuffer buf, final int size) { return verifyChecksum(buf, buf.position(), size, false); }