private Mono<Void> encodeAndWriteRegions(Publisher<? extends ResourceRegion> publisher, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) { Flux<DataBuffer> body = this.regionEncoder.encode( publisher, message.bufferFactory(), REGION_TYPE, mediaType, hints); return message.writeWith(body); }
public ResourceHttpMessageWriter(int bufferSize) { this.encoder = new ResourceEncoder(bufferSize); this.regionEncoder = new ResourceRegionEncoder(bufferSize); this.mediaTypes = MediaType.asMediaTypes(this.encoder.getEncodableMimeTypes()); }
private byte[] getContentRangeHeader(ResourceRegion region) { long start = region.getPosition(); long end = start + region.getCount() - 1; OptionalLong contentLength = contentLength(region.getResource()); if (contentLength.isPresent()) { long length = contentLength.getAsLong(); return getAsciiBytes("Content-Range: bytes " + start + '-' + end + '/' + length + "\r\n\r\n"); } else { return getAsciiBytes("Content-Range: bytes " + start + '-' + end + "\r\n\r\n"); } }
return writeResourceRegion(region, bufferFactory, hints); }); byte[] startBoundary = getAsciiBytes("\r\n--" + boundaryString + "\r\n"); byte[] contentType = (mimeType != null ? getAsciiBytes("Content-Type: " + mimeType + "\r\n") : new byte[0]); getRegionPrefix(bufferFactory, startBoundary, contentType, region), writeResourceRegion(region, bufferFactory, hints)); .concatWith(getRegionSuffix(bufferFactory, boundaryString));
private Flux<DataBuffer> getRegionSuffix(DataBufferFactory bufferFactory, String boundaryString) { byte[] endBoundary = getAsciiBytes("\r\n--" + boundaryString + "--"); return Flux.defer(() -> Flux.just( bufferFactory.allocateBuffer(endBoundary.length).write(endBoundary))); }
private Flux<DataBuffer> getRegionPrefix(DataBufferFactory bufferFactory, byte[] startBoundary, byte[] contentType, ResourceRegion region) { return Flux.defer(() -> Flux.just( bufferFactory.allocateBuffer(startBoundary.length).write(startBoundary), bufferFactory.allocateBuffer(contentType.length).write(contentType), bufferFactory.wrap(ByteBuffer.wrap(getContentRangeHeader(region)))) ); }
return writeResourceRegion(region, bufferFactory, hints); }); byte[] startBoundary = getAsciiBytes("\r\n--" + boundaryString + "\r\n"); byte[] contentType = (mimeType != null ? getAsciiBytes("Content-Type: " + mimeType + "\r\n") : new byte[0]); getRegionPrefix(bufferFactory, startBoundary, contentType, region), writeResourceRegion(region, bufferFactory, hints)); .concatWith(getRegionSuffix(bufferFactory, boundaryString));
private Flux<DataBuffer> getRegionSuffix(DataBufferFactory bufferFactory, String boundaryString) { byte[] endBoundary = getAsciiBytes("\r\n--" + boundaryString + "--"); return Flux.defer(() -> Flux.just( bufferFactory.allocateBuffer(endBoundary.length).write(endBoundary))); }
private Flux<DataBuffer> getRegionPrefix(DataBufferFactory bufferFactory, byte[] startBoundary, byte[] contentType, ResourceRegion region) { return Flux.defer(() -> Flux.just( bufferFactory.allocateBuffer(startBoundary.length).write(startBoundary), bufferFactory.allocateBuffer(contentType.length).write(contentType), bufferFactory.wrap(ByteBuffer.wrap(getContentRangeHeader(region)))) ); }
return writeResourceRegion(region, bufferFactory, hints); }); byte[] startBoundary = getAsciiBytes("\r\n--" + boundaryString + "\r\n"); byte[] contentType = (mimeType != null ? getAsciiBytes("Content-Type: " + mimeType + "\r\n") : new byte[0]); getRegionPrefix(bufferFactory, startBoundary, contentType, region), writeResourceRegion(region, bufferFactory, hints)); .concatWith(getRegionSuffix(bufferFactory, boundaryString));
private byte[] getContentRangeHeader(ResourceRegion region) { long start = region.getPosition(); long end = start + region.getCount() - 1; OptionalLong contentLength = contentLength(region.getResource()); if (contentLength.isPresent()) { long length = contentLength.getAsLong(); return getAsciiBytes("Content-Range: bytes " + start + '-' + end + '/' + length + "\r\n\r\n"); } else { return getAsciiBytes("Content-Range: bytes " + start + '-' + end + "\r\n\r\n"); } }
private Mono<Void> encodeAndWriteRegions(Publisher<? extends ResourceRegion> publisher, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) { Flux<DataBuffer> body = this.regionEncoder.encode( publisher, message.bufferFactory(), REGION_TYPE, mediaType, hints); return message.writeWith(body); }
private Flux<DataBuffer> getRegionSuffix(DataBufferFactory bufferFactory, String boundaryString) { byte[] endBoundary = getAsciiBytes("\r\n--" + boundaryString + "--"); return Flux.defer(() -> Flux.just( bufferFactory.allocateBuffer(endBoundary.length).write(endBoundary))); }
private Flux<DataBuffer> getRegionPrefix(DataBufferFactory bufferFactory, byte[] startBoundary, byte[] contentType, ResourceRegion region) { return Flux.defer(() -> Flux.just( bufferFactory.allocateBuffer(startBoundary.length).write(startBoundary), bufferFactory.allocateBuffer(contentType.length).write(contentType), bufferFactory.wrap(ByteBuffer.wrap(getContentRangeHeader(region)))) ); }
public ResourceHttpMessageWriter(int bufferSize) { this.encoder = new ResourceEncoder(bufferSize); this.regionEncoder = new ResourceRegionEncoder(bufferSize); this.mediaTypes = MediaType.asMediaTypes(this.encoder.getEncodableMimeTypes()); }
return writeResourceRegion(region, bufferFactory, hints); }); byte[] startBoundary = getAsciiBytes("\r\n--" + boundaryString + "\r\n"); byte[] contentType = (mimeType != null ? getAsciiBytes("Content-Type: " + mimeType + "\r\n") : new byte[0]); getRegionPrefix(bufferFactory, startBoundary, contentType, region), writeResourceRegion(region, bufferFactory, hints)); .concatWith(getRegionSuffix(bufferFactory, boundaryString));
private byte[] getContentRangeHeader(ResourceRegion region) { long start = region.getPosition(); long end = start + region.getCount() - 1; OptionalLong contentLength = contentLength(region.getResource()); if (contentLength.isPresent()) { long length = contentLength.getAsLong(); return getAsciiBytes("Content-Range: bytes " + start + '-' + end + '/' + length + "\r\n\r\n"); } else { return getAsciiBytes("Content-Range: bytes " + start + '-' + end + "\r\n\r\n"); } }
@Test public void shouldEncodeResourceRegionFileResource() throws Exception { ResourceRegion region = new ResourceRegion( new ClassPathResource("ResourceRegionEncoderTests.txt", getClass()), 0, 6); Flux<DataBuffer> result = this.encoder.encode(Mono.just(region), this.bufferFactory, ResolvableType.forClass(ResourceRegion.class), MimeTypeUtils.APPLICATION_OCTET_STREAM, Collections.emptyMap()); StepVerifier.create(result) .consumeNextWith(stringConsumer("Spring")) .expectComplete() .verify(); }
private Flux<DataBuffer> getRegionSuffix(DataBufferFactory bufferFactory, String boundaryString) { byte[] endBoundary = getAsciiBytes("\r\n--" + boundaryString + "--"); return Flux.defer(() -> Flux.just( bufferFactory.allocateBuffer(endBoundary.length).write(endBoundary))); }
private Flux<DataBuffer> getRegionPrefix(DataBufferFactory bufferFactory, byte[] startBoundary, byte[] contentType, ResourceRegion region) { return Flux.defer(() -> Flux.just( bufferFactory.allocateBuffer(startBoundary.length).write(startBoundary), bufferFactory.allocateBuffer(contentType.length).write(contentType), bufferFactory.wrap(ByteBuffer.wrap(getContentRangeHeader(region)))) ); }