List of usage examples for java.nio ByteBuffer rewind
public final Buffer rewind()
From source file:org.colombbus.tangara.io.ScriptReader.java
/** * Try to decode a byte buffer with a charset *// w ww . j a v a 2 s. c o m * @param content * the bute buffer * @param cs * the charset * @return <code>null</code> if the charset is not supported, or the decoded * string */ private static String tryDecodeBufferWithCharset(ByteBuffer content, Charset cs) { CharBuffer buffer = CharBuffer.allocate(content.capacity() * 2); CharsetDecoder decoder = createDecoder(cs); content.rewind(); CoderResult coderRes = decoder.decode(content, buffer, true); if (coderRes.isError() == false) { buffer.rewind(); return buffer.toString().trim(); } return null; }
From source file:org.apache.hadoop.hbase.KeyValueTestUtil.java
public static ByteBuffer toByteBufferAndRewind(final Iterable<? extends KeyValue> kvs, boolean includeMemstoreTS) { int totalBytes = KeyValueUtil.totalLengthWithMvccVersion(kvs, includeMemstoreTS); ByteBuffer bb = ByteBuffer.allocate(totalBytes); for (KeyValue kv : IterableUtils.emptyIfNull(kvs)) { KeyValueUtil.appendToByteBuffer(bb, kv, includeMemstoreTS); }/*w ww . j a v a2 s .c o m*/ bb.rewind(); return bb; }
From source file:info.gehrels.flockDBClient.ByteHelper.java
static ByteBuffer asByteBufferOrNull(long... destinationIds) { ByteBuffer buf = null; if (isNotEmpty(destinationIds)) { buf = ByteBuffer.wrap(new byte[destinationIds.length * (Long.SIZE / 8)]); buf.order(ByteOrder.LITTLE_ENDIAN); for (long destinationId : destinationIds) { buf.putLong(destinationId);/* w w w . j a va 2 s .c o m*/ } buf.rewind(); } return buf; }
From source file:Main.java
public static ByteBuffer clone(final ByteBuffer buf) { if (buf == null) { return null; }//from w w w .j a va2 s. c o m buf.rewind(); final ByteBuffer copy; if (buf.isDirect()) { copy = createByteBuffer(buf.limit()); } else { copy = createByteBufferOnHeap(buf.limit()); } copy.put(buf); return copy; }
From source file:com.icloud.framework.core.util.FBUtilities.java
public static ByteBuffer toByteBuffer(long n) { byte[] bytes = new byte[8]; ByteBuffer bb = ByteBuffer.wrap(bytes).putLong(n); bb.rewind(); return bb;//from ww w. ja v a 2 s. c o m }
From source file:org.lic.ip.iplocator.IPv4RadixIntTree.java
private static long inet_aton(String ipStr) { try {// w ww . j a v a 2 s . c o m ByteBuffer bb = ByteBuffer.allocate(8); bb.putInt(0); bb.put(InetAddress.getByName(ipStr).getAddress()); bb.rewind(); return bb.getLong(); } catch (UnknownHostException e) { logger.error(e.getMessage(), e); } return 0; }
From source file:org.apache.myriad.state.utils.ByteBufferSupport.java
public static ByteBuffer fillBuffer(byte src[]) { ByteBuffer bb = createBuffer(src.length); bb.put(src);//from w ww .j a v a2 s .co m bb.rewind(); return bb; }
From source file:org.apache.myriad.state.utils.ByteBufferSupport.java
public static ByteBuffer toBuffer(GeneratedMessage message) { byte dst[];//w w w . j a v a 2 s .c o m int size; if (message != null) { size = message.getSerializedSize() + INT_SIZE; dst = message.toByteArray(); } else { size = INT_SIZE; dst = ZERO_BYTES; } ByteBuffer bb = createBuffer(size); putBytes(bb, dst); bb.rewind(); return bb; }
From source file:jext2.Inode.java
/** allocate a ByteBuffer big enough for a Inode */ public static ByteBuffer allocateByteBuffer() { ByteBuffer buf = ByteBuffer.allocate(Superblock.getInstance().getInodeSize()); buf.rewind(); return buf;//from www. ja v a 2s. com }
From source file:com.amazonaws.services.sqs.MessageMD5ChecksumHandler.java
/** * Update the digest using a sequence of bytes that consists of the length * (in 4 bytes) of the input ByteBuffer and all the bytes it contains. *//* ww w.j av a 2s . c o m*/ private static void updateLengthAndBytes(MessageDigest digest, ByteBuffer binaryValue) { // Rewind the ByteBuffer, in case that get/put operations were applied to // the unmarshalled BB before it's passed to this handler. binaryValue.rewind(); int size = binaryValue.remaining(); ByteBuffer lengthBytes = ByteBuffer.allocate(INTEGER_SIZE_IN_BYTES).putInt(size); digest.update(lengthBytes.array()); digest.update(binaryValue); }