fix hanging on AllData with large file
authorMeteorite <alexey.meteorite@gmail.com>
Sun, 20 Aug 2017 09:31:06 +0000 (12:31 +0300)
committerMeteorite <alexey.meteorite@gmail.com>
Sun, 20 Aug 2017 09:31:06 +0000 (12:31 +0300)
src/main/java/net/pterodactylus/fcp/FcpUtils.java
src/test/java/net/pterodactylus/fcp/TempInputStreamTest.java [new file with mode: 0644]

index b809f5d..b83de58 100644 (file)
@@ -353,7 +353,7 @@ public class FcpUtils {
                                FileOutputStream fileOutputStream = null;
                                try {
                                        fileOutputStream = new FileOutputStream(tempFile);
-                                       FcpUtils.copy(originalInputStream, fileOutputStream);
+                                       FcpUtils.copy(originalInputStream, fileOutputStream, length);
                                        fileInputStream = new FileInputStream(tempFile);
                                } finally {
                                        FcpUtils.close(fileOutputStream);
diff --git a/src/test/java/net/pterodactylus/fcp/TempInputStreamTest.java b/src/test/java/net/pterodactylus/fcp/TempInputStreamTest.java
new file mode 100644 (file)
index 0000000..4e3646b
--- /dev/null
@@ -0,0 +1,54 @@
+package net.pterodactylus.fcp;\r
+\r
+import org.junit.Test;\r
+\r
+import java.io.ByteArrayInputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.util.Arrays;\r
+\r
+import static org.hamcrest.MatcherAssert.assertThat;\r
+import static org.hamcrest.Matchers.is;\r
+import static org.junit.Assert.assertArrayEquals;\r
+\r
+\r
+public class TempInputStreamTest {\r
+\r
+    private byte[] prepareArrayOfNBytes(int n) {\r
+        byte[] data = new byte[n];\r
+        for (int i = 0;  i < n;  ++i) {\r
+            data[i] = (byte)i;\r
+        }\r
+        return data;\r
+    }\r
+\r
+    private void checkTempInputStreamStoresPartOfAnotherStream(int length, int maxMemoryLength) throws IOException {\r
+        byte[] originalData = prepareArrayOfNBytes(length + 1);\r
+        InputStream anotherStream = new ByteArrayInputStream(originalData);\r
+        FcpUtils.TempInputStream cut = new FcpUtils.TempInputStream(anotherStream, length, maxMemoryLength);\r
+\r
+        // check length bytes are read from anotherStream and are accessible from cut\r
+        byte[] buffer = new byte[length];\r
+        int n = cut.read(buffer);\r
+        assertThat(n, is(length));\r
+        assertArrayEquals(Arrays.copyOf(originalData, length), buffer);\r
+        assertThat(cut.read(), is(-1)); // check end of cut stream\r
+\r
+        // check the rest of data in anotherStream is still there\r
+        n = anotherStream.read(buffer);\r
+        assertThat(n, is(1));\r
+        assertThat(buffer[0], is(originalData[originalData.length - 1]));\r
+        assertThat(anotherStream.read(), is(-1)); // check end of another stream\r
+    }\r
+\r
+    @Test\r
+    public void tempInputStreamShouldCorrectlyStorePartOfAnotherStreamInMemory() throws IOException  {\r
+        checkTempInputStreamStoresPartOfAnotherStream(1, 1);\r
+    }\r
+\r
+    @Test\r
+    public void tempInputStreamShouldCorrectlyStorePartOfAnotherStreamInFile() throws IOException  {\r
+        checkTempInputStreamStoresPartOfAnotherStream(2, 1);\r
+    }\r
+\r
+}\r