]> gitweb.factorcode.org Git - factor.git/blob - core/io/streams/byte-array/byte-array.factor
core: Add the shuffler words but without primitives.
[factor.git] / core / io / streams / byte-array / byte-array.factor
1 ! Copyright (C) 2008, 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors byte-arrays byte-vectors destructors io
4 io.encodings io.streams.sequence kernel math namespaces
5 sequences sequences.private ;
6 IN: io.streams.byte-array
7
8 INSTANCE: byte-vector output-stream
9 M: byte-vector stream-element-type drop +byte+ ; inline
10 M: byte-vector stream-tell length ; inline
11
12 : <byte-writer> ( encoding -- stream )
13     512 <byte-vector> swap <encoder> ; inline
14
15 : with-byte-writer ( encoding quot -- byte-array )
16     [ <byte-writer> ] dip [ with-output-stream* ] keepd
17     dup encoder? [ stream>> ] when >byte-array ; inline
18
19 TUPLE: byte-reader { underlying byte-array read-only } { i array-capacity } ;
20 INSTANCE: byte-reader input-stream
21
22 M: byte-reader stream-element-type drop +byte+ ; inline
23
24 M: byte-reader stream-read-unsafe sequence-read-unsafe ;
25 M: byte-reader stream-read1 sequence-read1 ;
26 M: byte-reader stream-read-until sequence-read-until ;
27 M: byte-reader dispose drop ;
28
29 M: byte-reader stream-tell i>> ;
30 M: byte-reader stream-seek sequence-seek ;
31 M: byte-reader stream-seekable? drop t ; inline
32 M: byte-reader stream-length underlying>> length ; inline
33
34 : <byte-reader> ( byte-array encoding -- stream )
35     [ B{ } like 0 byte-reader boa ] dip <decoder> ;
36
37 : with-byte-reader ( byte-array encoding quot -- )
38     [ <byte-reader> ] dip with-input-stream* ; inline