]> gitweb.factorcode.org Git - factor.git/blob - core/io/streams/byte-array/byte-array.factor
c8c8fc5341081624982f3b68edebb2f5b0dbee1b
[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 M: byte-vector stream-element-type drop +byte+ ;
9
10 : <byte-writer> ( encoding -- stream )
11     512 <byte-vector> swap <encoder> ;
12
13 : with-byte-writer ( encoding quot -- byte-array )
14     [ <byte-writer> ] dip [ output-stream get ] compose with-output-stream*
15     dup encoder? [ stream>> ] when >byte-array ; inline
16
17 TUPLE: byte-reader { underlying byte-array read-only } { i array-capacity } ;
18
19 M: byte-reader stream-element-type drop +byte+ ;
20
21 M: byte-reader stream-peek1 sequence-peek1 ;
22 M: byte-reader stream-peek sequence-peek ;
23 M: byte-reader stream-read-partial stream-read ;
24 M: byte-reader stream-read sequence-read ;
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 (stream-seek) ;
31
32 : <byte-reader> ( byte-array encoding -- stream )
33     [ B{ } like 0 byte-reader boa ] dip <decoder> ;
34
35 : with-byte-reader ( byte-array encoding quot -- )
36     [ <byte-reader> ] dip with-input-stream* ; inline