src/share/classes/java/nio/file/Files.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.nio.file;
  27 
  28 import java.nio.ByteBuffer;
  29 import java.nio.file.attribute.*;
  30 import java.nio.file.spi.FileSystemProvider;
  31 import java.nio.file.spi.FileTypeDetector;

  32 import java.nio.channels.FileChannel;
  33 import java.nio.channels.SeekableByteChannel;
  34 import java.io.Closeable;
  35 import java.io.InputStream;
  36 import java.io.OutputStream;
  37 import java.io.Reader;
  38 import java.io.Writer;
  39 import java.io.BufferedReader;
  40 import java.io.BufferedWriter;

  41 import java.io.InputStreamReader;
  42 import java.io.OutputStreamWriter;
  43 import java.io.IOException;
  44 import java.io.UncheckedIOException;
  45 import java.util.*;
  46 import java.util.function.BiPredicate;
  47 import java.util.stream.CloseableStream;
  48 import java.util.stream.DelegatingStream;
  49 import java.util.stream.Stream;
  50 import java.util.stream.StreamSupport;
  51 import java.security.AccessController;
  52 import java.security.PrivilegedAction;
  53 import java.nio.charset.Charset;
  54 import java.nio.charset.CharsetDecoder;
  55 import java.nio.charset.CharsetEncoder;
  56 
  57 /**
  58  * This class consists exclusively of static methods that operate on files,
  59  * directories, or other types of files.
  60  *


2977      *          the path to the file
2978      *
2979      * @return  a byte array containing the bytes read from the file
2980      *
2981      * @throws  IOException
2982      *          if an I/O error occurs reading from the stream
2983      * @throws  OutOfMemoryError
2984      *          if an array of the required size cannot be allocated, for
2985      *          example the file is larger that {@code 2GB}
2986      * @throws  SecurityException
2987      *          In the case of the default provider, and a security manager is
2988      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
2989      *          method is invoked to check read access to the file.
2990      */
2991     public static byte[] readAllBytes(Path path) throws IOException {
2992         try (FileChannel fc = FileChannel.open(path)) {
2993             long size = fc.size();
2994             if (size > (long)Integer.MAX_VALUE)
2995                 throw new OutOfMemoryError("Required array size too large");
2996 
2997             byte[] arr = new byte[(int)size];
2998             ByteBuffer bb = ByteBuffer.wrap(arr);
2999             while (bb.hasRemaining()) {
3000                 if (fc.read(bb) < 0) {
3001                     // truncated
3002                     break;
3003                 }



3004             }
3005 
3006             int nread = bb.position();
3007             return (nread == size) ? arr : Arrays.copyOf(arr, nread);
3008         }
3009     }
3010 
3011     /**
3012      * Read all lines from a file. This method ensures that the file is
3013      * closed when all bytes have been read or an I/O error, or other runtime
3014      * exception, is thrown. Bytes from the file are decoded into characters
3015      * using the specified charset.
3016      *
3017      * <p> This method recognizes the following as line terminators:
3018      * <ul>
3019      *   <li> <code>&#92;u000D</code> followed by <code>&#92;u000A</code>,
3020      *     CARRIAGE RETURN followed by LINE FEED </li>
3021      *   <li> <code>&#92;u000A</code>, LINE FEED </li>
3022      *   <li> <code>&#92;u000D</code>, CARRIAGE RETURN </li>
3023      * </ul>
3024      * <p> Additional Unicode line terminators may be recognized in future
3025      * releases.
3026      *
3027      * <p> Note that this method is intended for simple cases where it is




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.nio.file;
  27 

  28 import java.nio.file.attribute.*;
  29 import java.nio.file.spi.FileSystemProvider;
  30 import java.nio.file.spi.FileTypeDetector;
  31 import java.nio.channels.Channels;
  32 import java.nio.channels.FileChannel;
  33 import java.nio.channels.SeekableByteChannel;
  34 import java.io.Closeable;
  35 import java.io.InputStream;
  36 import java.io.OutputStream;
  37 import java.io.Reader;
  38 import java.io.Writer;
  39 import java.io.BufferedReader;
  40 import java.io.BufferedWriter;
  41 import java.io.ByteArrayOutputStream;
  42 import java.io.InputStreamReader;
  43 import java.io.OutputStreamWriter;
  44 import java.io.IOException;
  45 import java.io.UncheckedIOException;
  46 import java.util.*;
  47 import java.util.function.BiPredicate;
  48 import java.util.stream.CloseableStream;
  49 import java.util.stream.DelegatingStream;
  50 import java.util.stream.Stream;
  51 import java.util.stream.StreamSupport;
  52 import java.security.AccessController;
  53 import java.security.PrivilegedAction;
  54 import java.nio.charset.Charset;
  55 import java.nio.charset.CharsetDecoder;
  56 import java.nio.charset.CharsetEncoder;
  57 
  58 /**
  59  * This class consists exclusively of static methods that operate on files,
  60  * directories, or other types of files.
  61  *


2978      *          the path to the file
2979      *
2980      * @return  a byte array containing the bytes read from the file
2981      *
2982      * @throws  IOException
2983      *          if an I/O error occurs reading from the stream
2984      * @throws  OutOfMemoryError
2985      *          if an array of the required size cannot be allocated, for
2986      *          example the file is larger that {@code 2GB}
2987      * @throws  SecurityException
2988      *          In the case of the default provider, and a security manager is
2989      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
2990      *          method is invoked to check read access to the file.
2991      */
2992     public static byte[] readAllBytes(Path path) throws IOException {
2993         try (FileChannel fc = FileChannel.open(path)) {
2994             long size = fc.size();
2995             if (size > (long)Integer.MAX_VALUE)
2996                 throw new OutOfMemoryError("Required array size too large");
2997 
2998             try (InputStream fis = Channels.newInputStream(fc);
2999                  ByteArrayOutputStream bos = new ByteArrayOutputStream((int)size) {
3000                      @Override
3001                      public byte[] toByteArray() {
3002                          return (buf.length == count) ? buf : Arrays.copyOf(buf, count);

3003                      }
3004                  }) {
3005                 copy(fis, bos);
3006                 return bos.toByteArray();
3007             }



3008         }
3009     }
3010 
3011     /**
3012      * Read all lines from a file. This method ensures that the file is
3013      * closed when all bytes have been read or an I/O error, or other runtime
3014      * exception, is thrown. Bytes from the file are decoded into characters
3015      * using the specified charset.
3016      *
3017      * <p> This method recognizes the following as line terminators:
3018      * <ul>
3019      *   <li> <code>&#92;u000D</code> followed by <code>&#92;u000A</code>,
3020      *     CARRIAGE RETURN followed by LINE FEED </li>
3021      *   <li> <code>&#92;u000A</code>, LINE FEED </li>
3022      *   <li> <code>&#92;u000D</code>, CARRIAGE RETURN </li>
3023      * </ul>
3024      * <p> Additional Unicode line terminators may be recognized in future
3025      * releases.
3026      *
3027      * <p> Note that this method is intended for simple cases where it is