Java Code Examples for org.springframework.core.io.support.ResourceRegion#getPosition()

The following examples show how to use org.springframework.core.io.support.ResourceRegion#getPosition() . 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: ResourceRegionHttpMessageConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
	Assert.notNull(region, "ResourceRegion must not be null");
	HttpHeaders responseHeaders = outputMessage.getHeaders();

	long start = region.getPosition();
	long end = start + region.getCount() - 1;
	Long resourceLength = region.getResource().contentLength();
	end = Math.min(end, resourceLength - 1);
	long rangeLength = end - start + 1;
	responseHeaders.add("Content-Range", "bytes " + start + '-' + end + '/' + resourceLength);
	responseHeaders.setContentLength(rangeLength);

	InputStream in = region.getResource().getInputStream();
	try {
		StreamUtils.copyRange(in, outputMessage.getBody(), start, end);
	}
	finally {
		try {
			in.close();
		}
		catch (IOException ex) {
			// ignore
		}
	}
}
 
Example 2
Source File: ResourceHttpMessageWriter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private static Optional<Mono<Void>> zeroCopy(Resource resource, @Nullable ResourceRegion region,
		ReactiveHttpOutputMessage message, Map<String, Object> hints) {

	if (message instanceof ZeroCopyHttpOutputMessage && resource.isFile()) {
		try {
			File file = resource.getFile();
			long pos = region != null ? region.getPosition() : 0;
			long count = region != null ? region.getCount() : file.length();
			if (logger.isDebugEnabled()) {
				String formatted = region != null ? "region " + pos + "-" + (count) + " of " : "";
				logger.debug(Hints.getLogPrefix(hints) + "Zero-copy " + formatted + "[" + resource + "]");
			}
			return Optional.of(((ZeroCopyHttpOutputMessage) message).writeWith(file, pos, count));
		}
		catch (IOException ex) {
			// should not happen
		}
	}
	return Optional.empty();
}
 
Example 3
Source File: ResourceRegionHttpMessageConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
	Assert.notNull(region, "ResourceRegion must not be null");
	HttpHeaders responseHeaders = outputMessage.getHeaders();

	long start = region.getPosition();
	long end = start + region.getCount() - 1;
	Long resourceLength = region.getResource().contentLength();
	end = Math.min(end, resourceLength - 1);
	long rangeLength = end - start + 1;
	responseHeaders.add("Content-Range", "bytes " + start + '-' + end + '/' + resourceLength);
	responseHeaders.setContentLength(rangeLength);

	InputStream in = region.getResource().getInputStream();
	try {
		StreamUtils.copyRange(in, outputMessage.getBody(), start, end);
	}
	finally {
		try {
			in.close();
		}
		catch (IOException ex) {
			// ignore
		}
	}
}
 
Example 4
Source File: ResourceHttpMessageWriter.java    From java-technology-stack with MIT License 6 votes vote down vote up
private static Optional<Mono<Void>> zeroCopy(Resource resource, @Nullable ResourceRegion region,
		ReactiveHttpOutputMessage message, Map<String, Object> hints) {

	if (message instanceof ZeroCopyHttpOutputMessage && resource.isFile()) {
		try {
			File file = resource.getFile();
			long pos = region != null ? region.getPosition() : 0;
			long count = region != null ? region.getCount() : file.length();
			if (logger.isDebugEnabled()) {
				String formatted = region != null ? "region " + pos + "-" + (count) + " of " : "";
				logger.debug(Hints.getLogPrefix(hints) + "Zero-copy " + formatted + "[" + resource + "]");
			}
			return Optional.of(((ZeroCopyHttpOutputMessage) message).writeWith(file, pos, count));
		}
		catch (IOException ex) {
			// should not happen
		}
	}
	return Optional.empty();
}
 
Example 5
Source File: ResourceRegionHttpMessageConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
	Assert.notNull(region, "ResourceRegion must not be null");
	HttpHeaders responseHeaders = outputMessage.getHeaders();

	long start = region.getPosition();
	long end = start + region.getCount() - 1;
	Long resourceLength = region.getResource().contentLength();
	end = Math.min(end, resourceLength - 1);
	long rangeLength = end - start + 1;
	responseHeaders.add("Content-Range", "bytes " + start + '-' + end + '/' + resourceLength);
	responseHeaders.setContentLength(rangeLength);

	InputStream in = region.getResource().getInputStream();
	try {
		StreamUtils.copyRange(in, outputMessage.getBody(), start, end);
	}
	finally {
		try {
			in.close();
		}
		catch (IOException ex) {
			// ignore
		}
	}
}
 
