DataInputStream in = null;
in = new DataInputStream(is);
int bytesRead = 0;
int totalBytesRead = 0;
while (bytesRead != -1) {
byte[] readBytes = new byte[8192];
bytesRead = in.read(readBytes);
if (bytesRead != -1) {
totalBytesRead += bytesRead;
if (totalBytesRead <= length) {
System.arraycopy(midBytes(readBytes, 0, bytesRead), 0, allBytes, totalBytesRead - bytesRead, bytesRead);
} else {
sizeCheck = totalBytesRead - length;
System.arraycopy(midBytes(readBytes, 0, bytesRead), 0, allBytes, totalBytesRead - bytesRead, bytesRead - sizeCheck);
break;
}
}
}