List of usage examples for java.nio ByteBuffer position
public final int position()
From source file:com.linkedin.databus.core.DbusEventPart.java
/** * Decodes a bytebuffer and returns DbusEventPart. Preserves the ByteBuffer position. * @param buf//from w w w. jav a 2 s. c om * @return */ public static DbusEventPart decode(ByteBuffer buf) { int pos = buf.position(); int dataLen = buf.getInt(pos); if (dataLen < 0) { throw new UnsupportedOperationException("Data length " + dataLen + " not supported"); } short attributes = buf.getShort(pos + AttributesOffset); short schemaVersion = (short) (attributes >> VERSION_SHIFT); SchemaDigestType schemaDigestType = digestType(attributes); int digestLen = digestLen(schemaDigestType); byte[] digest = new byte[digestLen]; for (int i = 0; i < digestLen; i++) { digest[i] = buf.get(pos + AttributesOffset + AttributesLen + i); } // NOTE - this will create a new ByteBuffer object pointing to the // same memory. So the position of this new BB is beginning of the data // and limit is set to the end of the data. ByteBuffer dataBuf = buf.asReadOnlyBuffer(); dataBuf.position(pos + AttributesOffset + AttributesLen + digestLen); dataBuf.limit(dataBuf.position() + dataLen); return new DbusEventPart(schemaDigestType, digest, schemaVersion, dataBuf); }
From source file:ch.cyberduck.core.http.DelayedHttpMultipartEntity.java
private static ByteArrayBuffer encode(final Charset charset, final String input) { final ByteBuffer encoded = charset.encode(CharBuffer.wrap(input)); final ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining()); bab.append(encoded.array(), encoded.position(), encoded.remaining()); return bab;// w w w.j a va2s.c o m }
From source file:com.kotcrab.vis.editor.module.editor.AnalyticsModule.java
public static int truncateUtf8(String input, byte[] output) { ByteBuffer outBuf = ByteBuffer.wrap(output); CharBuffer inBuf = CharBuffer.wrap(input.toCharArray()); Charset utf8 = Charset.forName("UTF-8"); utf8.newEncoder().encode(inBuf, outBuf, true); return outBuf.position(); }
From source file:com.liveramp.commons.util.BytesUtils.java
public static String bytesToHexString(ByteBuffer b) { StringBuilder result = new StringBuilder(); for (int i = 0; i < b.remaining(); ++i) { final int v = b.array()[b.arrayOffset() + b.position() + i] & 0xff; if (i > 0) { result.append(" "); }//from ww w.j ava 2s. c om if (v < 16) { result.append("0"); } result.append(Integer.toString(v, 16)); } return result.toString(); }
From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueServices.java
static String convertCassandraByteBufferUUIDtoString(ByteBuffer uuid) { return new UUID(uuid.getLong(uuid.position()), uuid.getLong(uuid.position() + 8)).toString(); }
From source file:com.mcxiaoke.next.http.entity.mime.AbstractMultipartForm.java
private static ByteArrayBuffer encode(final Charset charset, final String string) { final ByteBuffer encoded = charset.encode(CharBuffer.wrap(string)); final ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining()); bab.append(encoded.array(), encoded.position(), encoded.remaining()); return bab;//w ww. j ava 2s . c om }
From source file:Main.java
public static ReadableByteChannel asReadableByteChannel(final ByteBuffer buffer) { return new ReadableByteChannel() { private boolean open = true; public int read(ByteBuffer dst) throws IOException { if (open == false) { throw new ClosedChannelException(); }// www. java 2s. c o m final int p = buffer.position(); final int l = buffer.limit(); final int r = dst.remaining(); buffer.limit(p + r); dst.put(buffer); buffer.limit(l); return r; } public void close() throws IOException { open = false; } public boolean isOpen() { return open; } }; }
From source file:de.csdev.ebus.utils.EBusUtils.java
/** * Convert a ByteBuffer to a byte array// ww w . j a v a2 s .c o m * * @param buffer * @return */ public static byte[] toByteArray(ByteBuffer buffer) { int size = 0; if (buffer.position() == 0) { size = buffer.limit(); } else { size = buffer.position(); } byte[] data = new byte[size]; ((ByteBuffer) buffer.duplicate().clear()).get(data); return data; }
From source file:Main.java
/** * Compute the sha-256 digest of data.// w ww.j av a 2 s . c o m * @param data The input byte buffer. This does not change the position. * @return The digest. */ public static byte[] digestSha256(ByteBuffer data) { MessageDigest sha256; try { sha256 = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException exception) { // Don't expect this to happen. throw new Error("MessageDigest: SHA-256 is not supported: " + exception.getMessage()); } int savePosition = data.position(); sha256.update(data); data.position(savePosition); return sha256.digest(); }
From source file:Main.java
/** * @return/*from w w w. j a v a2 s . c o m*/ */ public static WritableByteChannel asWritableByteChannel(final ByteBuffer buffer) { return new WritableByteChannel() { private boolean open = true; public int write(ByteBuffer src) throws IOException { if (open == false) { throw new ClosedChannelException(); } final int p = buffer.position(); final int l = buffer.limit(); final int r = src.remaining(); buffer.limit(l + r); buffer.position(l); buffer.put(src); buffer.position(p); return r; } public void close() throws IOException { open = false; } public boolean isOpen() { return open; } }; }