Skip to content

Commit 4da2499

Browse files
derrick-andersonpoutsma
authored andcommitted
Introduce Resource::getContentAsString
This commit introduces the getContentAsString method to Resource, returning the string contents of the resource. Closes gh-24651
1 parent 18896ac commit 4da2499

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
import java.net.URLConnection;
2727
import java.nio.channels.FileChannel;
2828
import java.nio.channels.ReadableByteChannel;
29+
import java.nio.charset.Charset;
30+
import java.nio.file.Files;
2931
import java.nio.file.NoSuchFileException;
32+
import java.nio.file.Paths;
3033
import java.nio.file.StandardOpenOption;
3134
import java.util.jar.JarEntry;
3235

@@ -320,6 +323,39 @@ protected void customizeConnection(URLConnection con) throws IOException {
320323
protected void customizeConnection(HttpURLConnection con) throws IOException {
321324
}
322325

326+
/**
327+
* This implementation returns the contents of a file as a string using the
328+
* system default Charset. Provided the resource exists and the context has
329+
* access to it, the contents will be returned as a single string with line
330+
* feed characters retained.
331+
* @return the contents of the requested file as a {@code String}.
332+
* @throws FileNotFoundException in the event the file path is invalid.
333+
* @throws IOException if the file can not be read or cannot be serialzied.
334+
*/
335+
@Override
336+
public String getContentAsString() throws IOException {
337+
338+
if( !exists() ) {
339+
throw new FileNotFoundException(getDescription() + " cannot be found.");
340+
}
341+
if ( !isReadable() ) {
342+
throw new IOException(getDescription() + " cannot be opened for reading.");
343+
}
344+
return new String(Files.readAllBytes(Paths.get(getFile().getAbsolutePath())), Charset.defaultCharset());
345+
}
346+
347+
@Override
348+
public String getContentAsString(Charset charset) throws IOException {
349+
350+
if( !exists() ) {
351+
throw new FileNotFoundException(getDescription() + " cannot be found.");
352+
}
353+
if ( !isReadable() ) {
354+
throw new IOException(getDescription() + " cannot be opened for reading.");
355+
}
356+
return new String(Files.readAllBytes(Paths.get(getFile().getAbsolutePath())), charset);
357+
358+
}
323359

324360
/**
325361
* Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime.

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
package org.springframework.core.io;
1818

1919
import java.io.File;
20+
import java.io.FileNotFoundException;
2021
import java.io.IOException;
2122
import java.io.InputStream;
2223
import java.net.URI;
2324
import java.net.URL;
2425
import java.nio.channels.Channels;
2526
import java.nio.channels.ReadableByteChannel;
27+
import java.nio.charset.Charset;
2628

2729
import org.springframework.lang.Nullable;
2830

@@ -176,4 +178,41 @@ default ReadableByteChannel readableChannel() throws IOException {
176178
*/
177179
String getDescription();
178180

181+
/**
182+
* Return a {@link ReadableByteChannel}.
183+
* <p>It is expected that each call creates a <i>fresh</i> channel.
184+
* <p>The default implementation returns {@link Channels#newChannel(InputStream)}
185+
* with the result of {@link #getInputStream()}.
186+
* @return the byte channel for the underlying resource (must not be {@code null})
187+
* @throws java.io.FileNotFoundException if the underlying resource doesn't exist
188+
* @throws IOException if the content channel could not be opened
189+
* @since 5.0
190+
* @see #getInputStream()
191+
*/
192+
193+
/**
194+
* Returns the contents of a file as a string using the system default Charset.
195+
* <p>The default implementation returns a {@link Object#toString()} representation of the resource.
196+
* @return the contents of the requested file as a {@code String}.
197+
* @throws FileNotFoundException in the event the file path is invalid.
198+
* @throws IOException if the file can not be read or cannot be accessed.
199+
* @since 5.2.5
200+
*/
201+
default String getContentAsString() throws IOException{
202+
return toString();
203+
}
204+
205+
/**
206+
* Returns the contents of a file as a string using the specified Charset.
207+
* <p>The default implementation returns a {@link Object#toString()} representation of the resource.
208+
* @param charset the {@code Charset} to use to deserialize the content. Defaults to system default.
209+
* @return the contents of the requested file as a {@code String}.
210+
* @throws FileNotFoundException in the event the file path is invalid.
211+
* @throws IOException if the file can not be read or cannot be accessed.
212+
* @since 5.2.5
213+
*/
214+
default String getContentAsString(Charset charset) throws IOException{
215+
return toString();
216+
}
217+
179218
}

spring-core/src/test/java/org/springframework/core/io/ResourceTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,4 +456,25 @@ public String getDescription() {
456456

457457
}
458458

459+
@Test
460+
void getContentAsString_givenValidFile_ShouldReturnFileContent() throws IOException {
461+
String expectedString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
462+
+ "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">\n"
463+
+ "<properties version=\"1.0\">\n"
464+
+ "\t<entry key=\"foo\">bar</entry>\n"
465+
+ "</properties>";
466+
467+
String fileDirString =
468+
new ClassPathResource("org/springframework/core/io/example.xml").getContentAsString();
469+
assertThat(fileDirString).isNotBlank();
470+
assertThat(fileDirString).isEqualTo(expectedString);
471+
}
472+
473+
@Test
474+
void getContentAsString_givenAnInvalidFile_ShouldThrowFileNotFoundException(){
475+
assertThatExceptionOfType(FileNotFoundException.class)
476+
.isThrownBy(new ClassPathResource("nonExistantFile")::getContentAsString);
477+
478+
}
479+
459480
}

spring-core/src/test/resources/org/springframework/core/io/example.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
33
<properties version="1.0">
44
<entry key="foo">bar</entry>
5-
</properties>
6-
5+
</properties>

0 commit comments

Comments
 (0)