content: package com.alibaba.compileflow.engine.core.infrastructure.utils;
  
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  /**
   * @author yusu
   */
  public class IOUtils {
  
      private static final int EOF = -1;
  
      private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
  
      public static byte[] toByteArray(final InputStream input, final long size) throws IOException {
          if (size > Integer.MAX_VALUE) {
              throw new IllegalArgumentException("Size cannot be greater than Integer max value: " + size);
          }
  
          return toByteArray(input, (int) size);
      }
  
      public static byte[] toByteArray(final InputStream input) throws IOException {
          try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) {
              copy(input, output);
              return output.toByteArray();
          }
      }
  
      public static byte[] toByteArray(final InputStream input, final int size) throws IOException {
  
          if (size < 0) {
              throw new IllegalArgumentException("Size must be equal or greater than zero: " + size);
          }
  
          if (size == 0) {
              return new byte[0];
          }
  
          final byte[] data = new byte[size];
          int offset = 0;
          int read;
  
          while (offset < size && (read = input.read(data, offset, size - offset)) != EOF) {
              offset += read;
          }
  
          if (offset != size) {
              throw new IOException("Unexpected read size. current: " + offset + ", expected: " + size);
          }
  
          return data;
      }
  
      public static int copy(final InputStream input, final OutputStream output) throws IOException {
          final long count = copyLarge(input, output);
          if (count > Integer.MAX_VALUE) {
              return -1;
          }
          return (int) count;
      }
  
      public static long copy(final InputStream input, final OutputStream output, final int bufferSize)
              throws IOException {
          return copyLarge(input, output, new byte[bufferSize]);
      }
  
      public static long copyLarge(final InputStream input, final OutputStream output)
              throws IOException {
          return copy(input, output, DEFAULT_BUFFER_SIZE);
      }
  
      public static long copyLarge(final InputStream input, final OutputStream output, final byte[] buffer)
              throws IOException {
          long count = 0;
          int n;
          while (EOF != (n = input.read(buffer))) {
              output.write(buffer, 0, n);
              count += n;
          }
          return count;
      }
  
  }
  