< prev index next >

src/java.base/share/classes/sun/misc/IOUtils.java

Print this page




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /**
  27  * IOUtils: A collection of IO-related public static methods.
  28  */
  29 
  30 package sun.misc;
  31 
  32 import java.io.EOFException;
  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.util.Arrays;
  36 
  37 public class IOUtils {
  38 
  39     /**
  40      * Read up to <code>length</code> of bytes from <code>in</code>
  41      * until EOF is detected.
  42      * @param in input stream, must not be null
  43      * @param length number of bytes to read, -1 or Integer.MAX_VALUE means
  44      *        read as much as possible
  45      * @param readAll if true, an EOFException will be thrown if not enough
  46      *        bytes are read. Ignored when length is -1 or Integer.MAX_VALUE
  47      * @return bytes read
  48      * @throws IOException Any IO error or a premature EOF is detected
  49      */
  50     public static byte[] readFully(InputStream is, int length, boolean readAll)
  51             throws IOException {
  52         byte[] output = {};
  53         if (length == -1) length = Integer.MAX_VALUE;
  54         int pos = 0;
  55         while (pos < length) {
  56             int bytesToRead;
  57             if (pos >= output.length) { // Only expand when there's no room
  58                 bytesToRead = Math.min(length - pos, output.length + 1024);
  59                 if (output.length < pos + bytesToRead) {
  60                     output = Arrays.copyOf(output, pos + bytesToRead);
  61                 }
  62             } else {


  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /**
  27  * IOUtils: A collection of IO-related public static methods.
  28  */
  29 
  30 package sun.misc;
  31 
  32 import java.io.EOFException;
  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.util.Arrays;
  36 
  37 public class IOUtils {
  38 
  39     /**
  40      * Read up to <code>length</code> of bytes from <code>in</code>
  41      * until EOF is detected.
  42      * @param is input stream, must not be null
  43      * @param length number of bytes to read, -1 or Integer.MAX_VALUE means
  44      *        read as much as possible
  45      * @param readAll if true, an EOFException will be thrown if not enough
  46      *        bytes are read. Ignored when length is -1 or Integer.MAX_VALUE
  47      * @return bytes read
  48      * @throws IOException Any IO error or a premature EOF is detected
  49      */
  50     public static byte[] readFully(InputStream is, int length, boolean readAll)
  51             throws IOException {
  52         byte[] output = {};
  53         if (length == -1) length = Integer.MAX_VALUE;
  54         int pos = 0;
  55         while (pos < length) {
  56             int bytesToRead;
  57             if (pos >= output.length) { // Only expand when there's no room
  58                 bytesToRead = Math.min(length - pos, output.length + 1024);
  59                 if (output.length < pos + bytesToRead) {
  60                     output = Arrays.copyOf(output, pos + bytesToRead);
  61                 }
  62             } else {
< prev index next >