org.springframework.core.io.buffer.DataBufferFactory#wrap ( )源码实例Demo

下面列出了org.springframework.core.io.buffer.DataBufferFactory#wrap ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: spring-analysis-note   文件: PayloadUtils.java
/**
 * Use this method to slice, retain and wrap the data portion of the
 * {@code Payload}, and also to release the {@code Payload}. This assumes
 * the Payload metadata has been read by now and ensures downstream code
 * need only be aware of {@code DataBuffer}s.
 * @param payload the payload to process
 * @param bufferFactory the DataBufferFactory to wrap with
 * @return the created {@code DataBuffer} instance
 */
public static DataBuffer retainDataAndReleasePayload(Payload payload, DataBufferFactory bufferFactory) {
	try {
		if (bufferFactory instanceof NettyDataBufferFactory) {
			ByteBuf byteBuf = payload.sliceData().retain();
			return ((NettyDataBufferFactory) bufferFactory).wrap(byteBuf);
		}
		else {
			return bufferFactory.wrap(payload.getData());
		}
	}
	finally {
		if (payload.refCnt() > 0) {
			payload.release();
		}
	}
}
 
@RequestMapping(path = "/gzip", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<Void> gzip(ServerWebExchange exchange) throws IOException {
	if (log.isDebugEnabled()) {
		log.debug("httpbin /gzip");
	}

	String jsonResponse = OBJECT_MAPPER.writeValueAsString("httpbin compatible home");
	byte[] bytes = jsonResponse.getBytes(StandardCharsets.UTF_8);

	ServerHttpResponse response = exchange.getResponse();
	response.getHeaders().add(HttpHeaders.CONTENT_ENCODING, "gzip");
	DataBufferFactory dataBufferFactory = response.bufferFactory();
	response.setStatusCode(HttpStatus.OK);

	ByteArrayOutputStream bos = new ByteArrayOutputStream();
	GZIPOutputStream is = new GZIPOutputStream(bos);
	FileCopyUtils.copy(bytes, is);

	byte[] gzippedResponse = bos.toByteArray();
	DataBuffer wrap = dataBufferFactory.wrap(gzippedResponse);
	return response.writeWith(Flux.just(wrap));
}
 
源代码3 项目: spring-analysis-note   文件: ByteArrayEncoder.java
@Override
public DataBuffer encodeValue(byte[] bytes, DataBufferFactory bufferFactory,
		ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

	DataBuffer dataBuffer = bufferFactory.wrap(bytes);
	if (logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) {
		String logPrefix = Hints.getLogPrefix(hints);
		logger.debug(logPrefix + "Writing " + dataBuffer.readableByteCount() + " bytes");
	}
	return dataBuffer;
}
 
源代码4 项目: spring-analysis-note   文件: ByteBufferEncoder.java
@Override
public DataBuffer encodeValue(ByteBuffer byteBuffer, DataBufferFactory bufferFactory,
		ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

	DataBuffer dataBuffer = bufferFactory.wrap(byteBuffer);
	if (logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) {
		String logPrefix = Hints.getLogPrefix(hints);
		logger.debug(logPrefix + "Writing " + dataBuffer.readableByteCount() + " bytes");
	}
	return dataBuffer;
}
 
public static Mono<Void> responseWrite(ServerWebExchange exchange, int httpStatus, Result result) {
    if (httpStatus == 0) {
        httpStatus = HttpStatus.INTERNAL_SERVER_ERROR.value();
    }
    ServerHttpResponse response = exchange.getResponse();
    response.getHeaders().setAccessControlAllowCredentials(true);
    response.getHeaders().setAccessControlAllowOrigin("*");
    response.setStatusCode(HttpStatus.valueOf(httpStatus));
    response.getHeaders().setContentType(MediaType.APPLICATION_JSON_UTF8);
    DataBufferFactory dataBufferFactory = response.bufferFactory();
    DataBuffer buffer = dataBufferFactory.wrap(JSONObject.toJSONString(result).getBytes(Charset.defaultCharset()));
    return response.writeWith(Mono.just(buffer)).doOnError((error) -> {
        DataBufferUtils.release(buffer);
    });
}
 
@Test
public void shouldAddDataBufferBody() {
    DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
    DataBuffer originalBuffer = dataBufferFactory.wrap("test".getBytes());
    Buffer expectedBuffer = Buffer.buffer("test".getBytes());

    new SnowdropAmqpMessageBuilder(mockDelegate).withBufferAsBody(originalBuffer);

    verify(mockDelegate).withBufferAsBody(expectedBuffer);
}
 
private DataBuffer getRegionSuffix(DataBufferFactory bufferFactory, String boundaryString) {
	byte[] endBoundary = toAsciiBytes("\r\n--" + boundaryString + "--");
	return bufferFactory.wrap(endBoundary);
}
 
private DataBuffer encodeText(CharSequence text, MediaType mediaType, DataBufferFactory bufferFactory) {
	Assert.notNull(mediaType.getCharset(), "Expected MediaType with charset");
	byte[] bytes = text.toString().getBytes(mediaType.getCharset());
	return bufferFactory.wrap(bytes); // wrapping, not allocating
}