Created
September 25, 2016 22:15
-
-
Save DenWav/d9166abbaa455861e56285bb48659d57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static byte[] readFully(InputStream in) throws IOException { | |
// We don't know the final size, and we will only ever iterate over this list | |
LinkedList<Byte> bytes = new LinkedList<Byte>(); | |
// 16kb sounds about right, idk | |
final int SIZE = 16 * 1024; | |
byte[] b = new byte[SIZE]; | |
int read = in.read(b); | |
while (read == SIZE) { | |
for (byte a : b) { | |
bytes.add(a); | |
} | |
read = in.read(b); | |
} | |
// Finish copying the final bytes in, if there are any left | |
if (read != -1) { | |
for (int i = 0; i < read; i++) { | |
bytes.add(b[i]); | |
} | |
} | |
final byte[] finalArray = new byte[bytes.size()]; | |
int i = 0; | |
for (byte aByte : bytes) { | |
finalArray[i++] = aByte; | |
} | |
return finalArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment