Avoid package dependency cycles

This commit is contained in:
Juergen Hoeller
2016-09-20 22:41:11 +02:00
parent 65e01eabf0
commit d94ce0a1b2
6 changed files with 76 additions and 51 deletions

View File

@@ -28,6 +28,8 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
@@ -134,7 +136,7 @@ public class ResourceRegionEncoder extends AbstractEncoder<ResourceRegion> {
private byte[] getContentRangeHeader(ResourceRegion region) {
long start = region.getPosition();
long end = start + region.getCount() - 1;
OptionalLong contentLength = ResourceUtils.contentLength(region.getResource());
OptionalLong contentLength = contentLength(region.getResource());
if (contentLength.isPresent()) {
return getAsciiBytes("Content-Range: bytes " + start + "-" + end + "/" + contentLength.getAsLong() + "\r\n\r\n");
}
@@ -143,4 +145,22 @@ public class ResourceRegionEncoder extends AbstractEncoder<ResourceRegion> {
}
}
/**
* Determine, if possible, the contentLength of the given resource without reading it.
* @param resource the resource instance
* @return the contentLength of the resource
*/
private OptionalLong contentLength(Resource resource) {
// Don't try to determine contentLength on InputStreamResource - cannot be read afterwards...
// Note: custom InputStreamResource subclasses could provide a pre-calculated content length!
if (InputStreamResource.class != resource.getClass()) {
try {
return OptionalLong.of(resource.contentLength());
}
catch (IOException ignored) {
}
}
return OptionalLong.empty();
}
}

View File

@@ -18,16 +18,11 @@ package org.springframework.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.OptionalLong;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
/**
* Utility methods for resolving resource locations to files in the
@@ -389,23 +384,4 @@ public abstract class ResourceUtils {
con.setUseCaches(con.getClass().getSimpleName().startsWith("JNLP"));
}
/**
* Determine, if possible, the contentLength of the given resource
* without reading it.
* @param resource the resource instance
* @return the contentLength of the resource
*/
public static OptionalLong contentLength(Resource resource) {
// Don't try to determine contentLength on InputStreamResource - cannot be read afterwards...
// Note: custom InputStreamResource subclasses could provide a pre-calculated content length!
if (InputStreamResource.class != resource.getClass()) {
try {
return OptionalLong.of(resource.contentLength());
}
catch (IOException ignored) {
}
}
return OptionalLong.empty();
}
}