1 package net.pterodactylus.fcp;
5 import java.io.ByteArrayInputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.util.Arrays;
10 import static org.hamcrest.MatcherAssert.assertThat;
11 import static org.hamcrest.Matchers.is;
12 import static org.junit.Assert.assertArrayEquals;
15 public class TempInputStreamTest {
17 private byte[] prepareArrayOfNBytes(int n) {
18 byte[] data = new byte[n];
19 for (int i = 0; i < n; ++i) {
25 private void checkTempInputStreamStoresPartOfAnotherStream(int length, int maxMemoryLength) throws IOException {
26 byte[] originalData = prepareArrayOfNBytes(length + 1);
27 InputStream anotherStream = new ByteArrayInputStream(originalData);
28 FcpUtils.TempInputStream cut = new FcpUtils.TempInputStream(anotherStream, length, maxMemoryLength);
30 // check length bytes are read from anotherStream and are accessible from cut
31 byte[] buffer = new byte[length];
32 int n = cut.read(buffer);
33 assertThat(n, is(length));
34 assertArrayEquals(Arrays.copyOf(originalData, length), buffer);
35 assertThat(cut.read(), is(-1)); // check end of cut stream
37 // check the rest of data in anotherStream is still there
38 n = anotherStream.read(buffer);
40 assertThat(buffer[0], is(originalData[originalData.length - 1]));
41 assertThat(anotherStream.read(), is(-1)); // check end of another stream
45 public void tempInputStreamShouldCorrectlyStorePartOfAnotherStreamInMemory() throws IOException {
46 checkTempInputStreamStoresPartOfAnotherStream(1, 1);
50 public void tempInputStreamShouldCorrectlyStorePartOfAnotherStreamInFile() throws IOException {
51 checkTempInputStreamStoresPartOfAnotherStream(2, 1);