Skip to content

Commit e0c4c75

Browse files
committed
Adding null handling for missing file read (and test case)
1 parent 496ad00 commit e0c4c75

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/main/java/picoded/dstack/mongodb/MongoDB_FileWorkspaceMap.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,15 @@ public void backend_fileWriteInputStream(final String oid, final String filepath
637637
**/
638638
@Override
639639
public byte[] backend_fileRead(String oid, String filepath) {
640+
// Get the buffer
640641
InputStream buffer = backend_fileReadInputStream(oid, filepath);
642+
643+
// Null handling
644+
if( buffer == null ) {
645+
return null;
646+
}
647+
648+
// Cast the inputstream to byte[]
641649
byte[] ret = null;
642650
try {
643651
ret = IOUtils.toByteArray(buffer);
@@ -667,7 +675,17 @@ public byte[] backend_fileRead(String oid, String filepath) {
667675
* @return the stored byte array of the file
668676
**/
669677
public InputStream backend_fileReadInputStream(String oid, String filepath) {
670-
return gridFSBucket.openDownloadStream(oid + "/" + filepath);
678+
try {
679+
return gridFSBucket.openDownloadStream(oid + "/" + filepath);
680+
} catch(Exception e) {
681+
if( e.getMessage().toLowerCase().indexOf("no file found") >= 0 ) {
682+
// Does nothing if no file is found
683+
return null;
684+
}
685+
686+
// rethrow the error
687+
throw e;
688+
}
671689
}
672690

673691
@Override

src/test/java/picoded/dstack/struct/simple/StructSimple_FileWorkspaceMap_test.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,24 @@ public void folderSetupAndRemove_pathVarients() {
144144

145145
}
146146

147+
@Test
148+
public void noFileRead_returnsNull() {
149+
// Get the file workspace to use
150+
FileWorkspace fileWorkspace = testObj.newEntry();
151+
assertNotNull(fileWorkspace);
152+
153+
// Check for null (no file) for non existant file
154+
assertNull(fileWorkspace.readString("test/folder/file.txt"));
155+
156+
// Write file
157+
fileWorkspace.writeString("test/folder/file.txt", "anything");
158+
assertTrue(fileWorkspace.fileExist("test/folder/file.txt"));
159+
assertEquals(fileWorkspace.readString("test/folder/file.txt"), "anything");
160+
161+
// Check for null (no file) for non existant file
162+
assertNull(fileWorkspace.readString("test/folder/file-somethingElse.txt"));
163+
}
164+
147165
@Test
148166
public void fileWrite_andProperlySetupFolder() {
149167
// Get the file workspace to use
@@ -153,6 +171,9 @@ public void fileWrite_andProperlySetupFolder() {
153171
// Folder does not exist first
154172
assertFalse(fileWorkspace.folderPathExist("test/folder"));
155173

174+
// Check for null (no file) for non existant file
175+
assertNull(fileWorkspace.readString("test/folder/file.txt"));
176+
156177
// Write file
157178
fileWorkspace.writeString("test/folder/file.txt", "anything");
158179
assertTrue(fileWorkspace.folderPathExist("test/folder"));

0 commit comments

Comments
 (0)