Example 6
Source File: RangeAwareResourceRegionHttpMessageConverter.java    From engine with GNU General Public License v3.0 6 votes vote down vote up
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
    Assert.notNull(region, "ResourceRegion must not be null");
    HttpHeaders responseHeaders = outputMessage.getHeaders();

    long start = region.getPosition();
    long end = start + region.getCount() - 1;
    Long resourceLength = region.getResource().contentLength();
    end = Math.min(end, resourceLength - 1);
    long rangeLength = end - start + 1;
    responseHeaders.add("Content-Range", "bytes " + start + '-' + end + '/' + resourceLength);
    responseHeaders.setContentLength(rangeLength);

    InputStream in = null;
    try {
        Resource resource = region.getResource();
        if (resource instanceof RangeAwareResource) {
            in = ((RangeAwareResource) resource).getInputStream(start, end);
            StreamUtils.copy(in, outputMessage.getBody());
        } else {
            in = resource.getInputStream();
            StreamUtils.copyRange(in, outputMessage.getBody(), start, end);
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}
 
Example 7
Source File: ResourceRegionEncoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Flux<DataBuffer> writeResourceRegion(
		ResourceRegion region, DataBufferFactory bufferFactory, @Nullable Map<String, Object> hints) {

	Resource resource = region.getResource();
	long position = region.getPosition();
	long count = region.getCount();

	if (logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) {
		logger.debug(Hints.getLogPrefix(hints) +
				"Writing region " + position + "-" + (position + count) + " of [" + resource + "]");
	}

	Flux<DataBuffer> in = DataBufferUtils.read(resource, position, bufferFactory, this.bufferSize);
	return DataBufferUtils.takeUntilByteCount(in, count);
}
 
Example 8
Source File: ResourceRegionEncoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 toAsciiBytes("Content-Range: bytes " + start + '-' + end + '/' + length + "\r\n\r\n");
	}
	else {
		return toAsciiBytes("Content-Range: bytes " + start + '-' + end + "\r\n\r\n");
	}
}
 
Example 9
Source File: ResourceRegionEncoder.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Flux<DataBuffer> writeResourceRegion(
		ResourceRegion region, DataBufferFactory bufferFactory, @Nullable Map<String, Object> hints) {

	Resource resource = region.getResource();
	long position = region.getPosition();
	long count = region.getCount();

	if (logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) {
		logger.debug(Hints.getLogPrefix(hints) +
				"Writing region " + position + "-" + (position + count) + " of [" + resource + "]");
	}

	Flux<DataBuffer> in = DataBufferUtils.read(resource, position, bufferFactory, this.bufferSize);
	return DataBufferUtils.takeUntilByteCount(in, count);
}
 
Example 10
Source File: ResourceRegionEncoder.java    From java-technology-stack with MIT License 5 votes vote down vote up
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");
	}
}
 
Example 11
Source File: ResourceRegionHttpMessageConverter.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRegions,
		HttpOutputMessage outputMessage) throws IOException {

	Assert.notNull(resourceRegions, "Collection of ResourceRegion should not be null");
	HttpHeaders responseHeaders = outputMessage.getHeaders();

	MediaType contentType = responseHeaders.getContentType();
	String boundaryString = MimeTypeUtils.generateMultipartBoundaryString();
	responseHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/byteranges; boundary=" + boundaryString);
	OutputStream out = outputMessage.getBody();

	for (ResourceRegion region : resourceRegions) {
		long start = region.getPosition();
		long end = start + region.getCount() - 1;
		InputStream in = region.getResource().getInputStream();
		try {
			// Writing MIME header.
			println(out);
			print(out, "--" + boundaryString);
			println(out);
			if (contentType != null) {
				print(out, "Content-Type: " + contentType.toString());
				println(out);
			}
			Long resourceLength = region.getResource().contentLength();
			end = Math.min(end, resourceLength - 1);
			print(out, "Content-Range: bytes " + start + '-' + end + '/' + resourceLength);
			println(out);
			println(out);
			// Printing content
			StreamUtils.copyRange(in, out, start, end);
		}
		finally {
			try {
				in.close();
			}
			catch (IOException ex) {
				// ignore
			}
		}
	}

	println(out);
	print(out, "--" + boundaryString + "--");
}
 
Example 12
Source File: ResourceRegionHttpMessageConverter.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRegions,
		HttpOutputMessage outputMessage) throws IOException {

	Assert.notNull(resourceRegions, "Collection of ResourceRegion should not be null");
	HttpHeaders responseHeaders = outputMessage.getHeaders();

	MediaType contentType = responseHeaders.getContentType();
	String boundaryString = MimeTypeUtils.generateMultipartBoundaryString();
	responseHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/byteranges; boundary=" + boundaryString);
	OutputStream out = outputMessage.getBody();

	for (ResourceRegion region : resourceRegions) {
		long start = region.getPosition();
		long end = start + region.getCount() - 1;
		InputStream in = region.getResource().getInputStream();
		try {
			// Writing MIME header.
			println(out);
			print(out, "--" + boundaryString);
			println(out);
			if (contentType != null) {
				print(out, "Content-Type: " + contentType.toString());
				println(out);
			}
			Long resourceLength = region.getResource().contentLength();
			end = Math.min(end, resourceLength - 1);
			print(out, "Content-Range: bytes " + start + '-' + end + '/' + resourceLength);
			println(out);
			println(out);
			// Printing content
			StreamUtils.copyRange(in, out, start, end);
		}
		finally {
			try {
				in.close();
			}
			catch (IOException ex) {
				// ignore
			}
		}
	}

	println(out);
	print(out, "--" + boundaryString + "--");
}
 
Example 13
Source File: ResourceRegionHttpMessageConverter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRegions,
		HttpOutputMessage outputMessage) throws IOException {

	Assert.notNull(resourceRegions, "Collection of ResourceRegion should not be null");
	HttpHeaders responseHeaders = outputMessage.getHeaders();

	MediaType contentType = responseHeaders.getContentType();
	String boundaryString = MimeTypeUtils.generateMultipartBoundaryString();
	responseHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/byteranges; boundary=" + boundaryString);
	OutputStream out = outputMessage.getBody();

	for (ResourceRegion region : resourceRegions) {
		long start = region.getPosition();
		long end = start + region.getCount() - 1;
		InputStream in = region.getResource().getInputStream();
		try {
			// Writing MIME header.
			println(out);
			print(out, "--" + boundaryString);
			println(out);
			if (contentType != null) {
				print(out, "Content-Type: " + contentType.toString());
				println(out);
			}
			Long resourceLength = region.getResource().contentLength();
			end = Math.min(end, resourceLength - 1);
			print(out, "Content-Range: bytes " + start + '-' + end + '/' + resourceLength);
			println(out);
			println(out);
			// Printing content
			StreamUtils.copyRange(in, out, start, end);
		}
		finally {
			try {
				in.close();
			}
			catch (IOException ex) {
				// ignore
			}
		}
	}

	println(out);
	print(out, "--" + boundaryString + "--");
}
 
Example 14
Source File: RangeAwareResourceRegionHttpMessageConverter.java    From engine with GNU General Public License v3.0 4 votes vote down vote up
protected void writeResourceRegionCollection(Collection<ResourceRegion> resourceRegions,
                                             HttpOutputMessage outputMessage) throws IOException {
    Assert.notNull(resourceRegions, "Collection of ResourceRegion should not be null");
    HttpHeaders responseHeaders = outputMessage.getHeaders();

    MediaType contentType = responseHeaders.getContentType();
    String boundaryString = MimeTypeUtils.generateMultipartBoundaryString();
    responseHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/byteranges; boundary=" + boundaryString);
    OutputStream out = outputMessage.getBody();

    for (ResourceRegion region : resourceRegions) {
        long start = region.getPosition();
        long end = start + region.getCount() - 1;
        InputStream in = null;
        try {
            // Writing MIME header.
            println(out);
            print(out, "--" + boundaryString);
            println(out);
            if (contentType != null) {
                print(out, "Content-Type: " + contentType.toString());
                println(out);
            }
            Long resourceLength = region.getResource().contentLength();
            end = Math.min(end, resourceLength - 1);
            print(out, "Content-Range: bytes " + start + '-' + end + '/' + resourceLength);
            println(out);
            println(out);

            // Printing content
            Resource resource = region.getResource();
            if (resource instanceof RangeAwareResource) {
                in = ((RangeAwareResource) resource).getInputStream(start, end);
                StreamUtils.copy(in, out);
            } else {
                in = resource.getInputStream();
                StreamUtils.copyRange(in, out, start, end);
            }
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    println(out);
    print(out, "--" + boundaryString + "--");
}