private DataBuffer stringBuffer(String value) { byte[] bytes = value.getBytes(StandardCharsets.UTF_8); DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length); buffer.write(bytes); return buffer; }
@Override public DataBuffer allocateBuffer() { return allocateBufferInternal(this.delegate.allocateBuffer()); }
@Test public void noLeak() { DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(); release(dataBuffer); this.bufferFactory.checkForLeaks(); }
/** * Checks whether any of the data buffers created by {@link #bufferFactory} have not been * released, throwing an assertion error if so. */ @After public final void checkForLeaks() { this.bufferFactory.checkForLeaks(); }
@Override public void decode() { Flux<DataBuffer> input = Flux.just( this.bufferFactory.wrap(this.fooBytes), this.bufferFactory.wrap(this.barBytes)); testDecodeAll(input, DataBuffer.class, step -> step .consumeNextWith(expectDataBuffer(this.fooBytes)) .consumeNextWith(expectDataBuffer(this.barBytes)) .verifyComplete()); }
private DataBuffer stringBuffer(String value) { byte[] bytes = value.getBytes(StandardCharsets.UTF_8); DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length); buffer.write(bytes); return buffer; }
@Test public void leak() { DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(); try { this.bufferFactory.checkForLeaks(); fail("AssertionError expected"); } catch (AssertionError expected) { // ignore } finally { release(dataBuffer); } }
@Override public DataBuffer allocateBuffer(int initialCapacity) { return allocateBufferInternal(this.delegate.allocateBuffer(initialCapacity)); }
private DataBuffer stringBuffer(String value) { byte[] bytes = value.getBytes(StandardCharsets.UTF_8); DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length); buffer.write(bytes); return buffer; }
private DataBuffer stringBuffer(String value) { byte[] bytes = value.getBytes(StandardCharsets.UTF_8); DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length); buffer.write(bytes); return buffer; }
private Mono<DataBuffer> stringBuffer(String value) { return Mono.defer(() -> { byte[] bytes = value.getBytes(StandardCharsets.UTF_8); DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length); buffer.write(bytes); return Mono.just(buffer); }); }
private Flux<DataBuffer> toDataBuffers(String s, int length, Charset charset) { byte[] bytes = s.getBytes(charset); List<DataBuffer> dataBuffers = new ArrayList<>(); for (int i = 0; i < bytes.length; i += length) { DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(length); dataBuffer.write(bytes, i, length); dataBuffers.add(dataBuffer); } return Flux.fromIterable(dataBuffers); }
private Mono<DataBuffer> stringBuffer(String value) { return Mono.defer(() -> { byte[] bytes = value.getBytes(StandardCharsets.UTF_8); DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length); buffer.write(bytes); return Mono.just(buffer); }); }
private Mono<DataBuffer> stringBuffer(String value) { return Mono.defer(() -> { byte[] bytes = value.getBytes(StandardCharsets.UTF_8); DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length); buffer.write(bytes); return Mono.just(buffer); }); }
/** * Creates a deferred {@link DataBuffer} containing the given bytes. * @param bytes the bytes that are to be stored in the buffer * @return the deferred buffer */ protected Mono<DataBuffer> dataBuffer(byte[] bytes) { return Mono.defer(() -> { DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(bytes.length); dataBuffer.write(bytes); return Mono.just(dataBuffer); }); }
private Mono<DataBuffer> dataBuffer(Msg msg) { return Mono.defer(() -> { byte[] bytes = msg.toByteArray(); DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length); buffer.write(bytes); return Mono.just(buffer); }); }
@Test public void decodeChunksToMono() { byte[] full = this.testMsg1.toByteArray(); byte[] chunk1 = Arrays.copyOfRange(full, 0, full.length / 2); byte[] chunk2 = Arrays.copyOfRange(full, chunk1.length, full.length); Flux<DataBuffer> input = Flux.just(chunk1, chunk2) .flatMap(bytes -> Mono.defer(() -> { DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(bytes.length); dataBuffer.write(bytes); return Mono.just(dataBuffer); })); testDecodeToMono(input, Msg.class, step -> step .expectNext(this.testMsg1) .verifyComplete()); }
@Test public void decodeMergedChunks() throws IOException { DataBuffer buffer = this.bufferFactory.allocateBuffer(); this.testMsg1.writeDelimitedTo(buffer.asOutputStream()); this.testMsg1.writeDelimitedTo(buffer.asOutputStream()); ResolvableType elementType = forClass(Msg.class); Flux<Message> messages = this.decoder.decode(Mono.just(buffer), elementType, null, emptyMap()); StepVerifier.create(messages) .expectNext(testMsg1) .expectNext(testMsg1) .verifyComplete(); }
@Override public void encode() throws Exception { Flux<DataBuffer> input = Flux.just(this.fooBytes, this.barBytes) .flatMap(bytes -> Mono.defer(() -> { DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(bytes.length); dataBuffer.write(bytes); return Mono.just(dataBuffer); })); testEncodeAll(input, DataBuffer.class, step -> step .consumeNextWith(expectBytes(this.fooBytes)) .consumeNextWith(expectBytes(this.barBytes)) .verifyComplete()); }
@Override @Test public void decode() { Flux<DataBuffer> input = Flux.just(this.testMsg1, this.testMsg2) .flatMap(msg -> Mono.defer(() -> { DataBuffer buffer = this.bufferFactory.allocateBuffer(); try { msg.writeDelimitedTo(buffer.asOutputStream()); return Mono.just(buffer); } catch (IOException e) { release(buffer); return Mono.error(e); } })); testDecodeAll(input, Msg.class, step -> step .expectNext(this.testMsg1) .expectNext(this.testMsg2) .verifyComplete()); }