 M: object underlying-handle underlying-port handle>> ;
 
 ! Fast-path optimization
-USING: hints strings io.encodings.utf8 io.encodings.ascii
-io.encodings.private ;
 
 HINTS: decoder-read-until { string input-port utf8 } { string input-port ascii } ;
 

    dup check-disposed
    dup wait-to-read [ drop f ] [ buffer>> buffer-pop ] if ; inline

: read-step ( count port -- byte-array/f )
    {
        { [ over 0 = ] [ 2drop f ] }
        { [ dup wait-to-read ] [ 2drop f ] }
        [ buffer>> buffer-read ]
    } cond ;

: prepare-read ( count stream -- count stream )
    dup check-disposed [ 0 max >fixnum ] dip ; inline

M: input-port stream-read-partial ( max stream -- byte-array/f )
    prepare-read read-step ;

: read-loop ( count port accum -- )
    pick over length - dup 0 > [
        pick read-step dup [
            append! read-loop
        ] [
            2drop 2drop
        ] if
    ] [
        2drop 2drop
    ] if ;

M: input-port stream-read
    prepare-read
    2dup read-step dup [
        pick over length > [
            pick <byte-vector>
            [ push-all ] keep
            [ read-loop ] keep
            B{ } like
        ] [ 2nip ] if
    ] [ 2nip ] if ;

: read-until-step ( separators port -- string/f separator/f )
    dup wait-to-read [ 2drop f f ] [ buffer>> buffer-until ] if ;

! Copyright (C) 2005, 2010 Slava Pestov, Doug Coleman
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien alien.c-types alien.data assocs
byte-arrays byte-vectors classes combinators continuations
destructors dlists fry generic grouping hints io io.backend
io.buffers io.encodings io.encodings.ascii io.encodings.binary
io.encodings.private io.encodings.utf8 io.timeouts kernel libc
locals math math.order namespaces sequences specialized-arrays
specialized-arrays.instances.alien.c-types.uchar splitting
strings summary system ;
IN: io.ports

SYMBOL: default-buffer-size
