Both Writable and Readable streams will buffer data on an internal
object which can be retrieved from _writableState.getBuffer()
or
_readableState.buffer
, respectively.
The amount of data that will potentially be buffered depends on the
highWaterMark
option which is passed into the constructor.
Buffering in Readable streams happens when the implementation calls
stream.push(chunk)
. If the consumer of the Stream does not
call stream.read()
, then the data will sit in the internal
queue until it is consumed.
Buffering in Writable streams happens when the user calls
stream.write(chunk)
repeatedly, even when it returns false
.
The purpose of streams, especially with the stream.pipe()
method, is to
limit the buffering of data to acceptable levels, so that sources and
destinations of varying speed will not overwhelm the available memory.
(FileInputStream. path ?options )
-> ReadableStreamfs.createReadStream
start
and end
values to read a range of bytes from the file instead of the entire file.
path
: string | Uri | Fileoptions
: optional map
:flags
^string "r"
:encoding
^string "utf8"
""
to return raw buffers instead of strings:fd
^int nil
:mode
^int 0o666
:autoClose
^bool true
:start
^int 0
:end
^int Infinity
"open"
, returns file-descriptor(FileInputStream. 'sample.txt' {start: 90, end: 99})
(FileOutputStream. path ?options )
-> WritableStreamfs.createWriteStream
path
: string | Uri | Fileoptions
: optional map
:append
: ^boolean false
:flags to "a"
:flags
^string "w"
:append true
:defaultEncoding
^string "utf8"
:fd
^int nil
:start
^int 0
:autoClose
^boolean true
:mode
^int 0o666
"open"
event, returns file-descriptor(DuplexStream {options})
stream.Duplex
is an abstract class designed to be extended with an underlying implementation of the stream._read(size)
and stream._write(chunk, encoding, callback)
methods as you would with a Readable or Writable stream class.stream.Duplex
is passed to both the Readable & Writable constructors:read
: fn
:write
: fn
:allowHalfOpen
: Boolean
true
false
, automatically end the readable side when the Writable side ends & vice versa:objectMode
: Boolean
false
stream.read(n)
returns a single value instead of a Buffer of size n.stream.write(anyObj)
can write arbitrary data instead of only Buffer
/ String
data.:readableObjectMode
: Boolean
false
:objectMode
:writableObjectMode
: Boolean
false
:objectMode
(TransformStream {options})
_read
& _write
methods, but instead you must implement a _transform
method & and an optional _flush
method:transform
: fn
stream._transform()
, see below:flush
: fn
stream._flush
, see below"finish"
stream.end()
is called & all chunks have been processed by the transform function"end"
stream._flush()
stream._flush(cb)
Zlib
compression stream will store up some internal state so that it can optimally compress the output. At the end, however, it needs to do the best it can with what is left, so that the data will be complete._flush()
is called after all written data is consumed but before emitting the "end"
event. Call transform.push(chunk)
as appropriate and call the cb when complete.stream._transform(chunk, encoding, cb)
chunk
: Buffer|String
:decodeStrings
option was set to false
(see implementing writables)encoding
: string
cb
: fn
_transform()
should do whatever has to be done in this specific Transform class, to handle the bytes being written, and pass them off to the readable portion of the interface. Do asynchronous I/O, process things, and so on.(spit "foo.ednl" "{:a 1}\n{:a 2}\n{:a 3}\n")
(defn ednlParseStream []
(let [sb (StringBuffer.)
decoder (new StringDecoder "utf8")
xf (fn [chunk enc cb]
(this-as this
(.append sb (.write decoder chunk))
(let [lines (.split (.toString sb) #"\r?\n")
_ (.set sb (.pop lines))]
(doseq [line lines]
(let [obj (try
(cljs.reader/read-string line)
(catch js/Error e (throw e )))]
(.push this obj)))
(cb))))
opts {:readableObjectMode true
:transform xf}]
(TransformStream opts)))
(def parser (ednlParseStream))
(def fstream (FileInputStream "foo.ednl"))
(.pipe fstream parser )
(.read parser) ;=> {:a 1}
(.read parser) ;=> {:a 2}
(BufferReadStream buffer ?{options})
-> ReadableStream:read
fn is provided.
; pass "" as encoding to get raw buffer
(def photo (slurp "example.jpeg" :encoding ""))
(defn run-server []
(let [handler (fn [request response]
(.writeHead response 200 "OK"
#js{"Content-Type" "image/jpeg"
"Content-Disposition" "inline; filename=example.jpeg"
"Content-Length" (.-length photo)})
(.pipe (BufferReadStream photo) response))
server (.createServer http handler)]
(.listen server 8080)
(println "server running on port 8080")
server))
(def server (run-server))
(BufferWriteStream callback ?{options})
-> WriteStream:write
fn is provided.
callback
(buffer) ->
.toBuffer
methodCan you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close