]> gitweb.factorcode.org Git - factor.git/blob - core/io/streams/byte-array/byte-array.factor
930bee22399a3292796af202b13ec8cb5ca118b5
[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.private io.streams.sequence kernel 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+ ;
10
11 : <byte-writer> ( encoding -- stream )
12     512 <byte-vector> swap <encoder> ;
13
14 : with-byte-writer ( encoding quot -- byte-array )
15     [ <byte-writer> ] dip [ output-stream get ] compose with-output-stream*
16     dup encoder? [ stream>> ] when >byte-array ; inline
17
18 TUPLE: byte-reader { underlying byte-array read-only } { i array-capacity } ;
19 INSTANCE: byte-reader input-stream
20
21 M: byte-reader stream-element-type drop +byte+ ;
22
23 M: byte-reader stream-read-unsafe sequence-read-unsafe ;
24 M: byte-reader stream-read1 sequence-read1 ;
25 M: byte-reader stream-read-until sequence-read-until ;
26 M: byte-reader dispose drop ;
27
28 M: byte-reader stream-tell i>> ;
29 M: byte-reader stream-seek (stream-seek) ;
30 M: byte-reader stream-seekable? drop t ; inline
31 M: byte-reader stream-length underlying>> length ; inline
32
33 : <byte-reader> ( byte-array encoding -- stream )
34     [ B{ } like 0 byte-reader boa ] dip <decoder> ;
35
36 : with-byte-reader ( byte-array encoding quot -- )
37     [ <byte-reader> ] dip with-input-stream* ; inline