Merge branch '6.1.x'

This commit is contained in:
Juergen Hoeller 2024-04-23 13:17:08 +02:00
commit adc7f73170
4 changed files with 95 additions and 69 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -178,18 +178,13 @@ public abstract class StreamUtils {
long bytesToCopy = end - start + 1;
byte[] buffer = new byte[(int) Math.min(StreamUtils.BUFFER_SIZE, bytesToCopy)];
while (bytesToCopy > 0) {
int bytesRead = in.read(buffer);
int bytesRead = (bytesToCopy < buffer.length ? in.read(buffer, 0, (int) bytesToCopy) :
in.read(buffer));
if (bytesRead == -1) {
break;
}
else if (bytesRead <= bytesToCopy) {
out.write(buffer, 0, bytesRead);
bytesToCopy -= bytesRead;
}
else {
out.write(buffer, 0, (int) bytesToCopy);
bytesToCopy = 0;
}
out.write(buffer, 0, bytesRead);
bytesToCopy -= bytesRead;
}
return (end - start + 1 - bytesToCopy);
}
@ -204,7 +199,9 @@ public abstract class StreamUtils {
*/
@Contract("null -> fail")
public static int drain(@Nullable InputStream in) throws IOException {
Assert.notNull(in, "No InputStream specified");
if (in == null) {
return 0;
}
return (int) in.transferTo(OutputStream.nullOutputStream());
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -38,6 +38,7 @@ import static org.mockito.Mockito.never;
* Tests for {@link StreamUtils}.
*
* @author Phillip Webb
* @author Juergen Hoeller
*/
class StreamUtilsTests {
@ -45,6 +46,7 @@ class StreamUtilsTests {
private String string = "";
@BeforeEach
void setup() {
new Random().nextBytes(bytes);
@ -53,6 +55,7 @@ class StreamUtilsTests {
}
}
@Test
void copyToByteArray() throws Exception {
InputStream inputStream = new ByteArrayInputStream(bytes);
@ -91,11 +94,30 @@ class StreamUtilsTests {
}
@Test
void copyRange() throws Exception {
void copyRangeWithinBuffer() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamUtils.copyRange(new ByteArrayInputStream(bytes), out, 0, 100);
byte[] range = Arrays.copyOfRange(bytes, 0, 101);
assertThat(out.toByteArray()).isEqualTo(range);
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
StreamUtils.copyRange(in, out, 0, 100);
assertThat(in.available()).isEqualTo(bytes.length - 101);
assertThat(out.toByteArray()).isEqualTo(Arrays.copyOfRange(bytes, 0, 101));
}
@Test
void copyRangeBeyondBuffer() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
StreamUtils.copyRange(in, out, 0, 8200);
assertThat(in.available()).isEqualTo(1);
assertThat(out.toByteArray()).isEqualTo(Arrays.copyOfRange(bytes, 0, 8201));
}
@Test
void copyRangeBeyondAvailable() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
StreamUtils.copyRange(in, out, 0, 8300);
assertThat(in.available()).isEqualTo(0);
assertThat(out.toByteArray()).isEqualTo(Arrays.copyOfRange(bytes, 0, 8202));
}
@Test
@ -127,4 +149,5 @@ class StreamUtilsTests {
ordered.verify(source).write(bytes, 1, 2);
ordered.verify(source, never()).close();
}
}

View File

@ -108,6 +108,23 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
}
}
@Override
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
writeContent(resource, outputMessage);
}
/**
* Add the default headers for the given resource to the given message.
* @since 6.0
*/
public void addDefaultHeaders(HttpOutputMessage message, Resource resource, @Nullable MediaType contentType)
throws IOException {
addDefaultHeaders(message.getHeaders(), resource, contentType);
}
@Override
protected MediaType getDefaultContentType(Resource resource) {
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
@ -125,23 +142,15 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
return (contentLength < 0 ? null : contentLength);
}
/**
* Adds the default headers for the given resource to the given message.
* @since 6.0
*/
public void addDefaultHeaders(HttpOutputMessage message, Resource resource, @Nullable MediaType contentType) throws IOException {
addDefaultHeaders(message.getHeaders(), resource, contentType);
}
@Override
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
writeContent(resource, outputMessage);
protected boolean supportsRepeatableWrites(Resource resource) {
return !(resource instanceof InputStreamResource);
}
protected void writeContent(Resource resource, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
// We cannot use try-with-resources here for the InputStream, since we have
// custom handling of the close() method in a finally-block.
try {
@ -168,8 +177,4 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
}
}
@Override
protected boolean supportsRepeatableWrites(Resource resource) {
return !(resource instanceof InputStreamResource);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -53,22 +53,6 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
}
@Override
@SuppressWarnings("unchecked")
protected MediaType getDefaultContentType(Object object) {
Resource resource = null;
if (object instanceof ResourceRegion resourceRegion) {
resource = resourceRegion.getResource();
}
else {
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
if (!regions.isEmpty()) {
resource = regions.iterator().next().getResource();
}
}
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
}
@Override
public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {
return false;
@ -138,6 +122,43 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
}
}
@Override
protected MediaType getDefaultContentType(Object object) {
Resource resource = null;
if (object instanceof ResourceRegion resourceRegion) {
resource = resourceRegion.getResource();
}
else {
@SuppressWarnings("unchecked")
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
if (!regions.isEmpty()) {
resource = regions.iterator().next().getResource();
}
}
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
}
@Override
protected boolean supportsRepeatableWrites(Object object) {
if (object instanceof ResourceRegion resourceRegion) {
return supportsRepeatableWrites(resourceRegion);
}
else {
@SuppressWarnings("unchecked")
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
for (ResourceRegion region : regions) {
if (!supportsRepeatableWrites(region)) {
return false;
}
}
return true;
}
}
private boolean supportsRepeatableWrites(ResourceRegion region) {
return !(region.getResource() instanceof InputStreamResource);
}
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
Assert.notNull(region, "ResourceRegion must not be null");
@ -240,24 +261,4 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
os.write(buf.getBytes(StandardCharsets.US_ASCII));
}
@Override
@SuppressWarnings("unchecked")
protected boolean supportsRepeatableWrites(Object object) {
if (object instanceof ResourceRegion resourceRegion) {
return supportsRepeatableWrites(resourceRegion);
}
else {
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
for (ResourceRegion region : regions) {
if (!supportsRepeatableWrites(region)) {
return false;
}
}
return true;
}
}
private boolean supportsRepeatableWrites(ResourceRegion region) {
return !(region.getResource() instanceof InputStreamResource);
}
}