Java Code Examples for org.springframework.core.io.buffer.DataBufferFactory#wrap()
The following examples show how to use
org.springframework.core.io.buffer.DataBufferFactory#wrap() .
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: HttpBinCompatibleController.java From spring-cloud-gateway with Apache License 2.0 | 6 votes |
@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)); }
Example 2
Source File: PayloadUtils.java From spring-analysis-note with MIT License | 5 votes |
/** * 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(); } } }
Example 3
Source File: ByteArrayEncoder.java From spring-analysis-note with MIT License | 5 votes |
@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; }
Example 4
Source File: ByteBufferEncoder.java From spring-analysis-note with MIT License | 5 votes |
@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; }
Example 5
Source File: WebfluxResponseUtil.java From microservices-platform with Apache License 2.0 | 5 votes |
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); }); }
Example 6
Source File: SnowdropAmqpMessageBuilderTest.java From vertx-spring-boot with Apache License 2.0 | 5 votes |
@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); }
Example 7
Source File: ResourceRegionEncoder.java From spring-analysis-note with MIT License | 4 votes |
private DataBuffer getRegionSuffix(DataBufferFactory bufferFactory, String boundaryString) { byte[] endBoundary = toAsciiBytes("\r\n--" + boundaryString + "--"); return bufferFactory.wrap(endBoundary); }
Example 8
Source File: ServerSentEventHttpMessageWriter.java From spring-analysis-note with MIT License | 4 votes |
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 }