Consistent ordering of Resource methods

See gh-24651
This commit is contained in:
Juergen Hoeller 2023-02-15 10:13:03 +01:00
parent 0619e19140
commit f87a87e29d
6 changed files with 120 additions and 94 deletions

View File

@ -38,6 +38,7 @@ import org.springframework.util.FileCopyUtils;
* certain resources. The actual behavior is implementation-specific.
*
* @author Juergen Hoeller
* @author Arjen Poutsma
* @since 28.12.2003
* @see #getInputStream()
* @see #getURL()
@ -138,6 +139,32 @@ public interface Resource extends InputStreamSource {
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
@ -179,30 +206,4 @@ public interface Resource extends InputStreamSource {
*/
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));
}
}

View File

@ -37,6 +37,7 @@ import org.springframework.util.ObjectUtils;
*
* @author Juergen Hoeller
* @author Sam Brannen
* @author Arjen Poutsma
* @since 1.2.6
* @see Resource#getInputStream()
* @see java.io.Reader
@ -161,10 +162,10 @@ public class EncodedResource implements InputStreamSource {
/**
* 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;

View File

@ -21,6 +21,7 @@ import java.io.IOException;
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;
@ -204,11 +205,6 @@ public class EncodedResourceResolver extends AbstractResourceResolver {
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();
@ -244,6 +240,26 @@ public class EncodedResourceResolver extends AbstractResourceResolver {
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();
@ -270,16 +286,6 @@ public class EncodedResourceResolver extends AbstractResourceResolver {
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;

View File

@ -21,6 +21,7 @@ import java.io.IOException;
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;
@ -284,9 +285,23 @@ public class VersionResourceResolver extends AbstractResourceResolver {
}
@Override
@Nullable
public String getFilename() {
return this.original.getFilename();
public InputStream getInputStream() throws IOException {
return this.original.getInputStream();
}
@Override
public ReadableByteChannel readableChannel() throws IOException {
return this.original.readableChannel();
}
@Override
public byte[] getContentAsByteArray() throws IOException {
return this.original.getContentAsByteArray();
}
@Override
public String getContentAsString(Charset charset) throws IOException {
return this.original.getContentAsString(charset);
}
@Override
@ -304,26 +319,17 @@ public class VersionResourceResolver extends AbstractResourceResolver {
return this.original.createRelative(relativePath);
}
@Override
@Nullable
public String getFilename() {
return this.original.getFilename();
}
@Override
public String getDescription() {
return this.original.getDescription();
}
@Override
public byte[] getContentAsByteArray() throws IOException {
return this.original.getContentAsByteArray();
}
@Override
public String getContentAsString(Charset charset) throws IOException {
return this.original.getContentAsString(charset);
}
@Override
public InputStream getInputStream() throws IOException {
return this.original.getInputStream();
}
@Override
public HttpHeaders getResponseHeaders() {
HttpHeaders headers = (this.original instanceof HttpResource httpResource ?

View File

@ -21,6 +21,7 @@ import java.io.IOException;
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;
@ -201,11 +202,6 @@ public class EncodedResourceResolver extends AbstractResourceResolver {
}
@Override
public InputStream getInputStream() throws IOException {
return this.encoded.getInputStream();
}
@Override
public boolean exists() {
return this.encoded.exists();
@ -241,6 +237,26 @@ public class EncodedResourceResolver extends AbstractResourceResolver {
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();
@ -267,16 +283,6 @@ public class EncodedResourceResolver extends AbstractResourceResolver {
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;

View File

@ -21,6 +21,7 @@ import java.io.IOException;
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;
@ -280,9 +281,23 @@ public class VersionResourceResolver extends AbstractResourceResolver {
}
@Override
@Nullable
public String getFilename() {
return this.original.getFilename();
public InputStream getInputStream() throws IOException {
return this.original.getInputStream();
}
@Override
public ReadableByteChannel readableChannel() throws IOException {
return this.original.readableChannel();
}
@Override
public byte[] getContentAsByteArray() throws IOException {
return this.original.getContentAsByteArray();
}
@Override
public String getContentAsString(Charset charset) throws IOException {
return this.original.getContentAsString(charset);
}
@Override
@ -300,26 +315,17 @@ public class VersionResourceResolver extends AbstractResourceResolver {
return this.original.createRelative(relativePath);
}
@Override
@Nullable
public String getFilename() {
return this.original.getFilename();
}
@Override
public String getDescription() {
return this.original.getDescription();
}
@Override
public byte[] getContentAsByteArray() throws IOException {
return this.original.getContentAsByteArray();
}
@Override
public String getContentAsString(Charset charset) throws IOException {
return this.original.getContentAsString(charset);
}
@Override
public InputStream getInputStream() throws IOException {
return this.original.getInputStream();
}
@Override
public HttpHeaders getResponseHeaders() {
HttpHeaders headers = (this.original instanceof HttpResource httpResource ?