Details
-
Type:
Defect
-
Status:
Closed
-
Resolution: Completed
-
Affects Version/s: None
-
Fix Version/s: Release 1.2
-
Component/s: None
-
Labels:None
-
Approval:Ok
Description
In clojure.java.io, IOFactory is extended to java.net.Sockets. The make-input-stream method is implemented correctly, but the make-output-stream method is missing. Actually, there is another key in the map named :output-stream. That is probably some old code lying around that didn't get refactored.
How to reproduce
(use 'clojure.java.io)
(writer (java.net.Socket. "google.com" 80))
When evaluated, the following is thrown: java.lang.IllegalArgumentException: Cannot open <#<Socket Socket[addr=google.com/66.102.13.147,port=80,localport=55335]>> as an OutputStream. (NO_SOURCE_FILE:0)
Any host/port combination that is open will do. I chose Google's HTTP server as an arbitrary example of an open port.
Expected Output
An instance of java.io.BufferedWriter.
Affected Versions
All versions of Clojure 1.2 since commit cd8fc7c11213fbe99355 "promote clojure.contrib.io to clojure.java.io (with api changes)" by Stuart Halloway.
Group Discussions
I mentioned this on the Clojure mail list at the end of this thread: http://groups.google.com/group/clojure/browse_thread/thread/898f5dbe14057277/8fe69b1d2104f8b4?lnk=gst&q=code+review#8fe69b1d2104f8b4
Proposed Solution
I signed the CA and posted it in the middle of June, but my name has not appeared on the list of contributors yet. I take the liberty of demonstrating how simple the fix should be:
<pre>diff --git a/src/clj/clojure/java/io.clj b/src/clj/clojure/java/io.clj
index 4d6c551..0151482 100644
— a/src/clj/clojure/java/io.clj
+++ b/src/clj/clojure/java/io.clj
@@ -260,7 +260,7 @@
(extend Socket
IOFactory
(assoc default-streams-impl
:make-input-stream (fn [^Socket x opts] (.getInputStream x))
- :output-stream (fn [^Socket x opts] (output-stream (.getOutputStream x) opt
+ :make-output-stream (fn [^Socket x opts] (.getOutputStream x))))</pre>
Possible Unit Tests
A simple test that demonstrates this bug would be to check that the result of clojure.java.io/writer returns a java.net.Writer. Preferably, there should be unit tests for all the classes IOFactory is extended to.
Converted from http://www.assembla.com/spaces/clojure/tickets/404
Attachments:
bug404_fix01_raek.diff - https://www.assembla.com/spaces/clojure/documents/d4GJvSONmr367JeJe5cbLA/download/d4GJvSONmr367JeJe5cbLA
0404-socket-with-opts.patch - https://www.assembla.com/spaces/clojure/documents/beVUnwPvOr3780eJe5cbLr/download/beVUnwPvOr3780eJe5cbLr