List of usage examples for java.nio ByteBuffer hasArray
public final boolean hasArray()
From source file:com.glaf.core.util.ByteBufferUtils.java
/** * Transfer bytes from one ByteBuffer to another. This function acts as * System.arrayCopy() but for ByteBuffers. * // w ww. ja va2 s . c om * @param src * the source ByteBuffer * @param srcPos * starting position in the source ByteBuffer * @param dst * the destination ByteBuffer * @param dstPos * starting position in the destination ByteBuffer * @param length * the number of bytes to copy */ public static void arrayCopy(ByteBuffer src, int srcPos, ByteBuffer dst, int dstPos, int length) { if (src.hasArray() && dst.hasArray()) { System.arraycopy(src.array(), src.arrayOffset() + srcPos, dst.array(), dst.arrayOffset() + dstPos, length); } else { if (src.limit() - srcPos < length || dst.limit() - dstPos < length) throw new IndexOutOfBoundsException(); for (int i = 0; i < length; i++) dst.put(dstPos++, src.get(srcPos++)); } }
From source file:com.glaf.core.util.ByteBufferUtils.java
/** * @return a new copy of the data in @param buffer USUALLY YOU SHOULD USE * ByteBuffer.duplicate() INSTEAD, which creates a new Buffer (so * you can mutate its position without affecting the original) * without copying the underlying array. *//*from w w w .j av a 2 s . com*/ public static ByteBuffer clone(ByteBuffer buffer) { assert buffer != null; if (buffer.remaining() == 0) return EMPTY_BYTE_BUFFER; ByteBuffer clone = ByteBuffer.allocate(buffer.remaining()); if (buffer.hasArray()) { System.arraycopy(buffer.array(), buffer.arrayOffset() + buffer.position(), clone.array(), 0, buffer.remaining()); } else { clone.put(buffer.duplicate()); clone.flip(); } return clone; }
From source file:net.darkmist.alib.io.BufferUtil.java
public static byte[] asBytes(ByteBuffer buf) { buf = buf.duplicate();/*w ww.j av a 2 s . co m*/ /* To use buf.array() the buffer must: * be writable as the array will be writable * have arrayOffset() == 0 or the array will not start at the right location * the returned array must be the same length as the buffer's limit or it will be the wrong size. */ if (!buf.isReadOnly() && buf.hasArray() && buf.arrayOffset() == 0) { logger.debug("!read-only, hasArray && offset is 0"); byte[] ret = buf.array(); if (ret.length == buf.limit()) return ret; logger.debug("length of array !=limit, doing copy..."); } byte[] bytes = new byte[buf.limit()]; buf.get(bytes, 0, buf.limit()); return bytes; }
From source file:com.ah.ui.actions.home.clientManagement.service.CertificateGenSV.java
@SuppressWarnings("resource") public static byte[] readFromFile(File file) throws IOException { FileChannel fileChannel = new FileInputStream(file).getChannel(); ByteBuffer bb = ByteBuffer.allocate((int) fileChannel.size()); fileChannel.read(bb);//from w w w.j av a2 s. c om fileChannel.close(); bb.flip(); byte[] bytes; if (bb.hasArray()) { bytes = bb.array(); } else { bytes = new byte[bb.limit()]; bb.get(bytes); } return bytes; }
From source file:com.glaf.core.util.ByteBufferUtils.java
/** * from to /*from www. j av a 2 s . c om*/ * * @param fromBuffer * Buffer ? flush * @param toBuffer * Buffer ? fill * @return number of bytes moved */ public static int put(ByteBuffer fromBuffer, ByteBuffer toBuffer) { int put; int remaining = fromBuffer.remaining(); if (remaining > 0) { // if (remaining <= toBuffer.remaining()) { toBuffer.put(fromBuffer); put = remaining; // from fromBuffer.position(fromBuffer.limit()); } // heap buffer else if (fromBuffer.hasArray()) { put = toBuffer.remaining(); // ?? toBuffer.put(fromBuffer.array(), fromBuffer.arrayOffset() + fromBuffer.position(), put); fromBuffer.position(fromBuffer.position() + put); } // direct buffer else { // ?? put = toBuffer.remaining(); ByteBuffer slice = fromBuffer.slice(); slice.limit(put); toBuffer.put(slice); fromBuffer.position(fromBuffer.position() + put); } } else { put = 0; } return put; }
From source file:com.homeadvisor.kafdrop.service.MessageInspector.java
private byte[] readBytes(ByteBuffer buffer, int offset, int size) { byte[] dest = new byte[size]; if (buffer.hasArray()) { System.arraycopy(buffer.array(), buffer.arrayOffset() + offset, dest, 0, size); } else {/*w ww . j a v a 2 s. co m*/ buffer.mark(); buffer.get(dest); buffer.reset(); } return dest; }
From source file:com.joyent.manta.http.entity.DigestedEntity.java
@Override public void writeTo(final OutputStream out) throws IOException { digest.reset(); // reset the digest state in case we're in a retry // If our wrapped entity is backed by a buffer of some form // we can read easily read the whole buffer into our message digest. if (wrapped instanceof MemoryBackedEntity) { final MemoryBackedEntity entity = (MemoryBackedEntity) wrapped; final ByteBuffer backingBuffer = entity.getBackingBuffer(); if (backingBuffer.hasArray()) { final byte[] bytes = backingBuffer.array(); final int offset = backingBuffer.arrayOffset(); final int position = backingBuffer.position(); final int limit = backingBuffer.limit(); digest.update(bytes, offset + position, limit - position); backingBuffer.position(limit); wrapped.writeTo(out);//from w ww . j av a 2 s. c o m } } else { try (DigestOutputStream dout = new DigestOutputStream(digest); TeeOutputStream teeOut = new TeeOutputStream(out, dout)) { wrapped.writeTo(teeOut); teeOut.flush(); } } }
From source file:nl.salp.warcraft4j.io.HttpDataReader.java
/** * {@inheritDoc}/*from ww w. j a va2s . com*/ */ @Override protected int readData(ByteBuffer buffer) throws DataReadingException { try { int read = 0; byte[] dataArray; if (buffer.hasArray()) { read = responseStream.read(buffer.array()); } else { byte[] data = new byte[buffer.capacity()]; read = responseStream.read(data); buffer.put(data); } return read; } catch (IOException e) { throw new DataReadingException(e); } }
From source file:com.github.mrstampy.gameboot.otp.websocket.OtpEncryptedWebSocketHandler.java
private byte[] extractArray(WebSocketSession session, BinaryMessage message) throws IOException { ByteBuffer buf = message.getPayload(); if (buf.hasArray()) return buf.array(); int size = buf.remaining(); if (size == 0) { log.error("No message, closing session {}", session); session.close();// w w w .ja va2 s. c om return null; } byte[] b = new byte[size]; buf.get(b, 0, b.length); return b; }
From source file:co.cask.cdap.client.rest.RestStreamWriter.java
@Override public ListenableFuture<Void> write(ByteBuffer buffer, Map<String, String> headers) throws IllegalArgumentException { Preconditions.checkArgument(buffer != null, "ByteBuffer parameter is null."); HttpEntity content;//ww w . j a v a2 s. co m if (buffer.hasArray()) { content = new ByteArrayEntity(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } else { byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); content = new ByteArrayEntity(bytes); } return write(content, headers); }