It looks like you're reading one byte at a time. Why not create a buffer and
read up to 1024 bytes at a time?
(defn write-file [ins outs]
(let [buffer (make-array Byte/TYPE 1024)]
(loop [len (.read ins buffer)]
(if (not (neg? len))
(do
(.write outs buffer 0 len)
(recur (.read ins buffer)))))
(if ins
(.close ins))
(if outs
(.close outs))))
On Fri, May 15, 2009 at 11:10 AM, Christopher Wilson <
[email protected]> wrote:
>
> Hi there,
>
> I'm working on a project that involves transferring large files over
> the network (100+MB) and wondering what would work better. I'm newer
> to Java than I am to lisp, so I've just grabbed the most obvious
> things from the API that I thought could possibly work:
>
> (ns misc-ports
> (:import (java.io BufferedReader InputStreamReader BufferedOutputStream
> PrintWriter FileInputStream BufferedOutputStream
> FileOutputStream)
> (java.net Socket ServerSocket)))
>
> (defn net-to-file
> "Listen on a port, when accepted dump data to named incoming file."
> [port filename]
> (with-open [sock (.accept (ServerSocket. port))
> ins (BufferedReader. (InputStreamReader. (.getInputStream
> sock)))
> outf (FileOutputStream. filename)]
> (loop [c (.read ins)]
> (when-not (== -1 c)
> (.write outf c)
> (recur (.read ins))))))
>
> (defn file-to-net
> "Take the name of a file and a waiting ip and port, send named file
> on socket."
> [filename ipaddr port]
> (with-open [sock (Socket. ipaddr port)
> outs (BufferedOutputStream. (.getOutputStream sock))
> ins (FileInputStream. filename)]
> (loop [c (.read ins)]
> (when-not (== -1 c)
> (.write outs c)
> (recur (.read ins))))))
>
> But it turns out that this is rather slow. What would be some methods
> to speed this up?
>
> Thanks!
>
> --
> Chris
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---