/** * Run a provided action when a consumer cancels this ByteStream. * * @param action the action runnable * @return a new ByteStream with the action attached */ public ByteStream doOnCancel(Runnable action) { return new ByteStream(Flux.from(this.stream).doOnCancel(action)); }
/** * Creates a new {@code ByteStream} from byte array. * * @param content content * @return ByteStream */ public static ByteStream from(byte[] content) { return new ByteStream(Flux.just(new Buffer(Unpooled.copiedBuffer(content)))); }
/** * Creates a new {@code ByteStream} from String. * * @param content content * @param charset Character set encoding * @return ByteStream */ public static ByteStream from(String content, Charset charset) { return new ByteStream(Flux.just(new Buffer(content, charset))); }
@Test public void contentAggregationOverflow() throws ExecutionException, InterruptedException { ByteStream stream = new ByteStream(Flux.just(buf1, buf2, buf3)); Throwable cause = stream.aggregate(2) .handle((result, throwable) -> throwable) .get(); assertThat(cause, instanceOf(ContentOverflowException.class)); }
private static LiveHttpResponse toStyxResponse(io.netty.handler.codec.http.HttpResponse nettyResponse, Observable<ByteBuf> contentObservable, Origin origin) { try { return toStyxResponse(nettyResponse) .body(new ByteStream(toPublisher(contentObservable.map(Buffers::fromByteBuf)))) .build(); } catch (IllegalArgumentException e) { throw new BadHttpResponseException(origin, e); } }
/** * Creates a new {@link Builder} object with default attributes. */ public Builder() { this.url = Url.Builder.url("/").build(); this.headers = new HttpHeaders.Builder(); this.body = new ByteStream(Flux.empty()); }
private static ByteStream body(String... contents) { return new ByteStream(Flux.fromIterable( Stream.of(contents) .map(content -> new Buffer(copiedBuffer(content, UTF_8))) .collect(toList()))); }
private static ByteStream body(String... contents) { return new ByteStream( Flux.fromIterable( Stream.of(contents) .map(content -> new Buffer(content, UTF_8)) .collect(toList()))); }
@Test public void runsOnCancelActionWhenCancelled() { AtomicBoolean cancelled = new AtomicBoolean(); ByteStream stream = new ByteStream(Flux.just(buf1, buf2)) .doOnCancel(() -> cancelled.set(true)); StepVerifier.create(stream) .thenRequest(1) .expectNext(buf1) .then(() -> assertThat(cancelled.get(), is(false))) .thenCancel() .verify(); assertThat(cancelled.get(), is(true)); }
@Test public void concatenatesEmptyStreams() { ByteStream stream = new ByteStream(Flux.empty()) .concat(new ByteStream(Flux.just(buf3))) .concat(new ByteStream(Flux.empty())); StepVerifier.create(stream) .expectNext(buf3) .verifyComplete(); }
private static LiveHttpResponse error(HttpResponseStatus status) { return new LiveHttpResponse.Builder(status) .body(new ByteStream(Flux.just(new Buffer(status.description(), UTF_8)))) .build(); }
Builder(HttpRequest request) { this.id = request.id(); this.method = request.method(); this.url = request.url(); this.version = request.version(); this.headers = request.headers().newBuilder(); this.body = new ByteStream(Flux.just(new Buffer(copiedBuffer(request.body())))); }
@Test public void mapRetainsRefcountsForInlineBufferChanges() { ByteStream stream = new ByteStream(Flux.just(buf1, buf2, buf3)); ByteStream mapped = stream.map(buf -> buf); StepVerifier.create(Flux.from(mapped).map(this::decodeUtf8String)) .expectSubscription() .expectNextCount(3) .verifyComplete(); assertThat(buf1.delegate().refCnt(), is(1)); assertThat(buf2.delegate().refCnt(), is(1)); assertThat(buf3.delegate().refCnt(), is(1)); }
@Test public void concatenatesStreams() { ByteStream stream = new ByteStream(Flux.just(buf1, buf2)) .concat(new ByteStream(Flux.just(buf3))); StepVerifier.create(stream) .expectNext(buf1) .expectNext(buf2) .expectNext(buf3) .verifyComplete(); }
@Test public void releasesRefcountForMappedBuffers() { ByteStream stream = new ByteStream(Flux.just(buf1, buf2, buf3)); ByteStream mapped = stream.map(this::toUpperCase); StepVerifier.create(Flux.from(mapped).map(this::decodeUtf8String)) .expectSubscription() .expectNext("A", "B", "C") .verifyComplete(); assertThat(buf1.delegate().refCnt(), is(0)); assertThat(buf2.delegate().refCnt(), is(0)); assertThat(buf3.delegate().refCnt(), is(0)); }
@DataProvider(name = "emptyBodyRequests") private Object[][] emptyBodyRequests() { return new Object[][]{ {get("/foo/bar").build()}, {post("/foo/bar", new ByteStream(Flux.empty())).build()}, }; }
@DataProvider(name = "emptyBodyResponses") private Object[][] emptyBodyResponses() { return new Object[][]{ {response().build()}, {response().body(new ByteStream(Flux.empty())).build()}, }; }
@Test public void shouldSetsContentLengthForNonStreamingBodyMessage() { assertThat(put("/home").body(new ByteStream(Flux.just(new Buffer("Hello", UTF_8)))).build().header(CONTENT_LENGTH), isAbsent()); }
@Test public void toFullResponseReleasesOriginalRefCountedBuffers() throws ExecutionException, InterruptedException { Buffer content = new Buffer(Unpooled.copiedBuffer("original", UTF_8)); LiveHttpResponse original = LiveHttpResponse.response(OK) .body(new ByteStream(Flux.just(content))) .build(); StepVerifier.create(original.aggregate(100)) .expectNextCount(1) .then(() -> assertThat(content.delegate().refCnt(), is(0))) .verifyComplete(); }
@Test public void replacesStream() { ByteStream stream = new ByteStream(Flux.just(buf1, buf2)) .replaceWith(new ByteStream(Flux.just(buf3))); StepVerifier.create(stream) .expectNext(buf3) .then(() -> { assertEquals(buf1.delegate().refCnt(), 0); assertEquals(buf2.delegate().refCnt(), 0); }) .verifyComplete(); }