Home | History | Annotate | Download | only in io
      1 package org.bouncycastle.util.io;
      2 
      3 import java.io.ByteArrayOutputStream;
      4 import java.io.IOException;
      5 import java.io.InputStream;
      6 import java.io.OutputStream;
      7 
      8 public final class Streams
      9 {
     10     private static int BUFFER_SIZE = 512;
     11 
     12     public static void drain(InputStream inStr)
     13         throws IOException
     14     {
     15         byte[] bs = new byte[BUFFER_SIZE];
     16         while (inStr.read(bs, 0, bs.length) >= 0)
     17         {
     18         }
     19     }
     20 
     21     public static byte[] readAll(InputStream inStr)
     22         throws IOException
     23     {
     24         ByteArrayOutputStream buf = new ByteArrayOutputStream();
     25         pipeAll(inStr, buf);
     26         return buf.toByteArray();
     27     }
     28 
     29     public static byte[] readAllLimited(InputStream inStr, int limit)
     30         throws IOException
     31     {
     32         ByteArrayOutputStream buf = new ByteArrayOutputStream();
     33         pipeAllLimited(inStr, limit, buf);
     34         return buf.toByteArray();
     35     }
     36 
     37     public static int readFully(InputStream inStr, byte[] buf)
     38         throws IOException
     39     {
     40         return readFully(inStr, buf, 0, buf.length);
     41     }
     42 
     43     public static int readFully(InputStream inStr, byte[] buf, int off, int len)
     44         throws IOException
     45     {
     46         int totalRead = 0;
     47         while (totalRead < len)
     48         {
     49             int numRead = inStr.read(buf, off + totalRead, len - totalRead);
     50             if (numRead < 0)
     51             {
     52                 break;
     53             }
     54             totalRead += numRead;
     55         }
     56         return totalRead;
     57     }
     58 
     59     public static void pipeAll(InputStream inStr, OutputStream outStr)
     60         throws IOException
     61     {
     62         byte[] bs = new byte[BUFFER_SIZE];
     63         int numRead;
     64         while ((numRead = inStr.read(bs, 0, bs.length)) >= 0)
     65         {
     66             outStr.write(bs, 0, numRead);
     67         }
     68     }
     69 
     70     public static long pipeAllLimited(InputStream inStr, long limit, OutputStream outStr)
     71         throws IOException
     72     {
     73         long total = 0;
     74         byte[] bs = new byte[BUFFER_SIZE];
     75         int numRead;
     76         while ((numRead = inStr.read(bs, 0, bs.length)) >= 0)
     77         {
     78             total += numRead;
     79             if (total > limit)
     80             {
     81                 throw new StreamOverflowException("Data Overflow");
     82             }
     83             outStr.write(bs, 0, numRead);
     84         }
     85         return total;
     86     }
     87 }
     88