-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for alternative response data types *experimental*
- Loading branch information
Showing
4 changed files
with
86 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
(ns clojure.tools.nrepl.response | ||
; TODO gah! Can we restrict ourselves to java 1.6 and use javax.xml.bind.DatatypeConverter? | ||
; Or maybe just snag http://migbase64.sourceforge.net? (It's not in central) | ||
(:import (sun.misc BASE64Encoder BASE64Decoder)) | ||
(:use [clojure.core.incubator :only (-?>)])) | ||
|
||
(defn base64-encode | ||
[bytes] | ||
(.encode (BASE64Encoder.) bytes)) | ||
|
||
(defn base64-decode | ||
[string] | ||
(.decodeBuffer (BASE64Decoder.) string)) | ||
|
||
(defmulti render | ||
"" | ||
(fn [value data-type] [(type value) (keyword "clojure.tools.nrepl.response" data-type)])) | ||
|
||
(defn render->base64 | ||
[value data-types] | ||
(->> data-types | ||
(map #(-?> value (render %) base64-encode (vector %))) | ||
(remove nil?) | ||
first)) | ||
|
||
(derive ::png ::image) | ||
(derive ::jpg ::image) | ||
(derive ::jpeg ::image) | ||
(derive ::bmp ::image) | ||
|
||
(defmethod render | ||
[java.awt.image.RenderedImage ::image] | ||
[img image-format] | ||
(let [out (java.io.ByteArrayOutputStream.) | ||
; good grief: can't just use (ImageIO/write img format out) because of http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6967419 | ||
img-out (javax.imageio.stream.MemoryCacheImageOutputStream. out)] | ||
; TODO provide some way to control image resolution, format, maybe max file size (hard?) | ||
(javax.imageio.ImageIO/write img (name image-format) img-out) | ||
(doto img-out .flush .close) | ||
(.toByteArray out))) | ||
|
||
(defmethod render :default [_ _] nil) |