@Test public void testDrainage() throws Exception { byte[] input = new byte[]{1, 2, 3, 4}; assertThat(new StreamDrainer(1).drain(new ByteArrayInputStream(input)), is(input)); }
public byte[] drain(InputStream inputStream) throws IOException { List<byte[]> previousBytes = new ArrayList<byte[]>(); byte[] currentArray = new byte[bufferSize]; int currentIndex = 0; int currentRead; do { currentRead = inputStream.read(currentArray, currentIndex, bufferSize - currentIndex); currentIndex += currentRead > 0 ? currentRead : 0; if (currentIndex == bufferSize) { previousBytes.add(currentArray); currentArray = new byte[bufferSize]; currentIndex = 0; } } while (currentRead != END_OF_STREAM); byte[] result = new byte[previousBytes.size() * bufferSize + currentIndex]; int arrayIndex = 0; for (byte[] previousByte : previousBytes) { System.arraycopy(previousByte, FROM_BEGINNING, result, arrayIndex++ * bufferSize, bufferSize); } System.arraycopy(currentArray, FROM_BEGINNING, result, arrayIndex * bufferSize, currentIndex); return result; }
StreamDrainer { public byte[] drain(InputStream inputStream) throws IOException { List<byte[]> previousBytes = new ArrayList<byte[]>(); byte[] currentArray = new byte[bufferSize]; int currentIndex = 0; int currentRead; do { currentRead = inputStream.read(currentArray, currentIndex, bufferSize - currentIndex); currentIndex += currentRead > 0 ? currentRead : 0; if (currentIndex == bufferSize) { previousBytes.add(currentArray); currentArray = new byte[bufferSize]; currentIndex = 0; } } while (currentRead != END_OF_STREAM); byte[] result = new byte[previousBytes.size() * bufferSize + currentIndex]; int arrayIndex = 0; for (byte[] previousByte : previousBytes) { System.arraycopy(previousByte, FROM_BEGINNING, result, arrayIndex++ * bufferSize, bufferSize); } System.arraycopy(currentArray, FROM_BEGINNING, result, arrayIndex * bufferSize, currentIndex); return result; } }
StreamDrainer { public byte[] drain(InputStream inputStream) throws IOException { List<byte[]> previousBytes = new ArrayList<byte[]>(); byte[] currentArray = new byte[bufferSize]; int currentIndex = 0; int currentRead; do { currentRead = inputStream.read(currentArray, currentIndex, bufferSize - currentIndex); currentIndex += currentRead > 0 ? currentRead : 0; if (currentIndex == bufferSize) { previousBytes.add(currentArray); currentArray = new byte[bufferSize]; currentIndex = 0; } } while (currentRead != END_OF_STREAM); byte[] result = new byte[previousBytes.size() * bufferSize + currentIndex]; int arrayIndex = 0; for (byte[] previousByte : previousBytes) { System.arraycopy(previousByte, FROM_BEGINNING, result, arrayIndex++ * bufferSize, bufferSize); } System.arraycopy(currentArray, FROM_BEGINNING, result, arrayIndex * bufferSize, currentIndex); return result; } StreamDrainer();  StreamDrainer(int bufferSize); }
StreamDrainer { public byte[] drain(InputStream inputStream) throws IOException { List<byte[]> previousBytes = new ArrayList<byte[]>(); byte[] currentArray = new byte[bufferSize]; int currentIndex = 0; int currentRead; do { currentRead = inputStream.read(currentArray, currentIndex, bufferSize - currentIndex); currentIndex += currentRead > 0 ? currentRead : 0; if (currentIndex == bufferSize) { previousBytes.add(currentArray); currentArray = new byte[bufferSize]; currentIndex = 0; } } while (currentRead != END_OF_STREAM); byte[] result = new byte[previousBytes.size() * bufferSize + currentIndex]; int arrayIndex = 0; for (byte[] previousByte : previousBytes) { System.arraycopy(previousByte, FROM_BEGINNING, result, arrayIndex++ * bufferSize, bufferSize); } System.arraycopy(currentArray, FROM_BEGINNING, result, arrayIndex * bufferSize, currentIndex); return result; } StreamDrainer();  StreamDrainer(int bufferSize); byte[] drain(InputStream inputStream); }
StreamDrainer { public byte[] drain(InputStream inputStream) throws IOException { List<byte[]> previousBytes = new ArrayList<byte[]>(); byte[] currentArray = new byte[bufferSize]; int currentIndex = 0; int currentRead; do { currentRead = inputStream.read(currentArray, currentIndex, bufferSize - currentIndex); currentIndex += currentRead > 0 ? currentRead : 0; if (currentIndex == bufferSize) { previousBytes.add(currentArray); currentArray = new byte[bufferSize]; currentIndex = 0; } } while (currentRead != END_OF_STREAM); byte[] result = new byte[previousBytes.size() * bufferSize + currentIndex]; int arrayIndex = 0; for (byte[] previousByte : previousBytes) { System.arraycopy(previousByte, FROM_BEGINNING, result, arrayIndex++ * bufferSize, bufferSize); } System.arraycopy(currentArray, FROM_BEGINNING, result, arrayIndex * bufferSize, currentIndex); return result; } StreamDrainer();  StreamDrainer(int bufferSize); byte[] drain(InputStream inputStream); static final int DEFAULT_BUFFER_SIZE; static final StreamDrainer DEFAULT; }