You can replace all of this with:

  byte[] input = loader.getCachedBytes(Integer.MAX_VALUE);

It handles reading from a stream into a byte array if its large for you. But throws LargeObjectException if the array cannot be allocated, or if the object size exceeds the array size.