-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChunkedStream.rbbas
172 lines (151 loc) · 4.15 KB
/
ChunkedStream.rbbas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#tag Class
Protected Class ChunkedStream
Implements Readable,Writeable
#tag Method, Flags = &h1
Protected Sub Constructor()
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Shared Function Create(ChunkReceiver As Writeable) As HTTP.ChunkedStream
Dim stream As New ChunkedStream
stream.WriteStream = ChunkReceiver
Return stream
End Function
#tag EndMethod
#tag Method, Flags = &h0
Function EOF() As Boolean
// Part of the Readable interface.
If ReadStream <> Nil Then Return ReadStream.EOF
End Function
#tag EndMethod
#tag Method, Flags = &h0
Sub Flush()
// Part of the Writeable interface.
If WriteStream <> Nil Then WriteStream.Flush
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Shared Function Open(ChunkedData As Readable) As HTTP.ChunkedStream
Dim stream As New ChunkedStream
stream.ReadStream = ChunkedData
Return stream
End Function
#tag EndMethod
#tag Method, Flags = &h0
Function Read(Count As Integer, encoding As TextEncoding = Nil) As String
// Part of the Readable interface.
If ReadStream = Nil Then Raise New IOException
If ReadBuffer = Nil Then ReadBuffer = New MemoryBlock(0)
Dim outstream As New BinaryStream(ReadBuffer)
outstream.Position = outstream.Length
Do Until ReadStream.EOF Or outstream.Length >= Count
Dim char As String
While InStrB(char, CRLF) <= 0 And Not ReadStream.EOF
char = char + ReadStream.Read(1)
Wend
Dim sz As Integer = Val("&h" + NthField(char, ";", 1))
outstream.Write(ReadStream.Read(sz))
Call ReadStream.Read(2)
Loop
outstream.Close
Dim data As String = LeftB(ReadBuffer, Count)
If ReadBuffer.Size = Count Then
ReadBuffer = Nil
Else
ReadBuffer = RightB(ReadBuffer, ReadBuffer.Size - Count)
End If
If encoding <> Nil Then data = DefineEncoding(data, encoding)
Return data
End Function
#tag EndMethod
#tag Method, Flags = &h0
Function ReadError() As Boolean
// Part of the Readable interface.
Return ReadStream <> Nil And ReadStream.ReadError
End Function
#tag EndMethod
#tag Method, Flags = &h0
Sub Write(text As String)
// Part of the Writeable interface.
If WriteStream = Nil Then Raise New IOException
If BufferStream = Nil Then
WriteBuffer = New MemoryBlock(0)
BufferStream = New BinaryStream(WriteBuffer)
End If
BufferStream.Write(text)
If BufferStream.Length >= MinChunkSize Then
BufferStream.Close
WriteStream.Write(Hex(WriteBuffer.Size) + CRLF)
WriteStream.Write(WriteBuffer + CRLF)
WriteBuffer = Nil
BufferStream = Nil
End If
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Function WriteError() As Boolean
// Part of the Writeable interface.
Return WriteStream.WriteError And WriteStream.WriteError
End Function
#tag EndMethod
#tag Property, Flags = &h21
Private BufferStream As BinaryStream
#tag EndProperty
#tag Property, Flags = &h0
MinChunkSize As Integer = 32
#tag EndProperty
#tag Property, Flags = &h21
Private ReadBuffer As MemoryBlock
#tag EndProperty
#tag Property, Flags = &h21
Private ReadStream As Readable
#tag EndProperty
#tag Property, Flags = &h21
Private WriteBuffer As MemoryBlock
#tag EndProperty
#tag Property, Flags = &h21
Private WriteStream As Writeable
#tag EndProperty
#tag ViewBehavior
#tag ViewProperty
Name="Index"
Visible=true
Group="ID"
InitialValue="-2147483648"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="Left"
Visible=true
Group="Position"
InitialValue="0"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="MinChunkSize"
Group="Behavior"
InitialValue="1024"
Type="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="Name"
Visible=true
Group="ID"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="Super"
Visible=true
Group="ID"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="Top"
Visible=true
Group="Position"
InitialValue="0"
InheritedFrom="Object"
#tag EndViewProperty
#tag EndViewBehavior
End Class
#tag EndClass