stream-read-unsafe ( n buf stream -- count )
Factor handbook > Input and output > Streams > Stream protocol

Prev:stream-read1 ( stream -- elt )
Next:stream-read-until ( seps stream -- seq sep/f )


Vocabulary
io

Inputs and outputs
na non-negative integer
bufa c-ptr or a string
streaman input stream
countan integer


Generic word contract
Reads up to n elements from the stream. The data is stored directly into the buffer provided by buf, which must be a string (in the case of a character stream) or a byte array, specialized array, or other pointer to memory (in the case of a byte stream). There must be space in the buffer for at least n elements. Returns the number of elements read from the stream, which will be equal to n unless the end of the stream is reached. If the stream is exhausted, returns zero.

Warning
This word does not perform bounds checking on buf. Most code should use stream-read or stream-read-into instead.


Errors
Throws an error if the I/O operation fails.

Definition

GENERIC: stream-read-unsafe ( n buf stream -- count )


Methods

























M:: interactor stream-read-unsafe
( n buf interactor -- count )
n
[ 0 ] [
drop interactor interactor-read dup [ "\n" join ] when
n short [ head-slice 0 buf copy ] keep
] if-zero ;






M:: memory-stream stream-read-unsafe
( n buf stream -- count )
stream alien>> :> src buf src n memcpy
n src <displaced-alien> stream alien<< n ; inline






M:: peek-stream stream-read-unsafe ( n buf stream -- count )
stream peeked>> :> peeked peeked length :> #peeked #peeked 0
=
[ n buf stream stream>> stream-read-unsafe ] [
#peeked n >= [
peeked <reversed> n head-slice 0 buf copy peeked
[ length n - ] keep shorten n
] [
peeked <reversed> 0 buf copy 0 peeked shorten
n #peeked - :> n' stream stream>> input-port?
[ #peeked buf <displaced-alien> ]
[ buf #peeked tail-slice ] if
:> buf' n' buf' stream stream-read-unsafe #peeked +
] if
] if ;