Permalink
Browse files

Added response-reader, which is similar to response-str, but returns …

…a java.io.Reader instead of a slurped String.
  • Loading branch information...
1 parent bf63b47 commit cd32d4fba7ecdaea49b62a474bd6a4861431ad96 @EugenDueck EugenDueck committed Jun 25, 2011
Showing with 18 additions and 6 deletions.
  1. +18 −6 clj_web_crawler.clj
View
@@ -70,17 +70,29 @@
(finally (.releaseConnection ~method))))
([server] (crawl server (method "/"))))
-(defn response-str
- "Returns the response from the method as a string."
+(defn response-reader
+ "Returns the response from the method as a java.io.Reader.
+
+ Should be used inside with-open to ensure the Reader is properly
+ closed."
([method]
- (slurp (.getResponseBodyAsStream method) :encoding (.getResponseCharSet method)))
+ (clojure.java.io/reader (.getResponseBodyAsStream method) :encoding (.getResponseCharSet method)))
([method client]
(let [redirect (redirect-location method)
new-method (if redirect (method redirect))]
- (if new-method
+ (if new-method
(crawl client new-method
- (response-str new-method))
- (response-str method)))))
+ (response-reader new-method))
+ (response-reader method)))))
+
+(defn response-str
+ "Returns the response from the method as a string."
+ ([method]
+ (with-open [reader (response-reader method)]
+ (slurp reader)))
+ ([method client]
+ (with-open [reader (response-reader method client)]
+ (slurp reader))))
(defmulti crawl-response (fn [server method] [(class server) (class method)]))

0 comments on commit cd32d4f

Please sign in to comment.