Skip to content

Commit dcb90e8

Browse files
committed
Add readContents method
1 parent f420ca9 commit dcb90e8

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/main/java/httpserver/file/FileOperator.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import java.nio.file.Path;
55
import java.nio.file.StandardOpenOption;
66

7-
import static java.nio.file.Files.createFile;
8-
import static java.nio.file.Files.deleteIfExists;
9-
import static java.nio.file.Files.write;
7+
import static java.nio.file.Files.*;
108

119
public class FileOperator {
1210
public void createFileAtPath(Path path) throws IOException {
@@ -24,4 +22,8 @@ public void appendToFile(Path path, byte[] contents) throws IOException {
2422
public void replaceContents(Path path, byte[] contents) throws IOException {
2523
write(path, contents);
2624
}
25+
26+
public byte[] readContents(Path path) throws IOException {
27+
return readAllBytes(path);
28+
}
2729
}

src/test/java/httpserver/file/FileOperatorTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ public void replacesFileContentsAtPath() throws Exception {
6363
assertEquals("second line\r\n", fileContents(path));
6464
}
6565

66+
@Test
67+
public void readsFileContentsAtPath() throws Exception {
68+
fileOperator.createFileAtPath(path);
69+
fileOperator.appendToFile(path, "first line\r\n".getBytes());
70+
71+
String contentsAsString = new String(fileOperator.readContents(path));
72+
assertEquals("first line\r\n", contentsAsString);
73+
}
74+
6675
private String fileContents(Path path) throws IOException {
6776
return new String(readAllBytes(path));
6877
}

0 commit comments

Comments
 (0)