Skip to content

Commit 89894b0

Browse files
committed
Added nqp::readfh on JVM. Fixes / cleanup of nqp::writefh impl.
1 parent f1d040d commit 89894b0

File tree

7 files changed

+72
-12
lines changed

7 files changed

+72
-12
lines changed

src/vm/jvm/QAST/Compiler.nqp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,7 @@ QAST::OperationsJAST.map_classlib_core_op('getstdout', $TYPE_OPS, 'getstdout', [
18901890
QAST::OperationsJAST.map_classlib_core_op('getstderr', $TYPE_OPS, 'getstderr', [], $RT_OBJ, :tc);
18911891
QAST::OperationsJAST.map_classlib_core_op('setencoding', $TYPE_OPS, 'setencoding', [$RT_OBJ, $RT_STR], $RT_OBJ, :tc);
18921892
QAST::OperationsJAST.map_classlib_core_op('tellfh', $TYPE_OPS, 'tellfh', [$RT_OBJ], $RT_INT, :tc);
1893+
QAST::OperationsJAST.map_classlib_core_op('readfh', $TYPE_OPS, 'readfh', [$RT_OBJ, $RT_OBJ, $RT_INT], $RT_OBJ, :tc);
18931894
QAST::OperationsJAST.map_classlib_core_op('writefh', $TYPE_OPS, 'writefh', [$RT_OBJ, $RT_OBJ], $RT_OBJ, :tc);
18941895
QAST::OperationsJAST.map_classlib_core_op('printfh', $TYPE_OPS, 'printfh', [$RT_OBJ, $RT_STR], $RT_STR, :tc);
18951896
QAST::OperationsJAST.map_classlib_core_op('sayfh', $TYPE_OPS, 'sayfh', [$RT_OBJ, $RT_STR], $RT_STR, :tc);

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public FileHandle(ThreadContext tc, String filename, String mode) {
3030
}
3131
else if (mode.equals("w")) {
3232
chan = FileChannel.open(p, StandardOpenOption.WRITE,
33-
StandardOpenOption.CREATE);
33+
StandardOpenOption.CREATE,
34+
StandardOpenOption.TRUNCATE_EXISTING);
3435
}
3536
else if (mode.equals("wa")) {
3637
chan = FileChannel.open(p, StandardOpenOption.WRITE,
@@ -180,13 +181,30 @@ public boolean eof(ThreadContext tc) {
180181
return eof;
181182
}
182183

183-
public void write(ThreadContext tc, ByteBuffer buffer) {
184+
public byte[] read(ThreadContext tc, int bytes) {
185+
try {
186+
ByteBuffer buffer = ByteBuffer.allocate(bytes);
187+
chan.read(buffer);
188+
buffer.flip();
189+
byte[] res = new byte[buffer.limit()];
190+
buffer.get(res);
191+
return res;
192+
} catch (IOException e) {
193+
throw ExceptionHandling.dieInternal(tc, e);
194+
}
195+
}
196+
197+
public void write(ThreadContext tc, byte[] array) {
198+
ByteBuffer buffer = ByteBuffer.wrap(array);
199+
write(tc, buffer);
200+
}
201+
202+
protected void write(ThreadContext tc, ByteBuffer buffer) {
184203
try {
185204
int toWrite = buffer.limit();
186205
int written = 0;
187206
while (written < toWrite) {
188207
written += chan.write(buffer);
189-
buffer.compact();
190208
}
191209
} catch (IOException e) {
192210
throw ExceptionHandling.dieInternal(tc, e);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
public interface IIOSyncReadable {
66
public String slurp(ThreadContext tc);
77
public String readline(ThreadContext tc);
8+
public byte[] read(ThreadContext tc, int bytes);
89
public boolean eof(ThreadContext tc);
910
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package org.perl6.nqp.io;
22

3-
import java.nio.ByteBuffer;
4-
53
import org.perl6.nqp.runtime.ThreadContext;
64

75
public interface IIOSyncWritable {
86
public void print(ThreadContext tc, String s);
97
public void say(ThreadContext tc, String s);
10-
public void write(ThreadContext tc, ByteBuffer bb);
8+
public void write(ThreadContext tc, byte[] bytes);
119
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ public void setEncoding(ThreadContext tc, Charset cs) {
4343
this.cs = cs;
4444
}
4545

46+
public byte[] read(ThreadContext tc, int bytes) {
47+
try {
48+
byte[] array = new byte[bytes];
49+
int read = 0;
50+
int offset = 0;
51+
while ((read = is.read(array, offset, bytes - offset)) != -1) {
52+
offset += read;
53+
}
54+
byte[] compact = new byte[offset];
55+
System.arraycopy(array, 0, compact, 0, offset);
56+
return compact;
57+
} catch (IOException e) {
58+
throw ExceptionHandling.dieInternal(tc, e);
59+
}
60+
}
61+
4662
public synchronized String slurp(ThreadContext tc) {
4763
try {
4864
if (br == null)

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ public void setEncoding(ThreadContext tc, Charset cs) {
4040
dec = cs.newDecoder();
4141
}
4242

43-
public void write(ThreadContext tc, ByteBuffer buffer) {
44-
byte[] bytes = buffer.array();
45-
ps.write(bytes, 0, buffer.limit());
43+
public void write(ThreadContext tc, byte[] bytes) {
44+
ps.write(bytes, 0, bytes.length);
4645
}
4746

4847
public void print(ThreadContext tc, String s) {

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,19 +403,46 @@ public static long tellfh(SixModelObject obj, ThreadContext tc) {
403403
}
404404
}
405405

406+
public static SixModelObject readfh(SixModelObject io, SixModelObject res, long bytes, ThreadContext tc) {
407+
if (io instanceof IOHandleInstance) {
408+
IOHandleInstance h = (IOHandleInstance)io;
409+
if (h.handle instanceof IIOSyncReadable) {
410+
if (res instanceof VMArrayInstance_i8) {
411+
VMArrayInstance_i8 arr = (VMArrayInstance_i8)res;
412+
413+
byte[] array = ((IIOSyncReadable)h.handle).read(tc, (int)bytes);
414+
arr.elems = array.length;
415+
arr.start = 0;
416+
arr.slots = array;
417+
418+
return res;
419+
} else {
420+
throw ExceptionHandling.dieInternal(tc,
421+
"readfh requires a buf with the VMArrayInstance_i8 REPR");
422+
}
423+
} else {
424+
throw ExceptionHandling.dieInternal(tc,
425+
"This handle does not support read");
426+
}
427+
} else {
428+
throw ExceptionHandling.dieInternal(tc,
429+
"readfh requires an object with the IOHandle REPR");
430+
}
431+
}
432+
406433
public static SixModelObject writefh(SixModelObject obj, SixModelObject buf, ThreadContext tc) {
407434
ByteBuffer bb = decode8(buf, tc);
408435
if (obj instanceof IOHandleInstance) {
409436
IOHandleInstance h = (IOHandleInstance)obj;
410437
if (h.handle instanceof IIOSyncWritable)
411-
((IIOSyncWritable)h.handle).write(tc, bb);
438+
((IIOSyncWritable)h.handle).write(tc, bb.array());
412439
else
413440
throw ExceptionHandling.dieInternal(tc,
414-
"This handle does not support print");
441+
"This handle does not support write");
415442
}
416443
else {
417444
throw ExceptionHandling.dieInternal(tc,
418-
"printfh requires an object with the IOHandle REPR");
445+
"writefh requires an object with the IOHandle REPR");
419446
}
420447
return buf;
421448
}

0 commit comments

Comments
 (0)