Synchronize access to streams.

This commit is contained in:
Maschell 2018-12-06 16:45:01 +01:00
parent b0b678c851
commit 31becf6c8b

View File

@ -33,6 +33,7 @@ public final class StreamUtils {
}
public static byte[] getBytesFromStream(InputStream in, int size) throws IOException {
synchronized (in) {
byte[] result = new byte[size];
byte[] buffer = new byte[0x8000];
int totalRead = 0;
@ -45,8 +46,10 @@ public final class StreamUtils {
in.close();
return result;
}
}
public static int getChunkFromStream(InputStream inputStream, byte[] output, ByteArrayBuffer overflowbuffer, int BLOCKSIZE) throws IOException {
synchronized (inputStream) {
int bytesRead = -1;
int inBlockBuffer = 0;
byte[] overflowbuf = overflowbuffer.getBuffer();
@ -90,6 +93,7 @@ public final class StreamUtils {
} while (inBlockBuffer != BLOCKSIZE);
return inBlockBuffer;
}
}
public static void saveInputStreamToOutputStream(InputStream inputStream, OutputStream outputStream, long filesize) throws IOException {
try {
@ -102,6 +106,8 @@ public final class StreamUtils {
public static void saveInputStreamToOutputStreamWithHash(InputStream inputStream, OutputStream outputStream, long filesize, byte[] hash,
long expectedSizeForHash) throws IOException, CheckSumWrongException {
synchronized (inputStream) {
MessageDigest sha1 = null;
if (hash != null) {
try {
@ -153,8 +159,10 @@ public final class StreamUtils {
outputStream.close();
inputStream.close();
}
}
public static void skipExactly(InputStream in, long offset) throws IOException {
synchronized (in) {
long n = offset;
while (n != 0) {
long skipped = in.skip(n);
@ -164,5 +172,7 @@ public final class StreamUtils {
}
n -= skipped;
}
}
}
}