public static void readFrom(InputStream is, int needed, ByteBuffer buffer) throws IOException { ByteBuffer tmp = allocate(8192); while (needed > 0 && buffer.hasRemaining()) { int l = is.read(tmp.array(), 0, 8192); if (l < 0) break; tmp.position(0); tmp.limit(l); buffer.put(tmp); } }
public static ByteBuffer toBuffer(Resource resource,boolean direct) throws IOException { int len=(int)resource.length(); if (len<0) throw new IllegalArgumentException("invalid resource: "+String.valueOf(resource)+" len="+len); ByteBuffer buffer = direct?BufferUtil.allocateDirect(len):BufferUtil.allocate(len); int pos=BufferUtil.flipToFill(buffer); if (resource.getFile()!=null) BufferUtil.readFrom(resource.getFile(),buffer); else { try (InputStream is = resource.getInputStream();) { BufferUtil.readFrom(is,len,buffer); } } BufferUtil.flipToFlush(buffer,pos); return buffer; }
public static ByteBuffer ensureCapacity(ByteBuffer buffer, int capacity) { if (buffer==null) return allocate(capacity); if (buffer.capacity()>=capacity) return buffer; if (buffer.hasArray()) return ByteBuffer.wrap(Arrays.copyOfRange(buffer.array(), buffer.arrayOffset(), buffer.arrayOffset()+capacity),buffer.position(),buffer.remaining()); throw new UnsupportedOperationException(); }
@Override public ByteBuffer acquire(int size, boolean direct) { Bucket bucket = bucketFor(size,direct); ByteBuffer buffer = bucket==null?null:bucket._queue.poll(); if (buffer == null) { int capacity = bucket==null?size:bucket._size; buffer = direct ? BufferUtil.allocateDirect(capacity) : BufferUtil.allocate(capacity); } return buffer; }
public static ByteBuffer ensureCapacity(ByteBuffer buffer, int capacity) { if (buffer==null) return allocate(capacity); if (buffer.capacity()>=capacity) return buffer; if (buffer.hasArray()) return ByteBuffer.wrap(Arrays.copyOfRange(buffer.array(), buffer.arrayOffset(), buffer.arrayOffset()+capacity),buffer.position(),buffer.remaining()); throw new UnsupportedOperationException(); }
@Override public ByteBuffer acquire(int size, boolean direct) { Bucket bucket = bucketFor(size,direct); ByteBuffer buffer = bucket==null?null:bucket._queue.poll(); if (buffer == null) { int capacity = bucket==null?size:bucket._size; buffer = direct ? BufferUtil.allocateDirect(capacity) : BufferUtil.allocate(capacity); } return buffer; }
public static ByteBuffer ensureCapacity(ByteBuffer buffer, int capacity) { if (buffer==null) return allocate(capacity); if (buffer.capacity()>=capacity) return buffer; if (buffer.hasArray()) return ByteBuffer.wrap(Arrays.copyOfRange(buffer.array(), buffer.arrayOffset(), buffer.arrayOffset()+capacity),buffer.position(),buffer.remaining()); throw new UnsupportedOperationException(); }
/** * @param capacity capacity capacity of the allocated ByteBuffer * @return An indirect buffer of the configured buffersize either from the pool or freshly allocated. */ public ByteBuffer acquire(int capacity) { return _pool==null?BufferUtil.allocate(capacity):_pool.acquire(capacity,false); }
/** * @param input the input bytes * @param outputSize the output size */ public ByteArrayEndPoint(byte[] input, int outputSize) { this(null,0,input!=null?BufferUtil.toBuffer(input):null,BufferUtil.allocate(outputSize)); }
/** * @param input the input string (converted to bytes using default encoding charset) * @param outputSize the output size */ public ByteArrayEndPoint(String input, int outputSize) { this(null,0,input!=null?BufferUtil.toBuffer(input):null,BufferUtil.allocate(outputSize)); }
private ByteBuffer preserve(final ByteBuffer dst, final int newLimit, final int oldLimit) { ByteBuffer buf = BufferUtil.allocate(newLimit - oldLimit); ByteBuffer slice = dst.slice(); slice.position(oldLimit); slice.limit(newLimit); BufferUtil.append(buf, slice); return buf; } }
/** * */ public ByteArrayEndPoint(byte[] input, int outputSize) { this(null,0,input!=null?BufferUtil.toBuffer(input):null,BufferUtil.allocate(outputSize)); }
/** * */ public ByteArrayEndPoint(String input, int outputSize) { this(null,0,input!=null?BufferUtil.toBuffer(input):null,BufferUtil.allocate(outputSize)); }
public ByteArrayEndPoint(Scheduler timer, long idleTimeoutMs, ByteBuffer input, ByteBuffer output) { super(timer,NOIP,NOIP); _in=input==null?BufferUtil.EMPTY_BUFFER:input; _out=output==null?BufferUtil.allocate(1024):output; setIdleTimeout(idleTimeoutMs); }
public ByteArrayEndPoint(Scheduler timer, long idleTimeoutMs, byte[] input, int outputSize) { this(timer,idleTimeoutMs,input!=null?BufferUtil.toBuffer(input):null,BufferUtil.allocate(outputSize)); }
/** * @param capacity capacity capacity of the allocated ByteBuffer * @return An indirect buffer of the configured buffersize either from the pool or freshly allocated. */ public ByteBuffer acquire(int capacity) { return _pool==null?BufferUtil.allocate(capacity):_pool.acquire(capacity,false); }
/** * @return Returns the out. */ public ByteBuffer takeOutput() { ByteBuffer b=_out; _out=BufferUtil.allocate(b.capacity()); getWriteFlusher().completeWrite(); return b; }
/** * @return Returns the out. */ public ByteBuffer takeOutput() { ByteBuffer b=_out; _out=BufferUtil.allocate(b.capacity()); getWriteFlusher().completeWrite(); return b; }
/** * @return Returns the out. */ public ByteBuffer takeOutput() { ByteBuffer b=_out; _out=BufferUtil.allocate(b.capacity()); getWriteFlusher().completeWrite(); return b; }
public ByteArrayEndPoint(Scheduler timer, long idleTimeoutMs, ByteBuffer input, ByteBuffer output) { super(timer); if (BufferUtil.hasContent(input)) addInput(input); _out=output==null?BufferUtil.allocate(1024):output; setIdleTimeout(idleTimeoutMs); onOpen(); }