Skip to content

Commit 740fa0c

Browse files
committed
Implement asyncreadbytes on JVM. Extracted Buffers utils from Ops.java
1 parent 7ca649e commit 740fa0c

File tree

3 files changed

+97
-62
lines changed

3 files changed

+97
-62
lines changed

src/vm/jvm/runtime/org/perl6/nqp/io/AsyncSocketHandle.java

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.nio.charset.CharsetEncoder;
1313
import java.nio.charset.CoderResult;
1414

15+
import org.perl6.nqp.runtime.Buffers;
1516
import org.perl6.nqp.runtime.ExceptionHandling;
1617
import org.perl6.nqp.runtime.HLLConfig;
1718
import org.perl6.nqp.runtime.Ops;
@@ -96,11 +97,11 @@ public void writeStr(ThreadContext tc, AsyncTaskInstance task, String toWrite) {
9697
}
9798

9899
public void writeBytes(ThreadContext tc, AsyncTaskInstance task, SixModelObject toWrite) {
99-
ByteBuffer buffer = Ops.decode8(toWrite, tc);
100+
ByteBuffer buffer = Buffers.unstashBytes(toWrite, tc);
100101
writeByteBuffer(tc, task, buffer);
101102
}
102103

103-
protected void writeByteBuffer(final ThreadContext tc, final AsyncTaskInstance task, ByteBuffer buffer) {
104+
private void writeByteBuffer(final ThreadContext tc, final AsyncTaskInstance task, ByteBuffer buffer) {
104105
try {
105106
HLLConfig hllConfig = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig;
106107
final SixModelObject Array = hllConfig.listType;
@@ -139,8 +140,42 @@ protected void callback(ThreadContext tc, AsyncTaskInstance task, SixModelObject
139140
}
140141

141142
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) {
142178
final ByteBuffer readBuffer = ByteBuffer.allocate(32768);
143-
final CharBuffer decodedBuffer = CharBuffer.allocate(32768);
144179

145180
HLLConfig hllConfig = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig;
146181
final SixModelObject Array = hllConfig.listType;
@@ -161,17 +196,10 @@ public void completed(Integer numRead, AsyncTaskInstance task) {
161196
callback(curTC, task, -1, Str, Null);
162197
} else {
163198
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);
168200
readBuffer.compact();
169201

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);
175203

176204
channel.read(readBuffer, task, this);
177205
}
@@ -205,10 +233,6 @@ protected void callback(ThreadContext tc, AsyncTaskInstance task, long seq, SixM
205233
}
206234
}
207235

