Optimize the check for Exceptions on the PipedInputStreamWithException. Now everything is checked (properly) on every action do try to do on the stream.

This commit is contained in:
Maschell 2019-04-30 15:45:38 +02:00
parent 62b87fe925
commit 010e349af9
4 changed files with 45 additions and 16 deletions

View File

@ -30,7 +30,6 @@ import de.mas.wiiu.jnus.interfaces.FSTDataProvider;
import de.mas.wiiu.jnus.interfaces.HasNUSTitle;
import de.mas.wiiu.jnus.interfaces.NUSDataProvider;
import de.mas.wiiu.jnus.utils.CheckSumWrongException;
import de.mas.wiiu.jnus.utils.StreamUtils;
import de.mas.wiiu.jnus.utils.Utils;
import de.mas.wiiu.jnus.utils.cryptography.NUSDecryption;
import lombok.Getter;
@ -143,7 +142,6 @@ public class FSTDataProviderNUSTitle implements FSTDataProvider, HasNUSTitle {
int readTotal = 0;
while (readTotal < toRead) {
int res = in.read(data, readTotal, toRead - readTotal);
StreamUtils.checkForException(in);
if (res < 0) {
// This should NEVER happen.
throw new IOException();

View File

@ -44,6 +44,11 @@ public class PipedInputStreamWithException extends PipedInputStream implements I
synchronized (lock) {
closed = true;
}
try {
checkForException();
} catch (Exception e) {
throw new IOException(e);
}
}
public void throwException(Exception e) {
@ -61,6 +66,46 @@ public class PipedInputStreamWithException extends PipedInputStream implements I
return isClosed;
}
@Override
public int read(byte b[], int off, int len) throws IOException {
try {
checkForException();
} catch (Exception e) {
throw new IOException(e);
}
return super.read(b, off, len);
}
@Override
public int read() throws IOException {
try {
checkForException();
} catch (Exception e) {
throw new IOException(e);
}
return super.read();
}
@Override
public int available() throws IOException {
try {
checkForException();
} catch (Exception e) {
throw new IOException(e);
}
return super.available();
}
@Override
public long skip(long n) throws IOException {
try {
checkForException();
} catch (Exception e) {
throw new IOException(e);
}
return super.skip(n);
}
@Override
public void checkForException() throws Exception {
if (isClosed()) {

View File

@ -58,7 +58,6 @@ public final class StreamUtils {
curReadChunk = toRead;
}
int read = in.read(buffer, 0, curReadChunk);
StreamUtils.checkForException(in);
if (read < 0) break;
System.arraycopy(buffer, 0, result, size - toRead, read);
toRead -= read;
@ -78,7 +77,6 @@ public final class StreamUtils {
do {
try {
bytesRead = inputStream.read(overflowbuf, overflowbuffer.getLengthOfDataInBuffer(), overflowbuffer.getSpaceLeft());
StreamUtils.checkForException(inputStream);
} catch (IOException e) {
log.info(e.getMessage());
if (!e.getMessage().equals("Write end dead")) {
@ -157,7 +155,6 @@ public final class StreamUtils {
try {
do {
read = inputStream.read(buffer);
StreamUtils.checkForException(inputStream);
if (read < 0) {
break;
}
@ -209,16 +206,6 @@ 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);
}
}
}
public static void closeAll(Closeable... stream) throws IOException {
IOException exception = null;
for (Closeable cur : stream) {

View File

@ -30,7 +30,6 @@ import de.mas.wiiu.jnus.entities.content.Content;
import de.mas.wiiu.jnus.utils.ByteArrayBuffer;
import de.mas.wiiu.jnus.utils.CheckSumWrongException;
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.Utils;
import lombok.extern.java.Log;