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