Refine search
/******************************************************************************************************************* * * * ******************************************************************************************************************/ @Nonnull private static SessionInputBufferImpl sessionInputBufferFrom (final @Nonnull InputStream is) { final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl(); final SessionInputBufferImpl sib = new SessionInputBufferImpl(metrics, 100); sib.bind(is); return sib; }
public int read() throws IOException { int noRead; while (!hasBufferedData()) { noRead = fillBuffer(); if (noRead == -1) { return -1; } } return this.buffer[this.bufferpos++] & 0xff; }
@Override public int read(final byte[] b) throws IOException { if (b == null) { return 0; } return read(b, 0, b.length); }
return 0; if (hasBufferedData()) { final int chunk = Math.min(len, this.bufferlen - this.bufferpos); System.arraycopy(this.buffer, this.bufferpos, b, off, chunk); final int read = streamRead(b, off, len); if (read > 0) { this.metrics.incrementBytesTransferred(read); while (!hasBufferedData()) { final int noRead = fillBuffer(); if (noRead == -1) { return -1;
while (retry) { final int i = locateLF(); if (i != -1) { return lineFromReadBuffer(charbuffer, i); } else { if (hasBufferedData()) { final int len = this.bufferlen - this.bufferpos; this.linebuffer.append(this.buffer, this.bufferpos, len); this.bufferpos = this.bufferlen; noRead = fillBuffer(); if (noRead == -1) { retry = false; return lineFromLineBuffer(charbuffer);
private ExpectedResult parseExpectedResponse(Element element, Evaluator evaluator, ResultRecorder resultRecorder) { String contents = getTextAndRemoveIndent(element); contents = replaceVariableReferences(evaluator, contents, resultRecorder); SessionInputBufferImpl buffer = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), contents.length()); buffer.bind(new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8))); DefaultHttpResponseParser defaultHttpResponseParser = new DefaultHttpResponseParser(buffer); ExpectedResult.ExpectedResultBuilder builder = expectedResult(); String body = null; try { HttpResponse httpResponse = defaultHttpResponseParser.parse(); StatusLine statusLine = httpResponse.getStatusLine(); builder.withStatus(statusLine.getStatusCode()); for (Header header : httpResponse.getAllHeaders()) { builder.withHeader(header.getName(), header.getValue()); } if (buffer.hasBufferedData()) { body = ""; while (buffer.hasBufferedData()) { body += (char) buffer.read(); } } builder.withBody(body); } catch (IOException | HttpException e) { e.printStackTrace(); } return builder.build(); }
super(); Args.positive(buffersize, "Buffer size"); final HttpTransportMetricsImpl inTransportMetrics = new HttpTransportMetricsImpl(); final HttpTransportMetricsImpl outTransportMetrics = new HttpTransportMetricsImpl(); this.inbuffer = new SessionInputBufferImpl(inTransportMetrics, buffersize, -1, messageConstraints != null ? messageConstraints : MessageConstraints.DEFAULT, chardecoder); this.outbuffer = new SessionOutputBufferImpl(outTransportMetrics, buffersize, fragmentSizeHint,
String s = "HTTP/1.1 200 OK\r\n" + "Content-Length: 100\r\n" + "Content-Type: text/plain\r\n" + "Server: some-server\r\n" + "\r\n"; SessionInputBufferImpl sessionInputBuffer = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), 2048); sessionInputBuffer.bind(new ByteArrayInputStream(s.getBytes(Consts.ASCII))); DefaultHttpResponseParser responseParser = new DefaultHttpResponseParser(sessionInputBuffer); HttpResponse response = responseParser.parse(); System.out.println(response);
protected void ensureOpen() throws IOException { final Socket socket = this.socketHolder.get(); if (socket == null) { throw new ConnectionClosedException("Connection is closed"); } if (!this.inbuffer.isBound()) { this.inbuffer.bind(getSocketInputStream(socket)); } if (!this.outbuffer.isBound()) { this.outbuffer.bind(getSocketOutputStream(socket)); } }
/** * Binds this connection to the given {@link Socket}. This socket will be * used by the connection to send and receive data. * <p> * After this method's execution the connection status will be reported * as open and the {@link #isOpen()} will return {@code true}. * * @param socket the socket. * @throws IOException in case of an I/O error. */ protected void bind(final Socket socket) throws IOException { Args.notNull(socket, "Socket"); this.socketHolder.set(socket); this.inbuffer.bind(null); this.outbuffer.bind(null); }
@Override public boolean isDataAvailable(final int timeout) throws IOException { return hasBufferedData(); }
private int lineFromReadBuffer(final CharArrayBuffer charbuffer, final int position) throws IOException { int pos = position; final int off = this.bufferpos; int len; this.bufferpos = pos + 1; if (pos > off && this.buffer[pos - 1] == HTTP.CR) { // skip CR if found pos--; } len = pos - off; if (this.decoder == null) { charbuffer.append(this.buffer, off, len); } else { final ByteBuffer bbuf = ByteBuffer.wrap(this.buffer, off, len); len = appendDecoded(charbuffer, bbuf); } return len; }
private int fillInputBuffer(final int timeout) throws IOException { final int oldtimeout = this.socket.getSoTimeout(); try { this.socket.setSoTimeout(timeout); return this.inbuffer.fillBuffer(); } finally { this.socket.setSoTimeout(oldtimeout); } }
private int appendDecoded( final CharArrayBuffer charbuffer, final ByteBuffer bbuf) throws IOException { if (!bbuf.hasRemaining()) { return 0; } if (this.cbuf == null) { this.cbuf = CharBuffer.allocate(1024); } this.decoder.reset(); int len = 0; while (bbuf.hasRemaining()) { final CoderResult result = this.decoder.decode(bbuf, this.cbuf, true); len += handleDecodingResult(result, charbuffer, bbuf); } final CoderResult result = this.decoder.flush(this.cbuf); len += handleDecodingResult(result, charbuffer, bbuf); this.cbuf.clear(); return len; }
@Override public void close() throws IOException { final Socket socket = this.socketHolder.getAndSet(null); if (socket != null) { try { this.inbuffer.clear(); this.outbuffer.flush(); try { try { socket.shutdownOutput(); } catch (final IOException ignore) { } try { socket.shutdownInput(); } catch (final IOException ignore) { } } catch (final UnsupportedOperationException ignore) { // if one isn't supported, the other one isn't either } } finally { socket.close(); } } }
return 0; if (hasBufferedData()) { final int chunk = Math.min(len, this.bufferlen - this.bufferpos); System.arraycopy(this.buffer, this.bufferpos, b, off, chunk); final int read = streamRead(b, off, len); if (read > 0) { this.metrics.incrementBytesTransferred(read); while (!hasBufferedData()) { final int noRead = fillBuffer(); if (noRead == -1) { return -1;
while (retry) { final int i = locateLF(); if (i != -1) { return lineFromReadBuffer(charbuffer, i); } else { if (hasBufferedData()) { final int len = this.bufferlen - this.bufferpos; this.linebuffer.append(this.buffer, this.bufferpos, len); this.bufferpos = this.bufferlen; noRead = fillBuffer(); if (noRead == -1) { retry = false; return lineFromLineBuffer(charbuffer);
SessionInputBufferImpl buffer = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), contents.length()); buffer.bind(new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8))); DefaultHttpRequestParser defaultHttpRequestParser = new DefaultHttpRequestParser(buffer); LinkedListMultimap<String, String> queryParameters = LinkedListMultimap.create(); if (buffer.hasBufferedData()) { body = ""; while (buffer.hasBufferedData()) { body += (char) buffer.read();
super(); Args.positive(buffersize, "Buffer size"); final HttpTransportMetricsImpl inTransportMetrics = new HttpTransportMetricsImpl(); final HttpTransportMetricsImpl outTransportMetrics = new HttpTransportMetricsImpl(); this.inbuffer = new SessionInputBufferImpl(inTransportMetrics, buffersize, -1, messageConstraints != null ? messageConstraints : MessageConstraints.DEFAULT, chardecoder); this.outbuffer = new SessionOutputBufferImpl(outTransportMetrics, buffersize, fragmentSizeHint,