-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced Java 11 Files.readString with an equivalent FilesUtil.readString
- Loading branch information
1 parent
bf4fa47
commit d8d5b9f
Showing
6 changed files
with
142 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
cli/src/main/java/io/github/nubesgen/cli/util/FilesUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.github.nubesgen.cli.util; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Java 8 compatible Files helpers. | ||
*/ | ||
public class FilesUtil { | ||
|
||
/** | ||
* Reads all content from a file into a string, decoding from bytes to characters using the UTF-8 charset. | ||
* The method ensures that the file is closed when all content have been read or an I/O error, or other | ||
* runtime exception, is thrown. | ||
* | ||
* Reproduces the behavior of Java 11 Paths.readString(Path). | ||
* | ||
* @param path the path to the file | ||
* @return a String containing the content read from the file | ||
* @throws IOException if an I/O error occurs reading from the file or a malformed or unmappable byte sequence is read | ||
*/ | ||
public static String readString(final Path path) throws IOException { | ||
final StringBuilder resultStringBuilder = new StringBuilder(); | ||
try (final BufferedReader reader = Files.newBufferedReader(path, UTF_8)) { | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
resultStringBuilder.append(line).append("\n"); | ||
} | ||
} | ||
return resultStringBuilder.toString(); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
cli/src/test/java/io/github/nubesgen/cli/util/FilesUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.github.nubesgen.cli.util; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.NoSuchFileException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
class FilesUtilTest { | ||
|
||
@Test | ||
public void readStringOk() throws URISyntaxException, IOException { | ||
final Path path = Paths.get(getClass().getResource("/util/content.txt").toURI()); | ||
final String content = FilesUtil.readString(path); | ||
assertThat(content).isEqualTo("Nubes\nGen\n"); | ||
} | ||
|
||
@Test | ||
public void readStringNonExistentThrowsFileNotFoundException() throws URISyntaxException, IOException { | ||
final Path path = Paths.get("nonexistent"); | ||
assertThatThrownBy(() -> FilesUtil.readString(path)).isInstanceOf(NoSuchFileException.class); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Nubes | ||
Gen |