[BZ-1138420] fix very inefficient algorithm to read a byte array from an InputStream