Check for execptions when reading an inputstream

This commit is contained in:
Maschell 2019-04-19 11:36:49 +02:00
parent ef878f7e41
commit dbad684549
2 changed files with 17 additions and 0 deletions

View File

@ -48,6 +48,7 @@ public final class StreamUtils {
curReadChunk = toRead; curReadChunk = toRead;
} }
int read = in.read(buffer, 0, curReadChunk); int read = in.read(buffer, 0, curReadChunk);
StreamUtils.checkForException(in);
if (read < 0) break; if (read < 0) break;
System.arraycopy(buffer, 0, result, size - toRead, read); System.arraycopy(buffer, 0, result, size - toRead, read);
toRead -= read; toRead -= read;
@ -65,6 +66,7 @@ public final class StreamUtils {
do { do {
try { try {
bytesRead = inputStream.read(overflowbuf, overflowbuffer.getLengthOfDataInBuffer(), overflowbuffer.getSpaceLeft()); bytesRead = inputStream.read(overflowbuf, overflowbuffer.getLengthOfDataInBuffer(), overflowbuffer.getSpaceLeft());
StreamUtils.checkForException(inputStream);
} catch (IOException e) { } catch (IOException e) {
log.info(e.getMessage()); log.info(e.getMessage());
if (!e.getMessage().equals("Write end dead")) { if (!e.getMessage().equals("Write end dead")) {
@ -141,6 +143,7 @@ public final class StreamUtils {
long written = 0; long written = 0;
do { do {
read = inputStream.read(buffer); read = inputStream.read(buffer);
StreamUtils.checkForException(inputStream);
if (read < 0) { if (read < 0) {
break; break;
} }
@ -192,4 +195,15 @@ public final class StreamUtils {
} }
} }
public static void checkForException(InputStream inputStream) throws IOException {
if (inputStream instanceof PipedInputStreamWithException) {
try {
((PipedInputStreamWithException) inputStream).checkForException();
} catch (Exception e) {
throw new IOException(e);
}
}
}
} }

View File

@ -31,6 +31,7 @@ import de.mas.wiiu.jnus.entities.content.Content;
import de.mas.wiiu.jnus.utils.ByteArrayBuffer; import de.mas.wiiu.jnus.utils.ByteArrayBuffer;
import de.mas.wiiu.jnus.utils.CheckSumWrongException; import de.mas.wiiu.jnus.utils.CheckSumWrongException;
import de.mas.wiiu.jnus.utils.HashUtil; import de.mas.wiiu.jnus.utils.HashUtil;
import de.mas.wiiu.jnus.utils.PipedInputStreamWithException;
import de.mas.wiiu.jnus.utils.StreamUtils; import de.mas.wiiu.jnus.utils.StreamUtils;
import de.mas.wiiu.jnus.utils.Utils; import de.mas.wiiu.jnus.utils.Utils;
import lombok.extern.java.Log; import lombok.extern.java.Log;
@ -101,7 +102,9 @@ public class NUSDecryption extends AESDecryption {
int readTotal = 0; int readTotal = 0;
while (readTotal < toRead) { while (readTotal < toRead) {
int res = inputStream.read(data, readTotal, toRead - readTotal); int res = inputStream.read(data, readTotal, toRead - readTotal);
StreamUtils.checkForException(inputStream);
if (res < 0) { if (res < 0) {
// This should NEVER happen.
throw new IOException(); throw new IOException();
} }
readTotal += res; readTotal += res;