InputStreams.java

  1. /*******************************************************************************
  2.  * Copyright (c) 2009, 2025 Mountainminds GmbH & Co. KG and Contributors
  3.  * This program and the accompanying materials are made available under
  4.  * the terms of the Eclipse Public License 2.0 which is available at
  5.  * http://www.eclipse.org/legal/epl-2.0
  6.  *
  7.  * SPDX-License-Identifier: EPL-2.0
  8.  *
  9.  * Contributors:
  10.  *    Evgeny Mandrikov - initial API and implementation
  11.  *
  12.  *******************************************************************************/
  13. package org.jacoco.core.internal;

  14. import java.io.ByteArrayOutputStream;
  15. import java.io.IOException;
  16. import java.io.InputStream;

  17. /**
  18.  * Utilities for {@link InputStream}s.
  19.  */
  20. public final class InputStreams {

  21.     private InputStreams() {
  22.     }

  23.     /**
  24.      * Reads all bytes from an input stream into a byte array. The provided
  25.      * {@link InputStream} is not closed by this method.
  26.      *
  27.      * @param is
  28.      *            the input stream to read from
  29.      * @return a byte array containing all the bytes from the stream
  30.      * @throws IOException
  31.      *             if an I/O error occurs
  32.      */
  33.     public static byte[] readFully(final InputStream is) throws IOException {
  34.         final byte[] buf = new byte[1024];
  35.         final ByteArrayOutputStream out = new ByteArrayOutputStream();
  36.         while (true) {
  37.             final int r = is.read(buf);
  38.             if (r == -1) {
  39.                 break;
  40.             }
  41.             out.write(buf, 0, r);
  42.         }
  43.         return out.toByteArray();
  44.     }

  45. }