Skip to content

Commit f87a87e

Browse files
committed
Consistent ordering of Resource methods
See gh-24651
1 parent 0619e19 commit f87a87e

File tree

6 files changed

+118
-92
lines changed

6 files changed

+118
-92
lines changed

spring-core/src/main/java/org/springframework/core/io/Resource.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* certain resources. The actual behavior is implementation-specific.
3939
*
4040
* @author Juergen Hoeller
41+
* @author Arjen Poutsma
4142
* @since 28.12.2003
4243
* @see #getInputStream()
4344
* @see #getURL()
@@ -138,6 +139,32 @@ default ReadableByteChannel readableChannel() throws IOException {
138139
return Channels.newChannel(getInputStream());
139140
}
140141

142+
/**
143+
* Return the contents of this resource as a byte array.
144+
* @return the contents of this resource as byte array
145+
* @throws java.io.FileNotFoundException if the resource cannot be resolved as
146+
* absolute file path, i.e. if the resource is not available in a file system
147+
* @throws IOException in case of general resolution/reading failures
148+
* @since 6.0.5
149+
*/
150+
default byte[] getContentAsByteArray() throws IOException {
151+
return FileCopyUtils.copyToByteArray(getInputStream());
152+
}
153+
154+
/**
155+
* Returns the contents of this resource as a string, using the specified
156+
* charset.
157+
* @param charset the charset to use for decoding
158+
* @return the contents of this resource as a {@code String}
159+
* @throws java.io.FileNotFoundException if the resource cannot be resolved as
160+
* absolute file path, i.e. if the resource is not available in a file system
161+
* @throws IOException in case of general resolution/reading failures
162+
* @since 6.0.5
163+
*/
164+
default String getContentAsString(Charset charset) throws IOException {
165+
return FileCopyUtils.copyToString(new InputStreamReader(getInputStream(), charset));
166+
}
167+
141168
/**
142169
* Determine the content length for this resource.
143170
* @throws IOException if the resource cannot be resolved
@@ -179,30 +206,4 @@ default ReadableByteChannel readableChannel() throws IOException {
179206
*/
180207
String getDescription();
181208

182-
/**
183-
* Return the contents of this resource as a byte array.
184-
* @return the contents of this resource as byte array
185-
* @throws java.io.FileNotFoundException if the resource cannot be resolved as
186-
* absolute file path, i.e. if the resource is not available in a file system
187-
* @throws IOException in case of general resolution/reading failures
188-
* @since 6.0.5
189-
*/
190-
default byte[] getContentAsByteArray() throws IOException {
191-
return FileCopyUtils.copyToByteArray(getInputStream());
192-
}
193-
194-
/**
195-
* Returns the contents of this resource as a string, using the specified
196-
* charset.
197-
* @param charset the charset to use for decoding
198-
* @return the contents of this resource as a {@code String}
199-
* @throws java.io.FileNotFoundException if the resource cannot be resolved as
200-
* absolute file path, i.e. if the resource is not available in a file system
201-
* @throws IOException in case of general resolution/reading failures
202-
* @since 6.0.5
203-
*/
204-
default String getContentAsString(Charset charset) throws IOException {
205-
return FileCopyUtils.copyToString(new InputStreamReader(getInputStream(), charset));
206-
}
207-
208209
}

