ResourceRegion fits better in core.io.support (next to EncodedResource)

Issue: SPR-14221
This commit is contained in:
Juergen Hoeller 2016-05-06 12:02:51 +02:00
parent e5dbe12e85
commit 42d32ba396
7 changed files with 59 additions and 47 deletions

View File

@ -14,15 +14,17 @@
* limitations under the License.
*/
package org.springframework.core.io;
package org.springframework.core.io.support;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
/**
* Region of a {@link Resource} implementation, materialized by a {@code position}
* within the {@link Resource} and a byte {@code count} for the length of that region.
*
* @author Arjen Poutsma
* @since 4.3.0
* @since 4.3
*/
public class ResourceRegion {
@ -32,16 +34,17 @@ public class ResourceRegion {
private final long count;
/**
* Create a new {@code ResourceRegion} from a given {@link Resource}.
* This region of a resource is reprensented by a start {@code position}
* This region of a resource is represented by a start {@code position}
* and a byte {@code count} within the given {@code Resource}.
* @param resource a Resource
* @param position the start position of the region in that resource
* @param count the byte count of the region in that resource
*/
public ResourceRegion(Resource resource, long position, long count) {
Assert.notNull(resource, "'resource' must not be null");
Assert.notNull(resource, "Resource must not be null");
Assert.isTrue(position >= 0, "'position' must be larger than or equal to 0");
Assert.isTrue(count >= 0, "'count' must be larger than or equal to 0");
this.resource = resource;
@ -49,6 +52,7 @@ public class ResourceRegion {
this.count = count;
}
/**
* Return the underlying {@link Resource} for this {@code ResourceRegion}
*/
@ -69,4 +73,5 @@ public class ResourceRegion {
public long getCount() {
return this.count;
}
}
}

View File

@ -14,12 +14,14 @@
* limitations under the License.
*/
package org.springframework.core.io;
import static org.mockito.Mockito.mock;
package org.springframework.core.io.support;
import org.junit.Test;
import org.springframework.core.io.Resource;
import static org.mockito.Mockito.*;
/**
* Unit tests for the {@link ResourceRegion} class.
*
@ -41,4 +43,5 @@ public class ResourceRegionTests {
public void shouldThrowExceptionForNegativeCount() {
new ResourceRegion(mock(Resource.class), 0, -1);
}
}

View File

@ -25,7 +25,7 @@ import java.util.List;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceRegion;
import org.springframework.core.io.support.ResourceRegion;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@ -64,7 +64,7 @@ public abstract class HttpRange {
* information contained in the current {@code HttpRange}.
* @param resource the {@code Resource} to select the region from
* @return the selected region of the given {@code Resource}
* @since 4.3.0
* @since 4.3
*/
public ResourceRegion toResourceRegion(Resource resource) {
// Don't try to determine contentLength on InputStreamResource - cannot be read afterwards...

View File

@ -23,7 +23,7 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import org.springframework.core.io.ResourceRegion;
import org.springframework.core.io.support.ResourceRegion;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
@ -33,11 +33,11 @@ import org.springframework.util.MimeTypeUtils;
import org.springframework.util.StreamUtils;
/**
* Implementation of {@link HttpMessageConverter} that can write a single {@link ResourceRegion ResourceRegion},
* Implementation of {@link HttpMessageConverter} that can write a single {@link ResourceRegion},
* or Collections of {@link ResourceRegion ResourceRegions}.
*
* @author Brian Clozel
* @since 4.3.0
* @since 4.3
*/
public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessageConverter<Object> {
@ -45,6 +45,32 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
super(MediaType.ALL);
}
@Override
protected boolean supports(Class<?> clazz) {
// should not be called as we override canRead/canWrite
return false;
}
@Override
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
return false;
}
@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
return null;
}
@Override
protected ResourceRegion readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
return null;
}
@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return canWrite(clazz, null, mediaType);
@ -152,6 +178,7 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
print(out, "--" + boundaryString + "--");
}
private static void println(OutputStream os) throws IOException {
os.write('\r');
os.write('\n');
@ -161,28 +188,4 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
os.write(buf.getBytes("US-ASCII"));
}
@Override
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
return false;
}
@Override
protected boolean supports(Class<?> clazz) {
// should not be called as we override canRead/canWrite
return false;
}
@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
return null;
}
@Override
protected ResourceRegion readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
return null;
}
}

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http;
import java.io.IOException;
@ -22,13 +23,13 @@ import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.ResourceRegion;
import org.springframework.core.io.support.ResourceRegion;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import static org.mockito.Mockito.mock;
/**
* Unit tests for {@link HttpRange}.

View File

@ -16,9 +16,6 @@
package org.springframework.http.converter;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.ArrayList;
@ -30,13 +27,16 @@ import org.junit.Test;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceRegion;
import org.springframework.core.io.support.ResourceRegion;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRange;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.util.StringUtils;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* Test cases for {@link ResourceRegionHttpMessageConverter} class.
*

View File

@ -30,7 +30,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceRegion;
import org.springframework.core.io.support.ResourceRegion;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRange;