Skip to content

Commit

Permalink
Consistent ordering of Resource methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Feb 15, 2023
1 parent 0619e19 commit f87a87e
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 92 deletions.
53 changes: 27 additions & 26 deletions spring-core/src/main/java/org/springframework/core/io/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* certain resources. The actual behavior is implementation-specific.
*
* @author Juergen Hoeller
* @author Arjen Poutsma
* @since 28.12.2003
* @see #getInputStream()
* @see #getURL()
Expand Down Expand Up @@ -138,6 +139,32 @@ default ReadableByteChannel readableChannel() throws IOException {
return Channels.newChannel(getInputStream());
}

/**
* Return the contents of this resource as a byte array.
* @return the contents of this resource as byte array
* @throws java.io.FileNotFoundException if the resource cannot be resolved as
* absolute file path, i.e. if the resource is not available in a file system
* @throws IOException in case of general resolution/reading failures
* @since 6.0.5
*/
default byte[] getContentAsByteArray() throws IOException {
return FileCopyUtils.copyToByteArray(getInputStream());
}

/**
* Returns the contents of this resource as a string, using the specified
* charset.
* @param charset the charset to use for decoding
* @return the contents of this resource as a {@code String}
* @throws java.io.FileNotFoundException if the resource cannot be resolved as
* absolute file path, i.e. if the resource is not available in a file system
* @throws IOException in case of general resolution/reading failures
* @since 6.0.5
*/
default String getContentAsString(Charset charset) throws IOException {
return FileCopyUtils.copyToString(new InputStreamReader(getInputStream(), charset));
}

/**
* Determine the content length for this resource.
* @throws IOException if the resource cannot be resolved
Expand Down Expand Up @@ -179,30 +206,4 @@ default ReadableByteChannel readableChannel() throws IOException {
*/
String getDescription();

/**
* Return the contents of this resource as a byte array.
* @return the contents of this resource as byte array
* @throws java.io.FileNotFoundException if the resource cannot be resolved as
* absolute file path, i.e. if the resource is not available in a file system
* @throws IOException in case of general resolution/reading failures
* @since 6.0.5
*/
default byte[] getContentAsByteArray() throws IOException {
return FileCopyUtils.copyToByteArray(getInputStream());
}

/**
* Returns the contents of this resource as a string, using the specified
* charset.
* @param charset the charset to use for decoding
* @return the contents of this resource as a {@code String}
* @throws java.io.FileNotFoundException if the resource cannot be resolved as
* absolute file path, i.e. if the resource is not available in a file system
* @throws IOException in case of general resolution/reading failures
* @since 6.0.5
*/
default String getContentAsString(Charset charset) throws IOException {
return FileCopyUtils.copyToString(new InputStreamReader(getInputStream(), charset));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*
* @author Juergen Hoeller
* @author Sam Brannen
* @author Arjen Poutsma
* @since 1.2.6
* @see Resource#getInputStream()
* @see java.io.Reader
Expand Down Expand Up @@ -161,10 +162,10 @@ public InputStream getInputStream() throws IOException {

/**
* Returns the contents of the specified resource as a string, using the specified
* {@link #getCharset() Charset} or {@linkplain #getEncoding() encoding}
* (if any).
* {@link #getCharset() Charset} or {@linkplain #getEncoding() encoding} (if any).
* @throws IOException if opening the resource failed
* @since 6.0.5
* @see Resource#getContentAsString(Charset)
*/
public String getContentAsString() throws IOException {
Charset charset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -204,11 +205,6 @@ static final class EncodedResource extends AbstractResource implements HttpResou
this.encoded = original.createRelative(original.getFilename() + extension);
}

@Override
public InputStream getInputStream() throws IOException {
return this.encoded.getInputStream();
}

@Override
public boolean exists() {
return this.encoded.exists();
Expand Down Expand Up @@ -244,6 +240,26 @@ public File getFile() throws IOException {
return this.encoded.getFile();
}

@Override
public InputStream getInputStream() throws IOException {
return this.encoded.getInputStream();
}

@Override
public ReadableByteChannel readableChannel() throws IOException {
return this.encoded.readableChannel();
}

@Override
public byte[] getContentAsByteArray() throws IOException {
return this.encoded.getContentAsByteArray();
}

@Override
public String getContentAsString(Charset charset) throws IOException {
return this.encoded.getContentAsString(charset);
}

@Override
public long contentLength() throws IOException {
return this.encoded.contentLength();
Expand All @@ -270,16 +286,6 @@ public String getDescription() {
return this.encoded.getDescription();
}

@Override
public byte[] getContentAsByteArray() throws IOException {
return this.encoded.getContentAsByteArray();
}

@Override
public String getContentAsString(Charset charset) throws IOException {
return this.encoded.getContentAsString(charset);
}

@Override
public HttpHeaders getResponseHeaders() {
HttpHeaders headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -284,44 +285,49 @@ public File getFile() throws IOException {
}

@Override
@Nullable
public String getFilename() {
return this.original.getFilename();
public InputStream getInputStream() throws IOException {
return this.original.getInputStream();
}

@Override
public long contentLength() throws IOException {
return this.original.contentLength();
public ReadableByteChannel readableChannel() throws IOException {
return this.original.readableChannel();
}

@Override
public long lastModified() throws IOException {
return this.original.lastModified();
public byte[] getContentAsByteArray() throws IOException {
return this.original.getContentAsByteArray();
}

@Override
public Resource createRelative(String relativePath) throws IOException {
return this.original.createRelative(relativePath);
public String getContentAsString(Charset charset) throws IOException {
return this.original.getContentAsString(charset);
}

@Override
public String getDescription() {
return this.original.getDescription();
public long contentLength() throws IOException {
return this.original.contentLength();
}

@Override
public byte[] getContentAsByteArray() throws IOException {
return this.original.getContentAsByteArray();
public long lastModified() throws IOException {
return this.original.lastModified();
}

@Override
public String getContentAsString(Charset charset) throws IOException {
return this.original.getContentAsString(charset);
public Resource createRelative(String relativePath) throws IOException {
return this.original.createRelative(relativePath);
}

@Override
public InputStream getInputStream() throws IOException {
return this.original.getInputStream();
@Nullable
public String getFilename() {
return this.original.getFilename();
}

@Override
public String getDescription() {
return this.original.getDescription();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -201,11 +202,6 @@ static final class EncodedResource extends AbstractResource implements HttpResou
}


@Override
public InputStream getInputStream() throws IOException {
return this.encoded.getInputStream();
}

@Override
public boolean exists() {
return this.encoded.exists();
Expand Down Expand Up @@ -241,6 +237,26 @@ public File getFile() throws IOException {
return this.encoded.getFile();
}

@Override
public InputStream getInputStream() throws IOException {
return this.encoded.getInputStream();
}

@Override
public ReadableByteChannel readableChannel() throws IOException {
return this.encoded.readableChannel();
}

@Override
public byte[] getContentAsByteArray() throws IOException {
return this.encoded.getContentAsByteArray();
}

@Override
public String getContentAsString(Charset charset) throws IOException {
return this.encoded.getContentAsString(charset);
}

@Override
public long contentLength() throws IOException {
return this.encoded.contentLength();
Expand All @@ -267,16 +283,6 @@ public String getDescription() {
return this.encoded.getDescription();
}

@Override
public byte[] getContentAsByteArray() throws IOException {
return this.encoded.getContentAsByteArray();
}

@Override
public String getContentAsString(Charset charset) throws IOException {
return this.encoded.getContentAsString(charset);
}

@Override
public HttpHeaders getResponseHeaders() {
HttpHeaders headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -280,44 +281,49 @@ public File getFile() throws IOException {
}

@Override
@Nullable
public String getFilename() {
return this.original.getFilename();
public InputStream getInputStream() throws IOException {
return this.original.getInputStream();
}

@Override
public long contentLength() throws IOException {
return this.original.contentLength();
public ReadableByteChannel readableChannel() throws IOException {
return this.original.readableChannel();
}

@Override
public long lastModified() throws IOException {
return this.original.lastModified();
public byte[] getContentAsByteArray() throws IOException {
return this.original.getContentAsByteArray();
}

@Override
public Resource createRelative(String relativePath) throws IOException {
return this.original.createRelative(relativePath);
public String getContentAsString(Charset charset) throws IOException {
return this.original.getContentAsString(charset);
}

@Override
public String getDescription() {
return this.original.getDescription();
public long contentLength() throws IOException {
return this.original.contentLength();
}

@Override
public byte[] getContentAsByteArray() throws IOException {
return this.original.getContentAsByteArray();
public long lastModified() throws IOException {
return this.original.lastModified();
}

@Override
public String getContentAsString(Charset charset) throws IOException {
return this.original.getContentAsString(charset);
public Resource createRelative(String relativePath) throws IOException {
return this.original.createRelative(relativePath);
}

@Override
public InputStream getInputStream() throws IOException {
return this.original.getInputStream();
@Nullable
public String getFilename() {
return this.original.getFilename();
}

@Override
public String getDescription() {
return this.original.getDescription();
}

@Override
Expand Down

0 comments on commit f87a87e

Please sign in to comment.