clojure.java.io/copy incorrectly stops copying on 0 byte read

Description

Reported at https://ask.clojure.org/index.php/8450/strange-behavior-with-clojure-java-io-copy

Actual copying is implemented by reading bytes from an InputStream and writing to an OutputStream. The copy loop terminates if the bytes read is not >0. However, 0 is a legal return value (only -1 indicates EOF).

Approach: Termination condition should be >= 0, not > 0.

Patch: clj-2533-2.patch

Environment

None

Activity

Show:
Sean Corfield
August 26, 2019, 4:32 AM

Two things:

  1. Several people seem to think that you’ll only get zero back if the buffer has size zero and that you’ll potentially get an infinite loop if you continuing reading after a zero size result (based on the Java API docs, I’m inclined to agree).

  2. Is this code correct in the patch?

Should the (is (instance? ,,,)) line be inside the try?

Also, the resource changes seem unrelated to this ticket so they presumably should not be part of this patch?

Alex Miller
August 26, 2019, 3:09 PM

Re 2, it's not correct, and it's actually a separate patch for CLJ-2431, fixed.

Assignee

Unassigned

Reporter

Alex Miller

Labels

Approval

Prescreened

Patch

Code

Affects versions

Priority

Minor