Back

input-stream (clj)

(source)

function

(input-stream x & opts)
Attempts to coerce its argument into an open java.io.InputStream. Default implementations always return a java.io.BufferedInputStream. Default implementations are defined for InputStream, File, URI, URL, Socket, byte array, and String arguments. If the argument is a String, it tries to resolve it first as a URI, then as a local file name. URIs with a 'file' protocol are converted to local file names. Should be used inside with-open to ensure the InputStream is properly closed.

Examples

metosin/compojure-api
(ns examples.thingie
  (:require [ring.util.http-response :refer :all]
            [compojure.api.sweet :refer :all]
            [compojure.api.upload :refer :all]
            [schema.core :as s]
            ring.swagger.json-schema-dirty
            ring.middleware.multipart-params.byte-array
            [examples.pizza :refer [pizza-routes Pizza]]
            [examples.ordered :refer [ordered-routes]]
            [examples.dates :refer [date-routes]]
            [clojure.java.io :as io])
  (:import (org.joda.time DateTime)
           (java.io File)))

      (GET "/file" []
        :summary "file download"
        :return File
        :produces ["image/png"]
        (-> (io/resource "screenshot.png")
            (io/input-stream)
            (ok)
            (header "Content-Type" "image/png"))))
cognitect-labs/aws-api
(ns s3-examples
  (:require [clojure.core.async :as a]
            [clojure.spec.alpha :as s]
            [clojure.spec.gen.alpha :as gen]
            [clojure.java.io :as io]
            [clojure.repl :as repl]
            [cognitect.aws.client.api :as aws]))

  (aws/invoke s3 {:op :PutObject :request {:Bucket bucket-name :Key "hello.txt"
                                           :Body (io/input-stream (.getBytes "Oh hai!"))}})
nextjournal/clerk-demo
;; # 🏞 Automatic Image Support
^{:nextjournal.clerk/visibility #{:hide-ns}}
(ns images
  (:require [nextjournal.clerk :as clerk]
            [clojure.java.io :as io])
  (:import [java.net URL]
           [java.nio.file Paths Files]
           [java.awt.image BufferedImage]
           [javax.imageio ImageIO]))

(with-open [in (io/input-stream raw-image)]
  (ImageIO/read in))
babashka/pod-registry
(require '[babashka.pods :as pods]
         '[clojure.java.io :as io])

   ;; multiple sources
   [(io/file "single-file")          ;; file
    (io/file "directory")            ;; directory
    ["content🚀" {:filename "string"}]    ;; string data
    [(byte-array [1 2 3 4])
     {:filename "byte-array"}]           ;; byte-array data
    [(io/input-stream (byte-array [0xf0 0x9f 0x9a 0x80 0x00]))
     {:filename "input-stream"
      :size 5
      }]                             ;; input stream
    ]
   "remote-path"                     ;; remote path
   {:session session                 ;; options
    :recurse? true})
riverford/durable-ref
(ns riverford.durable-ref.format.json.cheshire
  (:require [cheshire.core :as cheshire]
            [riverford.durable-ref.core :as dref]
            [clojure.java.io :as io])
  (:import (java.io ByteArrayOutputStream)
           (java.util.zip GZIPOutputStream GZIPInputStream)))

(defmethod dref/deserialize "json.zip"
  [in _ opts]
  (let [read-opts (-> opts :format :json :cheshire :read-opts)]
    (with-open [in (io/input-stream in)
                gzipi (GZIPInputStream. in)
                rdr (io/reader gzipi)]
      (cheshire/parse-stream
        rdr
        (:key-fn read-opts identity)
        (:array-coerce-fn read-opts (constantly []))))))