From 15563cc045dea8ac09a8b4f6a2aa9366eb5b3d3b Mon Sep 17 00:00:00 2001 From: James Reeves Date: Sat, 19 Oct 2024 09:27:05 +0100 Subject: [PATCH] Move byte array impl of StreamableResponseBody The extend-protocol macro doesn't officially support non-symbol types, such as (Class/forName "[B"). The implementation of StreamableResponseBody for byte arrays is therefore moved out to the low-level extend function. --- ring-core-protocols/src/ring/core/protocols.clj | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ring-core-protocols/src/ring/core/protocols.clj b/ring-core-protocols/src/ring/core/protocols.clj index 6de7c5bb..3d706428 100644 --- a/ring-core-protocols/src/ring/core/protocols.clj +++ b/ring-core-protocols/src/ring/core/protocols.clj @@ -36,11 +36,16 @@ (io/writer output-stream :encoding charset) (io/writer output-stream))) +;; Extending primitive arrays prior to Clojure 1.12 requires using the low-level +;; extend function. +(extend (Class/forName "[B") + StreamableResponseBody + {:write-body-to-stream + (fn [body _ ^OutputStream output-stream] + (.write output-stream ^bytes body) + (.close output-stream))}) + (extend-protocol StreamableResponseBody - #_{:clj-kondo/ignore [:syntax]} (Class/forName "[B") - (write-body-to-stream [body _ ^OutputStream output-stream] - (.write output-stream ^bytes body) - (.close output-stream)) String (write-body-to-stream [body response output-stream] (doto (response-writer response output-stream)