23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package java.nio.file; |
26 package java.nio.file; |
27 |
27 |
|
28 import java.nio.ByteBuffer; |
28 import java.nio.file.attribute.*; |
29 import java.nio.file.attribute.*; |
29 import java.nio.file.spi.FileSystemProvider; |
30 import java.nio.file.spi.FileSystemProvider; |
30 import java.nio.file.spi.FileTypeDetector; |
31 import java.nio.file.spi.FileTypeDetector; |
|
32 import java.nio.channels.FileChannel; |
31 import java.nio.channels.SeekableByteChannel; |
33 import java.nio.channels.SeekableByteChannel; |
32 import java.io.Closeable; |
34 import java.io.Closeable; |
33 import java.io.InputStream; |
35 import java.io.InputStream; |
34 import java.io.OutputStream; |
36 import java.io.OutputStream; |
35 import java.io.Reader; |
37 import java.io.Reader; |
2943 return copy(in, out); |
2945 return copy(in, out); |
2944 } |
2946 } |
2945 } |
2947 } |
2946 |
2948 |
2947 /** |
2949 /** |
2948 * Read all the bytes from an input stream. The {@code initialSize} |
|
2949 * parameter indicates the initial size of the byte[] to allocate. |
|
2950 */ |
|
2951 private static byte[] read(InputStream source, int initialSize) |
|
2952 throws IOException |
|
2953 { |
|
2954 int capacity = initialSize; |
|
2955 byte[] buf = new byte[capacity]; |
|
2956 int nread = 0; |
|
2957 int rem = buf.length; |
|
2958 int n; |
|
2959 // read to EOF which may read more or less than initialSize (eg: file |
|
2960 // is truncated while we are reading) |
|
2961 while ((n = source.read(buf, nread, rem)) > 0) { |
|
2962 nread += n; |
|
2963 rem -= n; |
|
2964 assert rem >= 0; |
|
2965 if (rem == 0) { |
|
2966 // need larger buffer |
|
2967 int newCapacity = capacity << 1; |
|
2968 if (newCapacity < 0) { |
|
2969 if (capacity == Integer.MAX_VALUE) |
|
2970 throw new OutOfMemoryError("Required array size too large"); |
|
2971 newCapacity = Integer.MAX_VALUE; |
|
2972 } |
|
2973 rem = newCapacity - capacity; |
|
2974 buf = Arrays.copyOf(buf, newCapacity); |
|
2975 capacity = newCapacity; |
|
2976 } |
|
2977 } |
|
2978 return (capacity == nread) ? buf : Arrays.copyOf(buf, nread); |
|
2979 } |
|
2980 |
|
2981 /** |
|
2982 * Read all the bytes from a file. The method ensures that the file is |
2950 * Read all the bytes from a file. The method ensures that the file is |
2983 * closed when all bytes have been read or an I/O error, or other runtime |
2951 * closed when all bytes have been read or an I/O error, or other runtime |
2984 * exception, is thrown. |
2952 * exception, is thrown. |
2985 * |
2953 * |
2986 * <p> Note that this method is intended for simple cases where it is |
2954 * <p> Note that this method is intended for simple cases where it is |
3001 * In the case of the default provider, and a security manager is |
2969 * In the case of the default provider, and a security manager is |
3002 * installed, the {@link SecurityManager#checkRead(String) checkRead} |
2970 * installed, the {@link SecurityManager#checkRead(String) checkRead} |
3003 * method is invoked to check read access to the file. |
2971 * method is invoked to check read access to the file. |
3004 */ |
2972 */ |
3005 public static byte[] readAllBytes(Path path) throws IOException { |
2973 public static byte[] readAllBytes(Path path) throws IOException { |
3006 long size = size(path); |
2974 try (FileChannel fc = FileChannel.open(path)) { |
3007 if (size > (long)Integer.MAX_VALUE) |
2975 long size = fc.size(); |
3008 throw new OutOfMemoryError("Required array size too large"); |
2976 if (size > (long)Integer.MAX_VALUE) |
3009 |
2977 throw new OutOfMemoryError("Required array size too large"); |
3010 try (InputStream in = newInputStream(path)) { |
2978 |
3011 return read(in, (int)size); |
2979 byte[] arr = new byte[(int)size]; |
|
2980 ByteBuffer bb = ByteBuffer.wrap(arr); |
|
2981 while (bb.hasRemaining()) { |
|
2982 if (fc.read(bb) < 0) { |
|
2983 // truncated |
|
2984 break; |
|
2985 } |
|
2986 } |
|
2987 |
|
2988 int nread = bb.position(); |
|
2989 return (nread == size) ? arr : Arrays.copyOf(arr, nread); |
3012 } |
2990 } |
3013 } |
2991 } |
3014 |
2992 |
3015 /** |
2993 /** |
3016 * Read all lines from a file. This method ensures that the file is |
2994 * Read all lines from a file. This method ensures that the file is |