Skip to content

Commit 0c307b5

Browse files
committed
Polishing
1 parent 6b6beec commit 0c307b5

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
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-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.
@@ -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

+9-6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessag
8181
if (this.supportsReadStreaming && InputStreamResource.class == clazz) {
8282
return new InputStreamResource(inputMessage.getBody()) {
8383
@Override
84+
@Nullable
8485
public String getFilename() {
8586
return inputMessage.getHeaders().getContentDisposition().getFilename();
8687
}
@@ -106,6 +107,13 @@ public String getFilename() {
106107
}
107108
}
108109

110+
@Override
111+
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage)
112+
throws IOException, HttpMessageNotWritableException {
113+
114+
writeContent(resource, outputMessage);
115+
}
116+
109117
@Override
110118
protected MediaType getDefaultContentType(Resource resource) {
111119
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
@@ -123,15 +131,10 @@ protected Long getContentLength(Resource resource, @Nullable MediaType contentTy
123131
return (contentLength < 0 ? null : contentLength);
124132
}
125133

126-
@Override
127-
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage)
128-
throws IOException, HttpMessageNotWritableException {
129-
130-
writeContent(resource, outputMessage);
131-
}
132134

133135
protected void writeContent(Resource resource, HttpOutputMessage outputMessage)
134136
throws IOException, HttpMessageNotWritableException {
137+
135138
// We cannot use try-with-resources here for the InputStream, since we have
136139
// custom handling of the close() method in a finally-block.
137140
try {

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

+21-21
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.
@@ -37,8 +37,8 @@
3737
import org.springframework.util.StreamUtils;
3838

3939
/**
40-
* Implementation of {@link HttpMessageConverter} that can write a single {@link ResourceRegion},
41-
* or Collections of {@link ResourceRegion ResourceRegions}.
40+
* Implementation of {@link HttpMessageConverter} that can write a single
41+
* {@link ResourceRegion} or Collections of {@link ResourceRegion ResourceRegions}.
4242
*
4343
* @author Brian Clozel
4444
* @author Juergen Hoeller
@@ -51,22 +51,6 @@ public ResourceRegionHttpMessageConverter() {
5151
}
5252

5353

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

125109
@Override
126-
@SuppressWarnings("unchecked")
127110
protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage)
128111
throws IOException, HttpMessageNotWritableException {
129112

130113
if (object instanceof ResourceRegion) {
131114
writeResourceRegion((ResourceRegion) object, outputMessage);
132115
}
133116
else {
117+
@SuppressWarnings("unchecked")
134118
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
135119
if (regions.size() == 1) {
136120
writeResourceRegion(regions.iterator().next(), outputMessage);
137121
}
138122
else {
139-
writeResourceRegionCollection((Collection<ResourceRegion>) object, outputMessage);
123+
writeResourceRegionCollection(regions, outputMessage);
140124
}
141125
}
142126
}
143127

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

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

0 commit comments

Comments
 (0)