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;