All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.resteasy.util.ReadFromStream Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package org.jboss.resteasy.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Bill Burke
 * @version $Revision: 1 $
 */
public class ReadFromStream
{
   /**
    * Stuff the contents of a InputStream into a byte buffer.  Reads until EOF (-1).
    *
    * @param bufferSize
    * @param entityStream
    * @return
    * @throws IOException
    */
   public static byte[] readFromStream(int bufferSize, InputStream entityStream)
           throws IOException
   {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();

      byte[] buffer = new byte[bufferSize];
      int wasRead = 0;
      do
      {
         wasRead = entityStream.read(buffer);
         if (wasRead > 0)
         {
            baos.write(buffer, 0, wasRead);
         }
      } while (wasRead > -1);
      return baos.toByteArray();
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy