Skip to content

Commit

Permalink
Ensure PhysFSInputStream throws FileNotFoundException like SFSInputSt…
Browse files Browse the repository at this point in the history
…ream
  • Loading branch information
DavidGregory084 committed Jan 25, 2021
1 parent 32fa232 commit 74ad4f5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
24 changes: 12 additions & 12 deletions physfs_java/src/main/java/com/maddox/rts/PhysFSInputStream.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package com.maddox.rts;

import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

public class PhysFSInputStream extends InputStream {

Expand All @@ -17,21 +13,25 @@ public class PhysFSInputStream extends InputStream {
private long fd;
private final String fileName;

public PhysFSInputStream(String file) {
public PhysFSInputStream(String file) throws FileNotFoundException {
this.fd = openRead(file);
this.fileName = file;
if (this.fd == 0) {
var exc = new PhysFSException("while opening file " + file);
var stackTrace = Thread.currentThread().getStackTrace();
var stackTraceElems = Arrays.stream(stackTrace);
if (stackTraceElems.noneMatch(elem -> "com.maddox.rts.ObjIO".equals(elem.getClassName()))) {
PhysFS.logMissing(file);
if (exc.getCode() == PhysFS.ERR_NOT_FOUND) {
var stackTrace = Thread.currentThread().getStackTrace();
var stackTraceElems = Arrays.stream(stackTrace);
if (stackTraceElems.noneMatch(elem -> "com.maddox.rts.ObjIO".equals(elem.getClassName()))) {
PhysFS.logMissing(file);
}
throw new FileNotFoundException(exc.getMessage());
} else {
throw exc;
}
throw exc;
}
}

public PhysFSInputStream(File file) {
public PhysFSInputStream(File file) throws FileNotFoundException {
this(file.getPath());
}

Expand Down
4 changes: 1 addition & 3 deletions physfs_java/src/main/java/com/maddox/rts/PhysFSLoader.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.maddox.rts;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;

public class PhysFSLoader extends ClassLoader {
private static PhysFSLoader loader = new PhysFSLoader();
Expand Down Expand Up @@ -57,7 +55,7 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
} else {
throw new ClassNotFoundException(name);
}
} catch (PhysFSException exc) {
} catch (Exception exc) {
throw new ClassNotFoundException(name, exc);
}
}
Expand Down
17 changes: 9 additions & 8 deletions physfs_java/src/main/java/com/maddox/rts/PhysFSReader.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
package com.maddox.rts;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class PhysFSReader extends InputStreamReader {
public PhysFSReader(String file) {
public PhysFSReader(String file) throws FileNotFoundException {
super(new PhysFSInputStream(file));
}

public PhysFSReader(File file) {
public PhysFSReader(File file) throws FileNotFoundException {
super(new PhysFSInputStream(file));
}

public PhysFSReader(String file, String charset) throws UnsupportedEncodingException {
public PhysFSReader(String file, String charset) throws UnsupportedEncodingException, FileNotFoundException {
super(new PhysFSInputStream(file), charset);
}

public PhysFSReader(File file, String charset) throws UnsupportedEncodingException {
public PhysFSReader(File file, String charset) throws UnsupportedEncodingException, FileNotFoundException {
super(new PhysFSInputStream(file), charset);
}

public PhysFSReader(String var1, int[] var2) {
public PhysFSReader(String var1, int[] var2) throws FileNotFoundException {
super(new PhysFSInputStream(var1));
}

public PhysFSReader(File var1, int[] var2) {
public PhysFSReader(File var1, int[] var2) throws FileNotFoundException {
super(new PhysFSInputStream(var1));
}

public PhysFSReader(String var1, String var2, int[] var3) throws UnsupportedEncodingException {
public PhysFSReader(String var1, String var2, int[] var3) throws UnsupportedEncodingException, FileNotFoundException {
super(new PhysFSInputStream(var1), var2);
}

public PhysFSReader(File var1, String var2, int[] var3) throws UnsupportedEncodingException {
public PhysFSReader(File var1, String var2, int[] var3) throws UnsupportedEncodingException, FileNotFoundException {
super(new PhysFSInputStream(var1), var2);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public int getContentLength() {
if (len > Integer.MAX_VALUE)
return -1;
else
return (int)len;
return (int) len;
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions physfs_java/src/test/java/com/maddox/rts/PhysFSTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void redirectsSfsMountToZip() {
@Test
void inputStreamOpenUnmountedFile() {
assertThrows(
PhysFSException.class,
FileNotFoundException.class,
() -> new PhysFSInputStream("test2.ini"),
"Attempting to open a file from an unmounted archive didn't throw an exception");
}
Expand All @@ -112,7 +112,7 @@ void inputStreamTell() throws IOException {
}

@Test
void inputStreamSeek() {
void inputStreamSeek() throws FileNotFoundException {
long expectedPosition = -1;
long actualPosition = -1;

Expand All @@ -127,7 +127,7 @@ void inputStreamSeek() {
}

@Test
void inputStreamSeekEof() {
void inputStreamSeekEof() throws FileNotFoundException {
long seekPosition = -1;
long fileLength = 0;

Expand All @@ -141,7 +141,7 @@ void inputStreamSeekEof() {
}

@Test
void inputStreamSeekAfterEof() {
void inputStreamSeekAfterEof() throws FileNotFoundException {
try (PhysFSInputStream is = new PhysFSInputStream("test.ini")) {
assertThrows(
PhysFSException.class,
Expand All @@ -151,7 +151,7 @@ void inputStreamSeekAfterEof() {
}

@Test
void inputStreamEndOfFile() {
void inputStreamEndOfFile() throws FileNotFoundException {
try (PhysFSInputStream is = new PhysFSInputStream("test.ini")) {
is.seek(is.fileLength());
assertTrue(is.endOfFile(), "End of file condition was not set when seeking to end of file");
Expand Down

0 comments on commit 74ad4f5

Please sign in to comment.