in httpcore5-testing/src/main/java/org/apache/hc/core5/benchmark/BenchmarkWorker.java [179:273]
private AsyncResponseConsumer<Void> createResponseConsumer() {
return new AsyncResponseConsumer<Void>() {
volatile int status;
volatile Charset charset;
final AtomicLong contentLength = new AtomicLong();
final AtomicReference<FutureCallback<Void>> resultCallbackRef = new AtomicReference<>();
@Override
public void consumeResponse(
final HttpResponse response,
final EntityDetails entityDetails,
final HttpContext context,
final FutureCallback<Void> resultCallback) throws HttpException, IOException {
status = response.getCode();
resultCallbackRef.set(resultCallback);
stats.setVersion(response.getVersion());
final Header serverHeader = response.getFirstHeader(HttpHeaders.SERVER);
if (serverHeader != null) {
stats.setServerName(serverHeader.getValue());
}
if (config.getVerbosity() >= 2) {
System.out.println(response.getCode());
}
if (entityDetails != null) {
if (config.getVerbosity() >= 6) {
if (entityDetails.getContentType() != null) {
final ContentType contentType = ContentType.parseLenient(entityDetails.getContentType());
charset = ContentType.getCharset(contentType, null);
}
}
} else {
streamEnd(null);
}
}
@Override
public void informationResponse(
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
}
@Override
public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
capacityChannel.update(Integer.MAX_VALUE);
}
@Override
public void consume(final ByteBuffer src) throws IOException {
final int n = src.remaining();
contentLength.addAndGet(n);
stats.incTotalContentLength(n);
if (config.getVerbosity() >= 6) {
final CharsetDecoder decoder = (charset != null ? charset : StandardCharsets.US_ASCII).newDecoder();
System.out.print(decoder.decode(src));
}
}
@Override
public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
if (status == HttpStatus.SC_OK) {
stats.incSuccessCount();
} else {
stats.incFailureCount();
}
stats.setContentLength(contentLength.get());
final FutureCallback<Void> resultCallback = resultCallbackRef.getAndSet(null);
if (resultCallback != null) {
resultCallback.completed(null);
}
if (config.getVerbosity() >= 6) {
System.out.println();
System.out.println();
}
}
@Override
public void failed(final Exception cause) {
stats.incFailureCount();
final FutureCallback<Void> resultCallback = resultCallbackRef.getAndSet(null);
if (resultCallback != null) {
resultCallback.failed(cause);
}
if (config.getVerbosity() >= 1) {
System.out.println("HTTP response error: " + cause.getMessage());
}
}
@Override
public void releaseResources() {
}
};
}