12
12
import java .nio .charset .CharsetEncoder ;
13
13
import java .nio .charset .CoderResult ;
14
14
15
+ import org .perl6 .nqp .runtime .Buffers ;
15
16
import org .perl6 .nqp .runtime .ExceptionHandling ;
16
17
import org .perl6 .nqp .runtime .HLLConfig ;
17
18
import org .perl6 .nqp .runtime .Ops ;
@@ -96,11 +97,11 @@ public void writeStr(ThreadContext tc, AsyncTaskInstance task, String toWrite) {
96
97
}
97
98
98
99
public void writeBytes (ThreadContext tc , AsyncTaskInstance task , SixModelObject toWrite ) {
99
- ByteBuffer buffer = Ops . decode8 (toWrite , tc );
100
+ ByteBuffer buffer = Buffers . unstashBytes (toWrite , tc );
100
101
writeByteBuffer (tc , task , buffer );
101
102
}
102
103
103
- protected void writeByteBuffer (final ThreadContext tc , final AsyncTaskInstance task , ByteBuffer buffer ) {
104
+ private void writeByteBuffer (final ThreadContext tc , final AsyncTaskInstance task , ByteBuffer buffer ) {
104
105
try {
105
106
HLLConfig hllConfig = tc .curFrame .codeRef .staticInfo .compUnit .hllConfig ;
106
107
final SixModelObject Array = hllConfig .listType ;
@@ -139,8 +140,42 @@ protected void callback(ThreadContext tc, AsyncTaskInstance task, SixModelObject
139
140
}
140
141
141
142
public void readChars (final ThreadContext tc , final AsyncTaskInstance task ) {
143
+ HLLConfig hllConfig = tc .curFrame .codeRef .staticInfo .compUnit .hllConfig ;
144
+ final SixModelObject Str = hllConfig .strBoxType ;
145
+
146
+ readSocket (tc , task , new Decoder () {
147
+ final CharBuffer decodedBuffer = CharBuffer .allocate (32768 );
148
+
149
+ public SixModelObject decode (ThreadContext tc , ByteBuffer source , Integer numRead ) throws Exception {
150
+ CoderResult coderResult = dec .decode (source , decodedBuffer , numRead == 0 ? true : false );
151
+ if (coderResult .isError ()) {
152
+ coderResult .throwException ();
153
+ }
154
+ decodedBuffer .flip ();
155
+ String decoded = decodedBuffer .toString ();
156
+ decodedBuffer .clear ();
157
+ return Ops .box_s (decoded , Str , tc );
158
+ }
159
+ });
160
+ }
161
+
162
+ public void readBytes (final ThreadContext tc , final AsyncTaskInstance task , final SixModelObject bufType ) {
163
+ readSocket (tc , task , new Decoder () {
164
+ public SixModelObject decode (ThreadContext tc , ByteBuffer source , Integer numRead )
165
+ throws Exception {
166
+ SixModelObject res = bufType .st .REPR .allocate (tc , bufType .st );
167
+ Buffers .stashBytes (tc , res , source .slice ().array ());
168
+ return res ;
169
+ }
170
+ });
171
+ }
172
+
173
+ static interface Decoder {
174
+ public SixModelObject decode (ThreadContext tc , ByteBuffer source , Integer numRead ) throws Exception ;
175
+ }
176
+
177
+ private void readSocket (final ThreadContext tc , final AsyncTaskInstance task , final Decoder decoder ) {
142
178
final ByteBuffer readBuffer = ByteBuffer .allocate (32768 );
143
- final CharBuffer decodedBuffer = CharBuffer .allocate (32768 );
144
179
145
180
HLLConfig hllConfig = tc .curFrame .codeRef .staticInfo .compUnit .hllConfig ;
146
181
final SixModelObject Array = hllConfig .listType ;
@@ -161,17 +196,10 @@ public void completed(Integer numRead, AsyncTaskInstance task) {
161
196
callback (curTC , task , -1 , Str , Null );
162
197
} else {
163
198
readBuffer .flip ();
164
- CoderResult coderResult = dec .decode (readBuffer , decodedBuffer , numRead == 0 ? true : false );
165
- if (coderResult .isError ()) {
166
- coderResult .throwException ();
167
- }
199
+ SixModelObject decoded = decoder .decode (tc , readBuffer , numRead );
168
200
readBuffer .compact ();
169
201
170
- decodedBuffer .flip ();
171
- String decoded = decodedBuffer .toString ();
172
- decodedBuffer .clear ();
173
-
174
- callback (curTC , task , task .seq ++, Ops .box_s (decoded , Str , curTC ), Null );
202
+ callback (curTC , task , task .seq ++, decoded , Null );
175
203
176
204
channel .read (readBuffer , task , this );
177
205
}
@@ -205,10 +233,6 @@ protected void callback(ThreadContext tc, AsyncTaskInstance task, long seq, SixM
205
233
}
206
234
}
207
235
208
- public void readBytes (final ThreadContext tc , final AsyncTaskInstance task , SixModelObject bufType ) {
209
-
210
- }
211
-
212
236
@ Override
213
237
public void close (ThreadContext tc ) {
214
238
try {
0 commit comments