spring-core/src/main/java/org/springframework/core/io/support/EncodedResource.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
*
3838
* @author Juergen Hoeller
3939
* @author Sam Brannen
40+
* @author Arjen Poutsma
4041
* @since 1.2.6
4142
* @see Resource#getInputStream()
4243
* @see java.io.Reader
@@ -161,10 +162,10 @@ public InputStream getInputStream() throws IOException {
161162

162163
/**
163164
* Returns the contents of the specified resource as a string, using the specified
164-
* {@link #getCharset() Charset} or {@linkplain #getEncoding() encoding}
165-
* (if any).
165+
* {@link #getCharset() Charset} or {@linkplain #getEncoding() encoding} (if any).
166166
* @throws IOException if opening the resource failed
167167
* @since 6.0.5
168+
* @see Resource#getContentAsString(Charset)
168169
*/
169170
public String getContentAsString() throws IOException {
170171
Charset charset;

spring-webflux/src/main/java/org/springframework/web/reactive/resource/EncodedResourceResolver.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.InputStream;
2222
import java.net.URI;
2323
import java.net.URL;
24+
import java.nio.channels.ReadableByteChannel;
2425
import java.nio.charset.Charset;
2526
import java.util.ArrayList;
2627
import java.util.Arrays;
@@ -204,11 +205,6 @@ static final class EncodedResource extends AbstractResource implements HttpResou
204205
this.encoded = original.createRelative(original.getFilename() + extension);
205206
}
206207

207-
@Override
208-
public InputStream getInputStream() throws IOException {
209-
return this.encoded.getInputStream();
210-
}
211-
212208
@Override
213209
public boolean exists() {
214210
return this.encoded.exists();
@@ -244,6 +240,26 @@ public File getFile() throws IOException {
244240
return this.encoded.getFile();
245241
}
246242

243+
@Override
244+
public InputStream getInputStream() throws IOException {
245+
return this.encoded.getInputStream();
246+
}
247+
248+
@Override
249+
public ReadableByteChannel readableChannel() throws IOException {
250+
return this.encoded.readableChannel();
251+
}
252+
253+
@Override
254+
public byte[] getContentAsByteArray() throws IOException {
255+
return this.encoded.getContentAsByteArray();
256+
}
257+
258+
@Override
259+
public String getContentAsString(Charset charset) throws IOException {
260+
return this.encoded.getContentAsString(charset);
261+
}
262+
247263
@Override
248264
public long contentLength() throws IOException {
249265
return this.encoded.contentLength();
@@ -270,16 +286,6 @@ public String getDescription() {
270286
return this.encoded.getDescription();
271287
}
272288

273-
@Override
274-
public byte[] getContentAsByteArray() throws IOException {
275-
return this.encoded.getContentAsByteArray();
276-
}
277-
278-
@Override
279-
public String getContentAsString(Charset charset) throws IOException {
280-
return this.encoded.getContentAsString(charset);
281-
}
282-
283289
@Override
284290
public HttpHeaders getResponseHeaders() {
285291
HttpHeaders headers;

spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.InputStream;
2222
import java.net.URI;
2323
import java.net.URL;
24+
import java.nio.channels.ReadableByteChannel;
2425
import java.nio.charset.Charset;
2526
import java.util.ArrayList;
2627
import java.util.Arrays;
@@ -284,44 +285,49 @@ public File getFile() throws IOException {
284285
}
285286

286287
@Override
287-
@Nullable
288-
public String getFilename() {
289-
return this.original.getFilename();
288+
public InputStream getInputStream() throws IOException {
289+
return this.original.getInputStream();
290290
}
291291

292292
@Override
293-
public long contentLength() throws IOException {
294-
return this.original.contentLength();
293+
public ReadableByteChannel readableChannel() throws IOException {
294+
return this.original.readableChannel();
295295
}
296296

297297
@Override
298-
public long lastModified() throws IOException {
299-
return this.original.lastModified();
298+
public byte[] getContentAsByteArray() throws IOException {
299+
return this.original.getContentAsByteArray();
300300
}
301301

302302
@Override
303-
public Resource createRelative(String relativePath) throws IOException {
304-
return this.original.createRelative(relativePath);
303+
public String getContentAsString(Charset charset) throws IOException {
304+
return this.original.getContentAsString(charset);
305305
}
306306

307307
@Override
308-
public String getDescription() {
309-
return this.original.getDescription();
308+
public long contentLength() throws IOException {
309+
return this.original.contentLength();
310310
}
311311

312312
@Override
313-
public byte[] getContentAsByteArray() throws IOException {
314-
return this.original.getContentAsByteArray();
313+
public long lastModified() throws IOException {
314+
return this.original.lastModified();
315315
}
316316

317317
@Override
318-
public String getContentAsString(Charset charset) throws IOException {
319-
return this.original.getContentAsString(charset);
318+
public Resource createRelative(String relativePath) throws IOException {
319+
return this.original.createRelative(relativePath);
320320
}
321321

322322
@Override
323-
public InputStream getInputStream() throws IOException {
324-
return this.original.getInputStream();
323+
@Nullable
324+
public String getFilename() {
325+
return this.original.getFilename();
326+
}
327+
328+
@Override
329+
public String getDescription() {
330+
return this.original.getDescription();
325331
}
326332

327333
@Override

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/EncodedResourceResolver.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.InputStream;
2222
import java.net.URI;
2323
import java.net.URL;
24+
import java.nio.channels.ReadableByteChannel;
2425
import java.nio.charset.Charset;
2526
import java.util.ArrayList;
2627
import java.util.Arrays;
@@ -201,11 +202,6 @@ static final class EncodedResource extends AbstractResource implements HttpResou
201202
}
202203

203204

204-
@Override
205-
public InputStream getInputStream() throws IOException {
206-
return this.encoded.getInputStream();
207-
}
208-
209205
@Override
210206
public boolean exists() {
211207
return this.encoded.exists();
@@ -241,6 +237,26 @@ public File getFile() throws IOException {
241237
return this.encoded.getFile();
242238
}
243239

240+
@Override
241+
public InputStream getInputStream() throws IOException {
242+
return this.encoded.getInputStream();
243+
}
244+
245+
@Override
246+
public ReadableByteChannel readableChannel() throws IOException {
247+
return this.encoded.readableChannel();
248+
}
249+
250+
@Override
251+
public byte[] getContentAsByteArray() throws IOException {
252+
return this.encoded.getContentAsByteArray();
253+
}
254+
255+
@Override
256+
public String getContentAsString(Charset charset) throws IOException {
257+
return this.encoded.getContentAsString(charset);
258+
}
259+
244260
@Override
245261
public long contentLength() throws IOException {
246262
return this.encoded.contentLength();
@@ -267,16 +283,6 @@ public String getDescription() {
267283
return this.encoded.getDescription();
268284
}
269285

270-
@Override
271-
public byte[] getContentAsByteArray() throws IOException {
272-
return this.encoded.getContentAsByteArray();
273-
}
274-
275-
@Override
276-
public String getContentAsString(Charset charset) throws IOException {
277-
return this.encoded.getContentAsString(charset);
278-
}
279-
280286
@Override
281287
public HttpHeaders getResponseHeaders() {
282288
HttpHeaders headers;

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.InputStream;
2222
import java.net.URI;
2323
import java.net.URL;
24+
import java.nio.channels.ReadableByteChannel;
2425
import java.nio.charset.Charset;
2526
import java.util.ArrayList;
2627
import java.util.Arrays;
@@ -280,44 +281,49 @@ public File getFile() throws IOException {
280281
}
281282

282283
@Override
283-
@Nullable
284-
public String getFilename() {
285-
return this.original.getFilename();
284+
public InputStream getInputStream() throws IOException {
285+
return this.original.getInputStream();
286286
}
287287

288288
@Override
289-
public long contentLength() throws IOException {
290-
return this.original.contentLength();
289+
public ReadableByteChannel readableChannel() throws IOException {
290+
return this.original.readableChannel();
291291
}
292292

293293
@Override
294-
public long lastModified() throws IOException {
295-
return this.original.lastModified();
294+
public byte[] getContentAsByteArray() throws IOException {
295+
return this.original.getContentAsByteArray();
296296
}
297297

298298
@Override
299-
public Resource createRelative(String relativePath) throws IOException {
300-
return this.original.createRelative(relativePath);
299+
public String getContentAsString(Charset charset) throws IOException {
300+
return this.original.getContentAsString(charset);
301301
}
302302

303303
@Override
304-
public String getDescription() {
305-
return this.original.getDescription();
304+
public long contentLength() throws IOException {
305+
return this.original.contentLength();
306306
}
307307

308308
@Override
309-
public byte[] getContentAsByteArray() throws IOException {
310-
return this.original.getContentAsByteArray();
309+
public long lastModified() throws IOException {
310+
return this.original.lastModified();
311311
}
312312

313313
@Override
314-
public String getContentAsString(Charset charset) throws IOException {
315-
return this.original.getContentAsString(charset);
314+
public Resource createRelative(String relativePath) throws IOException {
315+
return this.original.createRelative(relativePath);
316316
}
317317

318318
@Override
319-
public InputStream getInputStream() throws IOException {
320-
return this.original.getInputStream();
319+
@Nullable
320+
public String getFilename() {
321+
return this.original.getFilename();
322+
}
323+
324+
@Override
325+
public String getDescription() {
326+
return this.original.getDescription();
321327
}
322328

323329
@Override

0 commit comments

Comments
 (0)