List of usage examples for java.nio ByteBuffer hasArray
public final boolean hasArray()
From source file:Main.java
public static void main(String[] argv) throws Exception { ByteBuffer bbuf = ByteBuffer.allocate(10); int capacity = bbuf.capacity(); // 10 System.out.println(capacity); bbuf.putShort(2, (short) 123); System.out.println(bbuf.hasArray()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { ByteBuffer bbuf = ByteBuffer.allocate(10); int capacity = bbuf.capacity(); // 10 System.out.println(capacity); bbuf.putShort(2, (short) 123); System.out.println(Arrays.toString(bbuf.array())); System.out.println(bbuf.hasArray()); }
From source file:Main.java
public static boolean wrapsFullArray(ByteBuffer byteBuffer) { return byteBuffer.hasArray() && byteBuffer.position() == 0 && byteBuffer.arrayOffset() == 0 && byteBuffer.remaining() == byteBuffer.capacity(); }
From source file:net.sf.jml.util.DigestUtils.java
private static void update(MessageDigest digest, ByteBuffer buffer) { if (buffer.hasArray()) { digest.update(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } else {/*ww w . j a v a 2s.c o m*/ byte[] b = new byte[buffer.remaining()]; buffer.get(b); digest.update(b); } }
From source file:Main.java
public static List<ByteBuffer> mergeAdjacentBuffers(List<ByteBuffer> samples) { ArrayList<ByteBuffer> nuSamples = new ArrayList<ByteBuffer>(samples.size()); for (ByteBuffer buffer : samples) { int lastIndex = nuSamples.size() - 1; if (lastIndex >= 0 && buffer.hasArray() && nuSamples.get(lastIndex).hasArray() && buffer.array() == nuSamples.get(lastIndex).array() && nuSamples.get(lastIndex).arrayOffset() + nuSamples.get(lastIndex).limit() == buffer.arrayOffset()) { ByteBuffer oldBuffer = nuSamples.remove(lastIndex); ByteBuffer nu = ByteBuffer .wrap(buffer.array(), oldBuffer.arrayOffset(), oldBuffer.limit() + buffer.limit()).slice(); // We need to slice here since wrap([], offset, length) just sets position and not the arrayOffset. nuSamples.add(nu);//from w ww .j a va2 s . com } else if (lastIndex >= 0 && buffer instanceof MappedByteBuffer && nuSamples.get(lastIndex) instanceof MappedByteBuffer && nuSamples.get(lastIndex) .limit() == nuSamples.get(lastIndex).capacity() - buffer.capacity()) { // This can go wrong - but will it? ByteBuffer oldBuffer = nuSamples.get(lastIndex); oldBuffer.limit(buffer.limit() + oldBuffer.limit()); } else { buffer.reset(); nuSamples.add(buffer); } } return nuSamples; }
From source file:Main.java
public static List<ByteBuffer> mergeAdjacentBuffers(List<ByteBuffer> paramList) { ArrayList localArrayList = new ArrayList(paramList.size()); Iterator localIterator = paramList.iterator(); while (localIterator.hasNext()) { ByteBuffer localByteBuffer1 = (ByteBuffer) localIterator.next(); int i = -1 + localArrayList.size(); if ((i >= 0) && (localByteBuffer1.hasArray()) && (((ByteBuffer) localArrayList.get(i)).hasArray()) && (localByteBuffer1.array() == ((ByteBuffer) localArrayList.get(i)).array()) && (((ByteBuffer) localArrayList.get(i)).arrayOffset() + ((ByteBuffer) localArrayList.get(i)).limit() == localByteBuffer1.arrayOffset())) { ByteBuffer localByteBuffer3 = (ByteBuffer) localArrayList.remove(i); localArrayList.add(ByteBuffer.wrap(localByteBuffer1.array(), localByteBuffer3.arrayOffset(), localByteBuffer3.limit() + localByteBuffer1.limit()).slice()); } else if ((i >= 0) && ((localByteBuffer1 instanceof MappedByteBuffer)) && ((localArrayList.get(i) instanceof MappedByteBuffer)) && (((ByteBuffer) localArrayList.get(i)) .limit() == ((ByteBuffer) localArrayList.get(i)).capacity() - localByteBuffer1.capacity())) { ByteBuffer localByteBuffer2 = (ByteBuffer) localArrayList.get(i); localByteBuffer2.limit(localByteBuffer1.limit() + localByteBuffer2.limit()); } else {/*from w w w . j a v a 2 s . c om*/ localByteBuffer1.reset(); localArrayList.add(localByteBuffer1); } } return localArrayList; }
From source file:Main.java
public static ByteBuffer copyBinary(final ByteBuffer orig) { if (orig == null) { return null; }/*from ww w . ja v a 2 s .c om*/ ByteBuffer copy = ByteBuffer.wrap(new byte[orig.remaining()]); if (orig.hasArray()) { System.arraycopy(orig.array(), orig.arrayOffset() + orig.position(), copy.array(), 0, orig.remaining()); } else { orig.slice().get(copy.array()); } return copy; }
From source file:cn.ctyun.amazonaws.util.StringUtils.java
/** * Base64 encodes the data in the specified byte buffer and returns it as a * base64 encoded string./*from w w w . ja va 2 s. c o m*/ * * @param byteBuffer * The data to base64 encode and return as a string. * * @return The base64 encoded contents of the specified byte buffer. */ public static String fromByteBuffer(ByteBuffer byteBuffer) { byte[] encodedBytes = null; if (byteBuffer.hasArray()) { encodedBytes = Base64.encodeBase64(byteBuffer.array()); } else { byte[] binaryData = new byte[byteBuffer.limit()]; byteBuffer.get(binaryData); encodedBytes = Base64.encodeBase64(binaryData); } return new String(encodedBytes); }
From source file:com.glaf.core.util.BinaryUtils.java
/** * Returns a copy of the bytes from the given <code>ByteBuffer</code>, * ranging from the the buffer's current position to the buffer's limit; or * null if the input is null.// w ww. j av a 2 s . co m * <p> * 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[] copyBytesFrom(ByteBuffer bb) { if (bb == null) return null; if (bb.hasArray()) return Arrays.copyOfRange(bb.array(), bb.position(), bb.limit()); bb.mark(); try { byte[] dst = new byte[bb.remaining()]; bb.get(dst); return dst; } finally { bb.reset(); } }
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>//w w w . j a va 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); } }