Refine search
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 toBuffer(String s) { return toBuffer(s, StandardCharsets.ISO_8859_1); }
public static String toDetailString(ByteBuffer[] buffer) { StringBuilder builder = new StringBuilder(); builder.append('['); for (int i = 0; i < buffer.length; i++) { if (i > 0) builder.append(','); builder.append(toDetailString(buffer[i])); } builder.append(']'); return builder.toString(); }
/** Appends a buffer to a buffer * @param to Buffer is flush mode * @param b buffer to append * @return The position of the valid data before the flipped position. */ public static int append(ByteBuffer to, ByteBuffer b) { int pos = flipToFill(to); try { return put(b, to); } finally { flipToFlush(to, pos); } }
/** Appends a byte to a buffer * @param to Buffer is flush mode * @param b byte to append */ public static void append(ByteBuffer to, byte b) { int pos = flipToFill(to); try { to.put(b); } finally { flipToFlush(to, pos); } }
private IllegalCharacterException(State state,HttpTokens.Token token,ByteBuffer buffer) { super(400,String.format("Illegal character %s",token)); if (LOG.isDebugEnabled()) LOG.debug(String.format("Illegal character %s in state=%s for buffer %s",token,state,BufferUtil.toDetailString(buffer))); } }
@Override public void upgrade(Connection newConnection) { Connection old_connection = getConnection(); if (LOG.isDebugEnabled()) LOG.debug("{} upgrading from {} to {}", this, old_connection, newConnection); ByteBuffer prefilled = (old_connection instanceof Connection.UpgradeFrom) ?((Connection.UpgradeFrom)old_connection).onUpgradeFrom():null; old_connection.onClose(); old_connection.getEndPoint().setConnection(newConnection); if (newConnection instanceof Connection.UpgradeTo) ((Connection.UpgradeTo)newConnection).onUpgradeTo(prefilled); else if (BufferUtil.hasContent(prefilled)) throw new IllegalStateException(); newConnection.onOpen(); }
/** * Blocking send of whole content. * * @param content The whole content to send * @throws IOException if the send fails */ public void sendContent(ByteBuffer content) throws IOException { if (LOG.isDebugEnabled()) LOG.debug("sendContent({})",BufferUtil.toDetailString(content)); write(content, true); closed(); }
private Action flush() if (!BufferUtil.isEmpty(aggregate)) if (LOG.isDebugEnabled()) LOG.debug("{} flushing aggregate {}", this, aggregate); if (BufferUtil.hasContent(payload)) buffers.add(payload); if (LOG.isDebugEnabled()) LOG.debug("{} flushing {} frames: {}", this, entries.size(), entries); endPoint.write(this, buffers.toArray(new ByteBuffer[buffers.size()])); buffers.clear(); return Action.SCHEDULED;
@Override public void onFillable() { try { while (true) { // Avoid to read too much from the socket: ask // the parser how much left there is to read. ByteBuffer buffer = BufferUtil.allocate(parser.expected()); int filled = getEndPoint().fill(buffer); if (LOG.isDebugEnabled()) LOG.debug("Read SOCKS4 connect response, {} bytes", filled); if (filled < 0) throw new IOException("SOCKS4 tunnel failed, connection closed"); if (filled == 0) { fillInterested(); return; } if (parser.parse(buffer)) return; } } catch (Throwable x) { failed(x); } }
_writeListener.onError(_onError==null?new EofException("Async close"):_onError); break; write(BufferUtil.hasContent(_aggregate)?_aggregate:BufferUtil.EMPTY_BUFFER, !_channel.getResponse().isIncluding()); releaseBuffer();
@Override public void input(ByteBuffer input) { if (LOG.isDebugEnabled()) { LOG.debug("input: {}",BufferUtil.toDetailString(input)); } // Set the data that is uncompressed to the deflater byte raw[] = BufferUtil.toArray(input); deflater.setInput(raw,0,raw.length); deflater.finish(); }
@Override public void close() { if (!isClosed()) { try { if (BufferUtil.hasContent(_aggregate)) _channel.write(_aggregate, !_channel.getResponse().isIncluding()); else _channel.write(BufferUtil.EMPTY_BUFFER, !_channel.getResponse().isIncluding()); } catch(IOException e) { _channel.getEndPoint().shutdownOutput(); LOG.ignore(e); } } closed(); }
LOG.debug("flushed {} {}", flushed, this); throw new EofException(e); if (BufferUtil.isEmpty(b)) BufferUtil.clear(b); else all_flushed=false;
LOG.debug("onIncompleteFlush {}", getEndPoint()); if (BufferUtil.hasContent(_encryptedOutput)) getEndPoint().write(_writeCallback, _encryptedOutput);
@Override public void flush() throws IOException { while(true) { switch(_state.get()) { case OPEN: write(BufferUtil.hasContent(_aggregate)?_aggregate:BufferUtil.EMPTY_BUFFER, false); return; case ASYNC: throw new IllegalStateException("isReady() not called"); case READY: if (!_state.compareAndSet(OutputState.READY, OutputState.PENDING)) continue; new AsyncFlush().iterate(); return; case PENDING: case UNREADY: throw new WritePendingException(); case ERROR: throw new EofException(_onError); case CLOSED: return; default: throw new IllegalStateException(); } } }
throw new EofException("CLOSED"); if (BufferUtil.hasContent(in)) filled=BufferUtil.append(buffer,in); if (BufferUtil.isEmpty(in)) _inQ.poll(); break;
@Override public void write(int b) throws IOException { if (isClosed()) throw new EOFException("Closed"); if (_aggregate == null) _aggregate = _channel.getByteBufferPool().acquire(getBufferSize(), OUTPUT_BUFFER_DIRECT); BufferUtil.append(_aggregate, (byte)b); _written++; // Check if all written or full if (!closeIfAllContentWritten() && BufferUtil.isFull(_aggregate)) _channel.write(_aggregate, false); }
public ByteBuffer acquire(int capacity, boolean direct) { ByteBuffer buffer = byteBufferPool.acquire(capacity, direct); BufferUtil.clearToFill(buffer); return buffer; }