src/java.base/share/classes/java/nio/file/Files.java
changeset 48353 315c690bb90b
parent 47496 66e2e3f62eb5
child 50447 3111982511ee
equal deleted inserted replaced
48352:9b700a5c4381 48353:315c690bb90b
  2953     {
  2953     {
  2954         return newBufferedWriter(path, StandardCharsets.UTF_8, options);
  2954         return newBufferedWriter(path, StandardCharsets.UTF_8, options);
  2955     }
  2955     }
  2956 
  2956 
  2957     /**
  2957     /**
  2958      * Reads all bytes from an input stream and writes them to an output stream.
       
  2959      */
       
  2960     private static long copy(InputStream source, OutputStream sink)
       
  2961         throws IOException
       
  2962     {
       
  2963         long nread = 0L;
       
  2964         byte[] buf = new byte[BUFFER_SIZE];
       
  2965         int n;
       
  2966         while ((n = source.read(buf)) > 0) {
       
  2967             sink.write(buf, 0, n);
       
  2968             nread += n;
       
  2969         }
       
  2970         return nread;
       
  2971     }
       
  2972 
       
  2973     /**
       
  2974      * Copies all bytes from an input stream to a file. On return, the input
  2958      * Copies all bytes from an input stream to a file. On return, the input
  2975      * stream will be at end of stream.
  2959      * stream will be at end of stream.
  2976      *
  2960      *
  2977      * <p> By default, the copy fails if the target file already exists or is a
  2961      * <p> By default, the copy fails if the target file already exists or is a
  2978      * symbolic link. If the {@link StandardCopyOption#REPLACE_EXISTING
  2962      * symbolic link. If the {@link StandardCopyOption#REPLACE_EXISTING
  3080             throw x;
  3064             throw x;
  3081         }
  3065         }
  3082 
  3066 
  3083         // do the copy
  3067         // do the copy
  3084         try (OutputStream out = ostream) {
  3068         try (OutputStream out = ostream) {
  3085             return copy(in, out);
  3069             return in.transferTo(out);
  3086         }
  3070         }
  3087     }
  3071     }
  3088 
  3072 
  3089     /**
  3073     /**
  3090      * Copies all bytes from a file to an output stream.
  3074      * Copies all bytes from a file to an output stream.
  3122     public static long copy(Path source, OutputStream out) throws IOException {
  3106     public static long copy(Path source, OutputStream out) throws IOException {
  3123         // ensure not null before opening file
  3107         // ensure not null before opening file
  3124         Objects.requireNonNull(out);
  3108         Objects.requireNonNull(out);
  3125 
  3109 
  3126         try (InputStream in = newInputStream(source)) {
  3110         try (InputStream in = newInputStream(source)) {
  3127             return copy(in, out);
  3111             return in.transferTo(out);
  3128         }
  3112         }
  3129     }
  3113     }
  3130 
  3114 
  3131     /**
  3115     /**
  3132      * The maximum size of array to allocate.
  3116      * The maximum size of array to allocate.