Skip to content

Commit 9d2c6f8

Browse files
committed
Polishing
1 parent d9330bc commit 9d2c6f8

File tree

4 files changed

+42
-35
lines changed

4 files changed

+42
-35
lines changed

Diff for: spring-core/src/test/java/org/springframework/util/StreamUtilsTests.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,6 +45,7 @@ class StreamUtilsTests {
4545

4646
private String string = "";
4747

48+
4849
@BeforeEach
4950
void setup() {
5051
new Random().nextBytes(bytes);
@@ -53,6 +54,7 @@ void setup() {
5354
}
5455
}
5556

57+
5658
@Test
5759
void copyToByteArray() throws Exception {
5860
InputStream inputStream = new ByteArrayInputStream(bytes);
@@ -127,4 +129,5 @@ void nonClosingOutputStream() throws Exception {
127129
ordered.verify(source).write(bytes, 1, 2);
128130
ordered.verify(source, never()).close();
129131
}
132+
130133
}

Diff for: spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java

+19-14
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessag
8282
if (this.supportsReadStreaming && InputStreamResource.class == clazz) {
8383
return new InputStreamResource(inputMessage.getBody()) {
8484
@Override
85+
@Nullable
8586
public String getFilename() {
8687
return inputMessage.getHeaders().getContentDisposition().getFilename();
8788
}
@@ -107,6 +108,23 @@ public String getFilename() {
107108
}
108109
}
109110

111+
@Override
112+
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage)
113+
throws IOException, HttpMessageNotWritableException {
114+
115+
writeContent(resource, outputMessage);
116+
}
117+
118+
/**
119+
* Add the default headers for the given resource to the given message.
120+
* @since 6.0
121+
*/
122+
public void addDefaultHeaders(HttpOutputMessage message, Resource resource, @Nullable MediaType contentType)
123+
throws IOException {
124+
125+
addDefaultHeaders(message.getHeaders(), resource, contentType);
126+
}
127+
110128
@Override
111129
protected MediaType getDefaultContentType(Resource resource) {
112130
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
@@ -124,23 +142,10 @@ protected Long getContentLength(Resource resource, @Nullable MediaType contentTy
124142
return (contentLength < 0 ? null : contentLength);
125143
}
126144

127-
/**
128-
* Adds the default headers for the given resource to the given message.
129-
* @since 6.0
130-
*/
131-
public void addDefaultHeaders(HttpOutputMessage message, Resource resource, @Nullable MediaType contentType) throws IOException {
132-
addDefaultHeaders(message.getHeaders(), resource, contentType);
133-
}
134-
135-
@Override
136-
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage)
137-
throws IOException, HttpMessageNotWritableException {
138-
139-
writeContent(resource, outputMessage);
140-
}
141145

142146
protected void writeContent(Resource resource, HttpOutputMessage outputMessage)
143147
throws IOException, HttpMessageNotWritableException {
148+
144149
// We cannot use try-with-resources here for the InputStream, since we have
145150
// custom handling of the close() method in a finally-block.
146151
try {

Diff for: spring-web/src/main/java/org/springframework/http/converter/ResourceRegionHttpMessageConverter.java

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,22 +52,6 @@ public ResourceRegionHttpMessageConverter() {
5252
}
5353

5454

55-
@Override
56-
@SuppressWarnings("unchecked")
57-
protected MediaType getDefaultContentType(Object object) {
58-
Resource resource = null;
59-
if (object instanceof ResourceRegion resourceRegion) {
60-
resource = resourceRegion.getResource();
61-
}
62-
else {
63-
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
64-
if (!regions.isEmpty()) {
65-
resource = regions.iterator().next().getResource();
66-
}
67-
}
68-
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
69-
}
70-
7155
@Override
7256
public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {
7357
return false;
@@ -119,14 +103,14 @@ public boolean canWrite(@Nullable Type type, @Nullable Class<?> clazz, @Nullable
119103
}
120104

121105
@Override
122-
@SuppressWarnings("unchecked")
123106
protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage)
124107
throws IOException, HttpMessageNotWritableException {
125108

126109
if (object instanceof ResourceRegion resourceRegion) {
127110
writeResourceRegion(resourceRegion, outputMessage);
128111
}
129112
else {
113+
@SuppressWarnings("unchecked")
130114
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
131115
if (regions.size() == 1) {
132116
writeResourceRegion(regions.iterator().next(), outputMessage);
@@ -137,6 +121,22 @@ protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessa
137121
}
138122
}
139123

124+
@Override
125+
protected MediaType getDefaultContentType(Object object) {
126+
Resource resource = null;
127+
if (object instanceof ResourceRegion resourceRegion) {
128+
resource = resourceRegion.getResource();
129+
}
130+
else {
131+
@SuppressWarnings("unchecked")
132+
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
133+
if (!regions.isEmpty()) {
134+
resource = regions.iterator().next().getResource();
135+
}
136+
}
137+
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
138+
}
139+
140140

141141
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
142142
Assert.notNull(region, "ResourceRegion must not be null");

Diff for: spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -230,7 +230,6 @@ else if (targetException instanceof Exception exception) {
230230

231231
/**
232232
* Invoke the given Kotlin coroutine suspended function.
233-
*
234233
* <p>The default implementation invokes
235234
* {@link CoroutinesUtils#invokeSuspendingFunction(Method, Object, Object...)},
236235
* but subclasses can override this method to use

0 commit comments

Comments
 (0)