Java Code Examples for org.springframework.core.io.buffer.DataBuffer#asOutputStream()

The following examples show how to use org.springframework.core.io.buffer.DataBuffer#asOutputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProtobufEncoder.java    From java-technology-stack with MIT License 6 votes vote down vote up
private DataBuffer encodeMessage(Message message, DataBufferFactory bufferFactory, boolean streaming) {
	DataBuffer buffer = bufferFactory.allocateBuffer();
	OutputStream outputStream = buffer.asOutputStream();
	try {
		if (streaming) {
			message.writeDelimitedTo(outputStream);
		}
		else {
			message.writeTo(outputStream);
		}
		return buffer;
	}
	catch (IOException ex) {
		throw new IllegalStateException("Unexpected I/O error while writing to data buffer", ex);
	}
}
 
Example 2
Source File: MustacheView.java    From spring-reactive-sample with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Mono<Void> renderInternal(Map<String, Object> model, MediaType contentType,
                                    ServerWebExchange exchange) {
    Resource resource = resolveResource();
    if (resource == null) {
        return Mono.error(new IllegalStateException(
                "Could not find Mustache template with URL [" + getUrl() + "]"));
    }
    DataBuffer dataBuffer = exchange.getResponse().bufferFactory().allocateBuffer();
    try (Reader reader = getReader(resource)) {
        Template template = this.compiler.compile(reader);
        Charset charset = getCharset(contentType).orElse(getDefaultCharset());
        try (Writer writer = new OutputStreamWriter(dataBuffer.asOutputStream(),
                charset)) {
            template.execute(model, writer);
            writer.flush();
        }
    }
    catch (Exception ex) {
        DataBufferUtils.release(dataBuffer);
        return Mono.error(ex);
    }
    return exchange.getResponse().writeWith(Flux.just(dataBuffer));
}
 
Example 3
Source File: HessianCodecSupport.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
public DataBuffer encode(Object obj, DataBufferFactory bufferFactory) throws Exception {
    DataBuffer dataBuffer = bufferFactory.allocateBuffer();
    HessianSerializerOutput output = new HessianSerializerOutput(dataBuffer.asOutputStream());
    output.writeObject(obj);
    output.flush();
    return dataBuffer;
}