Skip to content
Browse files

CLJ-669: Use Java NIO to do file-to-file clojure.java.io/copy

  • Loading branch information...
1 parent 6516278 commit 8bd8e8d0f2d68e1652eaa3b08f315d4e6a573be7 @jafingerhut jafingerhut committed with stuarthalloway
Showing with 8 additions and 3 deletions.
  1. +8 −3 src/clj/clojure/java/io.clj
View
11 src/clj/clojure/java/io.clj
@@ -342,9 +342,14 @@
(do-copy in output opts)))
(defmethod do-copy [File File] [^File input ^File output opts]
- (with-open [in (FileInputStream. input)
- out (FileOutputStream. output)]
- (do-copy in out opts)))
+ (with-open [in (-> input FileInputStream. .getChannel)
+ out (-> output FileOutputStream. .getChannel)]
+ (let [sz (.size in)]
+ (loop [pos 0]
+ (let [bytes-xferred (.transferTo in pos (- sz pos) out)
+ pos (+ pos bytes-xferred)]
+ (when (< pos sz)
+ (recur pos)))))))
(defmethod do-copy [String OutputStream] [^String input ^OutputStream output opts]
(do-copy (StringReader. input) output opts))

0 comments on commit 8bd8e8d

Please sign in to comment.
Something went wrong with that request. Please try again.