if (len > toRead) {
len = (int)toRead;
}
c = in.read(getCurrentChunk(), chunkOffset, len);
} catch (IOException ex) {
throw new StreamCopyException(StreamCopyException.READ, ex);
}
if (c == -1) {
break;
}
read += c;
toRead -= c;
chunkOffset += c;
if (chunkOffset == chunkSize) {
chunkIndex++;
chunkOffset = 0;
if (chunkIndex == chunks.length) {
FileOutputStream fileOutputStream;
try {
fileOutputStream = switchToTempFile();
} catch (IOException ex) {
throw new StreamCopyException(StreamCopyException.WRITE, ex);
}
byte[] buf = new byte[4096];
while (true) {
int c2;
try {
c2 = in.read(buf, 0, (int)Math.min(toRead, 4096));
} catch (IOException ex) {
throw new StreamCopyException(StreamCopyException.READ, ex);
}
if (c2 == -1) {
break;
}
try {
fileOutputStream.write(buf, 0, c2);
} catch (IOException ex) {
throw new StreamCopyException(StreamCopyException.WRITE, ex);
}
read += c2;
toRead -= c2;
}
try {
fileOutputStream.close();
} catch (IOException ex) {
throw new StreamCopyException(StreamCopyException.WRITE, ex);
}
break;
}
}
}