content: package me.n1ar4.clazz.obfuscator.utils;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.List;
  
  public class IOUtils {
      private static final int DEFAULT_BUFFER_SIZE = 8192;
      private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
  
      public static byte[] readAllBytes(InputStream is) throws IOException {
          return readNBytes(is, Integer.MAX_VALUE);
      }
  
      public static byte[] readNBytes(InputStream is, int len) throws IOException {
          if (len < 0) {
              throw new IllegalArgumentException("len < 0");
          }
          List<byte[]> bufs = null;
          byte[] result = null;
          int total = 0;
          int remaining = len;
          int n;
          do {
              byte[] buf = new byte[Math.min(remaining, DEFAULT_BUFFER_SIZE)];
              int nread = 0;
              while ((n = is.read(buf, nread,
                      Math.min(buf.length - nread, remaining))) > 0) {
                  nread += n;
                  remaining -= n;
              }
  
              if (nread > 0) {
                  if (MAX_BUFFER_SIZE - total < nread) {
                      throw new OutOfMemoryError("Required array size too large");
                  }
                  total += nread;
                  if (result == null) {
                      result = buf;
                  } else {
                      if (bufs == null) {
                          bufs = new ArrayList<>();
                          bufs.add(result);
                      }
                      bufs.add(buf);
                  }
              }
          } while (n == 0 && remaining > 0);
  
          if (bufs == null) {
              if (result == null) {
                  return new byte[0];
              }
              return result.length == total ?
                      result : Arrays.copyOf(result, total);
          }
  
          result = new byte[total];
          int offset = 0;
          remaining = total;
          for (byte[] b : bufs) {
              int count = Math.min(b.length, remaining);
              System.arraycopy(b, 0, result, offset, count);
              offset += count;
              remaining -= count;
          }
  
          return result;
      }
  }
  