22// Use of this source code is governed by an MIT-style license that can be
33// found in the LICENSE file.
44
5- import reader
6- import writer
5+ import io
76
87import .connection
98
10- // This is an adapter that converts a chunked stream (RFC 2616) to a stream of
11- // just the payload bytes. It takes a BufferedReader as a constructor argument,
12- // and acts like a Socket or TlsSocket, having one method, called read, which
13- // returns ByteArrays. End of stream is indicated with a null return value
14- // from read.
15- class ChunkedReader implements reader.Reader :
9+ /**
10+ This is an adapter that converts a chunked stream (RFC 2616) to a stream of
11+ just the payload bytes. It takes an $io.Reader as a constructor argument,
12+ and acts like an $io.Reader.
13+
14+ Deprecated for public use. Use the type $io.Reader instead.
15+ This class will be made private in the future.
16+ */
17+ class ChunkedReader extends ChunkedReader_ :
18+ constructor connection / Connection reader / io.Reader :
19+ super connection reader
20+
21+ /**
22+ This is an adapter that converts a chunked stream (RFC 2616) to a stream of
23+ just the payload bytes. It takes an $io.Reader as a constructor argument,
24+ and acts like an $io.Reader.
25+ */
26+ class ChunkedReader_ extends io .Reader :
1627 connection_ / Connection? := null
17- reader_ / reader.BufferedReader ? := ?
28+ reader_ / io.Reader ? := ?
1829 left_in_chunk_ := 0 // How much more raw data we are waiting for before the next size line.
1930
2031 constructor .connection_ .reader_ :
@@ -23,26 +34,29 @@ class ChunkedReader implements reader.Reader:
2334 Returns the underlying reader, which may have buffered up data.
2435
2536 The ChunkedReader is unusable after a called to $detach_reader.
37+
38+ Deprecated.
2639 */
27- detach_reader -> reader.BufferedReader :
40+ // TODO(florian): remove already now?
41+ detach_reader -> io.Reader :
2842 r := reader_
2943 reader_ = null
3044 return r
3145
32- read -> ByteArray? :
46+ read_ -> ByteArray? :
3347 while true :
3448 if not connection_ :
3549 return null
3650 if left_in_chunk_ > 0 :
3751 result := reader_ .read --max_size = left_in_chunk_
38- if not result : throw reader . UNEXPECTED_END_OF_READER_EXCEPTION
52+ if not result : throw io . Reader . UNEXPECTED_END_OF_READER
3953 left_in_chunk_ -= result .size
4054 if left_in_chunk_ == 0 :
4155 expect_ '\r'
4256 expect_ '\n'
4357 return result
4458
45- raw_length := reader_ .read_bytes_until '\r'
59+ raw_length := reader_ .read_bytes_up_to '\r'
4660 expect_ '\n'
4761
4862 left_in_chunk_ = int .parse raw_length --radix = 16
@@ -55,34 +69,42 @@ class ChunkedReader implements reader.Reader:
5569 connection_ = null
5670
5771 expect_ byte / int :
58- b := reader_ .byte 0
72+ b := reader_ .peek_byte 0
5973 if b != byte : throw "PROTOCOL_ERROR"
6074 reader_ .skip 1
6175
62- class ChunkedWriter implements BodyWriter :
76+ /**
77+ Deprecated for public use. Use the type $io.CloseableWriter instead.
78+ This class will be made private in the future.
79+ */
80+ class ChunkedWriter extends ChunkedWriter_ :
81+ constructor connection / Connection writer / io.Writer :
82+ super connection writer
83+
84+ class ChunkedWriter_ extends io .CloseableWriter :
6385 static CRLF_ ::= "\r\n "
6486
6587 connection_ / Connection? := null
66- writer_ / writer.Writer
88+ writer_ / io.Writer
89+
90+ constructor .connection_ .writer_ :
6791
6892 // We don't know the amount of data ahead of time, so it may already be done.
69- is_done -> bool :
93+ is_done_ -> bool :
7094 return true
7195
72- constructor .connection_ .writer_ :
73-
74- write data -> int :
75- size := data .size
96+ try_write_ data / io.Data from / int to / int -> int :
97+ size := to - from
7698 if size == 0 : return 0
7799 write_header_ size
78- writer_ .write data // Always writes all data.
100+ writer_ .write data from to // Always writes all data.
79101 // Once we've sent the data, the other side might conclude that
80102 // they have gotten everything they need, so we don't want to throw
81103 // an exception on writing the final CRLF.
82104 catch : writer_ .write CRLF_
83105 return size
84106
85- close -> none :
107+ close_ -> none :
86108 if not connection_ : return
87109 // Once we've sent the last chunk, the remaining transmitted information
88110 // is redundant, so we don't want to throw exceptions if the other side
0 commit comments