Clojure

Making a writer from a Socket with clojure.java.io/writer fails

Details

  • Type: Defect Defect
  • Status: Closed 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.

Activity

Hide
Assembla Importer added a comment -

rasmus.svensson said: Patch and test that fails for the current version and passes for the fixed one.

Show
Assembla Importer added a comment - rasmus.svensson said: Patch and test that fails for the current version and passes for the fixed one.
Hide
Assembla Importer added a comment -

rasmus.svensson said: [file:d4GJvSONmr367JeJe5cbLA]: Patch and test for iissue #404

Show
Assembla Importer added a comment - rasmus.svensson said: [file:d4GJvSONmr367JeJe5cbLA]: Patch and test for iissue #404
Hide
Assembla Importer added a comment -
Show
Assembla Importer added a comment - stu said: [file:beVUnwPvOr3780eJe5cbLr]
Hide
Assembla Importer added a comment -

stu said: second patch subsumes first and preserves options passed in via opts

Show
Assembla Importer added a comment - stu said: second patch subsumes first and preserves options passed in via opts
Hide
Assembla Importer added a comment -

rasmus.svensson said: Should the tests check for instances of Buffered(Input|Output)Stream rather than (Input|Output)Stream, too?

Show
Assembla Importer added a comment - rasmus.svensson said: Should the tests check for instances of Buffered(Input|Output)Stream rather than (Input|Output)Stream, too?
Hide
Assembla Importer added a comment -

stu said: Updating tickets (#404, #422, #382, #423)

Show
Assembla Importer added a comment - stu said: Updating tickets (#404, #422, #382, #423)

People

  • Assignee:
    Unassigned
    Reporter:
    Anonymous
Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: