Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap a string in an input-stream?

Tags:

clojure

How can I wrap a string in an input-stream in such a way that I can test the function bellow?

(defn parse-body [body]
    (cheshire/parse-stream  (clojure.java.io/reader body) true))

(deftest test-parse-body
    (testing "read body"
        (let [body "{\"age\": 28}"]  ;; must wrap string
            (is (= (parse-body body) {:age 28}))
            )))
like image 935
user983716 Avatar asked Jul 09 '16 16:07

user983716


People also ask

How do you append data to InputStream?

Reader r = new InputStreamReader(stdout, "ASCII"); char buf = new char[4096]; int length; StringBuilder s = new StringBuilder(); while ((length = r. read(buf)) != -1) { s. append(buf, 0, length); } String str = s.

How do I change InputStream to String?

To convert an InputStream Object int to a String using this method. Instantiate the Scanner class by passing your InputStream object as parameter. Read each line from this Scanner using the nextLine() method and append it to a StringBuffer object. Finally convert the StringBuffer to String using the toString() method.

Which method is used to read a String from the input stream?

read() method reads the next byte of the data from the the input stream and returns int in the range of 0 to 255.

How do I convert a string to an input stream?

You can use StandardCharsets.UTF_8 definition instead of plain text. You could use a StringReader and convert the reader to an input stream using the solution in this other stackoverflow post. There are two ways we can convert String to InputStream in Java, You can try cactoos for that.

How do you read a string in a stream?

A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). Basic methods are –. clear() — to clear the stream. str() — to get and set string object whose content is present in stream.

What is stringstream in Java?

A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). Basic methods are –. clear() — to clear the stream.

How to get all lines from an InputStream in Java 8?

In terms of reduce, and concat it can be expressed in Java 8 as: String fromFile = new BufferedReader (new InputStreamReader (inputStream)).lines ().reduce (String::concat).get (); Here's my Java 8 based solution, which uses the new Stream API to collect all lines from an InputStream:


1 Answers

It is straightforward to construct an InputStream from a String using host interop, by converting to a byte-array first:

(defn string->stream
  ([s] (string->stream s "UTF-8"))
  ([s encoding]
   (-> s
       (.getBytes encoding)
       (java.io.ByteArrayInputStream.))))

As another stream and byte interop example, here's a function that returns a vector of the bytes produced when encoding a String to a given format:

(defn show-bytes
  [s encoding]
  (let [buf (java.io.ByteArrayOutputStream.)
        stream (string->stream s encoding)
        ;; worst case, 8 bytes per char?
        data (byte-array (* (count s) 8))
        size (.read stream data 0 (count data))]
    (.write buf data 0 size)
    (.flush buf)
    (apply vector-of :byte (.toByteArray buf))))

+user=> (string->stream "hello")
#object[java.io.ByteArrayInputStream 0x39b43d60 "java.io.ByteArrayInputStream@39b43d60"]
+user=> (isa? (class *1) java.io.InputStream)
true
+user=> (show-bytes "hello" "UTF-8")
[104 101 108 108 111]
+user=> (show-bytes "hello" "UTF-32")
[0 0 0 104 0 0 0 101 0 0 0 108 0 0 0 108 0 0 0 111]
like image 94
noisesmith Avatar answered Nov 06 '22 05:11

noisesmith