List of usage examples for java.nio ByteBuffer remaining
public final int remaining()
From source file:com.liveramp.commons.util.BytesUtils.java
public static ByteBuffer byteBufferDeepCopy(ByteBuffer src) { ByteBuffer copy = ByteBuffer.allocate(src.remaining()).put(src.slice()); copy.flip();/*from w ww . java 2s . c o m*/ return copy; }
From source file:com.icloud.framework.core.nio.ByteBufferUtil.java
public static String string(ByteBuffer b, Charset charset) { return new String(b.array(), b.arrayOffset() + b.position(), b.remaining(), charset); }
From source file:com.liveramp.commons.util.BytesUtils.java
public static ByteBuffer byteBufferDeepCopy(ByteBuffer src, ByteBuffer dst) { if (dst == null || dst.capacity() < src.remaining()) { dst = byteBufferDeepCopy(src);// ww w. j a v a 2s .c o m } else { dst.rewind(); dst.limit(src.remaining()); dst.put(src.slice()); dst.flip(); } return dst; }
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 w w w .j ava 2 s .c om if (v < 16) { result.append("0"); } result.append(Integer.toString(v, 16)); } return result.toString(); }
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;/*from ww w . java 2 s . c o m*/ }
From source file:com.liveramp.cascading_ext.Bytes.java
/** * If the given ByteBuffer wraps completely its underlying byte array, return the underlying * byte array (no copy). Otherwise, return a deep copy of the range represented by the given * ByteBuffer./*from w w w. j a v a2 s . c o m*/ * * @param byteBuffer */ public static byte[] byteBufferToByteArray(ByteBuffer byteBuffer) { if (wrapsFullArray(byteBuffer)) { return byteBuffer.array(); } byte[] target = new byte[byteBuffer.remaining()]; byteBufferToByteArray(byteBuffer, target, 0); return target; }
From source file:com.icloud.framework.core.nio.ByteBufferUtil.java
public static byte[] getBytes(ByteBuffer byteBuffer) { byte[] bytes = // new byte[byteBuffer.limit()-byteBuffer.position()]; new byte[byteBuffer.remaining()]; byteBuffer.get(bytes);//from w ww . j a va 2s . c om return bytes; }
From source file:com.offbynull.portmapper.common.ByteBufferUtils.java
/** * Copy the remaining content of a {@link ByteBuffer} in to a new array. * @param src buffer to copy/*from w ww .j a va2s. com*/ * @param incrementSrc of {@code true} increments {@code src}'s position * @return new buffer with the remaining content in {@code src} * @throws NullPointerException if any arguments are {@code null} */ public static byte[] copyContentsToArray(ByteBuffer src, boolean incrementSrc) { Validate.notNull(src); if (!incrementSrc) { src.mark(); } ByteBuffer dst = ByteBuffer.allocate(src.remaining()); dst.put(src); if (!incrementSrc) { src.reset(); } return dst.array(); }
From source file:Main.java
public static final int compareToP(ByteBuffer bb1, ByteBuffer bb2) { final int offset1 = bb1.position(); final int offset2 = bb2.position(); final byte[] array1 = bb1.array(); final byte[] array2 = bb2.array(); final int len1 = bb1.remaining(); final int len2 = bb2.remaining(); return compareTo(array1, offset1, len1, array2, offset2, len2); }
From source file:Main.java
public static final String convertStringCharset(String originalString, String sourceCharset, String targetCharset) {/*from w w w .j av a 2 s.co m*/ if (sourceCharset.equalsIgnoreCase(targetCharset)) { return originalString; } final Charset charset = Charset.forName(sourceCharset); final ByteBuffer byteBuffer = charset.encode(originalString); // byteBuffer.array() "may" return byte array which is larger than // byteBuffer.remaining(). Here, we keep on the safe side. final byte[] bytes = new byte[byteBuffer.remaining()]; byteBuffer.get(bytes); try { return new String(bytes, targetCharset); } catch (UnsupportedEncodingException e) { Log.e(LOG_TAG, "Failed to encode: charset=" + targetCharset); return null; } }