208-
public void readBytes(final ThreadContext tc, final AsyncTaskInstance task, SixModelObject bufType) {
209-
210-
}
211-
212236
@Override
213237
public void close(ThreadContext tc) {
214238
try {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.perl6.nqp.runtime;
2+
3+
import java.nio.ByteBuffer;
4+
5+
import org.perl6.nqp.sixmodel.SixModelObject;
6+
import org.perl6.nqp.sixmodel.reprs.VMArrayInstance_i8;
7+
import org.perl6.nqp.sixmodel.reprs.VMArrayInstance_u8;
8+
9+
public class Buffers {
10+
11+
public static void stashBytes(ThreadContext tc, SixModelObject res, byte[] bytes) {
12+
if (res instanceof VMArrayInstance_i8) {
13+
VMArrayInstance_i8 arr = (VMArrayInstance_i8)res;
14+
arr.elems = bytes.length;
15+
arr.start = 0;
16+
arr.slots = bytes;
17+
}
18+
else {
19+
res.set_elems(tc, bytes.length);
20+
for (int i = 0; i < bytes.length; i++) {
21+
tc.native_i = bytes[i];
22+
res.bind_pos_native(tc, i);
23+
}
24+
}
25+
}
26+
27+
public static ByteBuffer unstashBytes(SixModelObject buf, ThreadContext tc) {
28+
ByteBuffer bb;
29+
if (buf instanceof VMArrayInstance_i8) {
30+
VMArrayInstance_i8 bufi8 = (VMArrayInstance_i8)buf;
31+
bb = bufi8.slots != null
32+
? ByteBuffer.wrap(bufi8.slots, bufi8.start, bufi8.elems)
33+
: ByteBuffer.allocate(0);
34+
}
35+
else if (buf instanceof VMArrayInstance_u8) {
36+
VMArrayInstance_u8 bufu8 = (VMArrayInstance_u8)buf;
37+
bb = bufu8.slots != null
38+
? ByteBuffer.wrap(bufu8.slots, bufu8.start, bufu8.elems)
39+
: ByteBuffer.allocate(0);
40+
}
41+
else {
42+
int n = (int)buf.elems(tc);
43+
bb = ByteBuffer.allocate(n);
44+
for (int i = 0; i < n; i++) {
45+
buf.at_pos_native(tc, i);
46+
bb.put((byte)tc.native_i);
47+
}
48+
bb.rewind();
49+
}
50+
return bb;
51+
}
52+
}

src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ public static SixModelObject readfh(SixModelObject io, SixModelObject res, long
528528
}
529529

530530
public static long writefh(SixModelObject obj, SixModelObject buf, ThreadContext tc) {
531-
ByteBuffer bb = decode8(buf, tc);
531+
ByteBuffer bb = Buffers.unstashBytes(buf, tc);
532532
long written;
533533
if (obj instanceof IOHandleInstance) {
534534
IOHandleInstance h = (IOHandleInstance)obj;
@@ -3245,31 +3245,16 @@ public static long codepointfromname(String name) {
32453245
return found == null ? -1 : found;
32463246
}
32473247

3248-
private static void stashBytes(ThreadContext tc, SixModelObject res, byte[] bytes) {
3249-
if (res instanceof VMArrayInstance_i8) {
3250-
VMArrayInstance_i8 arr = (VMArrayInstance_i8)res;
3251-
arr.elems = bytes.length;
3252-
arr.start = 0;
3253-
arr.slots = bytes;
3254-
}
3255-
else {
3256-
res.set_elems(tc, bytes.length);
3257-
for (int i = 0; i < bytes.length; i++) {
3258-
tc.native_i = bytes[i];
3259-
res.bind_pos_native(tc, i);
3260-
}
3261-
}
3262-
}
32633248
public static SixModelObject encode(String str, String encoding, SixModelObject res, ThreadContext tc) {
32643249
try {
32653250
if (encoding.equals("utf8")) {
3266-
stashBytes(tc, res, str.getBytes("UTF-8"));
3251+
Buffers.stashBytes(tc, res, str.getBytes("UTF-8"));
32673252
}
32683253
else if (encoding.equals("ascii")) {
3269-
stashBytes(tc, res, str.getBytes("US-ASCII"));
3254+
Buffers.stashBytes(tc, res, str.getBytes("US-ASCII"));
32703255
}
32713256
else if (encoding.equals("iso-8859-1")) {
3272-
stashBytes(tc, res, str.getBytes("ISO-8859-1"));
3257+
Buffers.stashBytes(tc, res, str.getBytes("ISO-8859-1"));
32733258
}
32743259
else if (encoding.equals("utf16")) {
32753260
short[] buffer = new short[str.length()];
@@ -3321,34 +3306,8 @@ else if (encoding.equals("utf32")) {
33213306
}
33223307
}
33233308

3324-
public static ByteBuffer decode8(SixModelObject buf, ThreadContext tc) {
3325-
ByteBuffer bb;
3326-
if (buf instanceof VMArrayInstance_i8) {
3327-
VMArrayInstance_i8 bufi8 = (VMArrayInstance_i8)buf;
3328-
bb = bufi8.slots != null
3329-
? ByteBuffer.wrap(bufi8.slots, bufi8.start, bufi8.elems)
3330-
: ByteBuffer.allocate(0);
3331-
}
3332-
else if (buf instanceof VMArrayInstance_u8) {
3333-
VMArrayInstance_u8 bufu8 = (VMArrayInstance_u8)buf;
3334-
bb = bufu8.slots != null
3335-
? ByteBuffer.wrap(bufu8.slots, bufu8.start, bufu8.elems)
3336-
: ByteBuffer.allocate(0);
3337-
}
3338-
else {
3339-
int n = (int)buf.elems(tc);
3340-
bb = ByteBuffer.allocate(n);
3341-
for (int i = 0; i < n; i++) {
3342-
buf.at_pos_native(tc, i);
3343-
bb.put((byte)tc.native_i);
3344-
}
3345-
bb.rewind();
3346-
}
3347-
return bb;
3348-
}
3349-
33503309
public static String decode8(SixModelObject buf, String csName, ThreadContext tc) {
3351-
ByteBuffer bb = decode8(buf, tc);
3310+
ByteBuffer bb = Buffers.unstashBytes(buf, tc);
33523311
return Charset.forName(csName).decode(bb).toString();
33533312
}
33543313

0 commit comments

Comments
 (0)