Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added nqp::readfh on JVM. Fixes / cleanup of nqp::writefh impl.
  • Loading branch information
donaldh committed Aug 26, 2013
1 parent f1d040d commit 89894b0
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/vm/jvm/QAST/Compiler.nqp
Expand Up @@ -1890,6 +1890,7 @@ QAST::OperationsJAST.map_classlib_core_op('getstdout', $TYPE_OPS, 'getstdout', [
QAST::OperationsJAST.map_classlib_core_op('getstderr', $TYPE_OPS, 'getstderr', [], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('setencoding', $TYPE_OPS, 'setencoding', [$RT_OBJ, $RT_STR], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('tellfh', $TYPE_OPS, 'tellfh', [$RT_OBJ], $RT_INT, :tc);
QAST::OperationsJAST.map_classlib_core_op('readfh', $TYPE_OPS, 'readfh', [$RT_OBJ, $RT_OBJ, $RT_INT], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('writefh', $TYPE_OPS, 'writefh', [$RT_OBJ, $RT_OBJ], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('printfh', $TYPE_OPS, 'printfh', [$RT_OBJ, $RT_STR], $RT_STR, :tc);
QAST::OperationsJAST.map_classlib_core_op('sayfh', $TYPE_OPS, 'sayfh', [$RT_OBJ, $RT_STR], $RT_STR, :tc);
Expand Down
24 changes: 21 additions & 3 deletions src/vm/jvm/runtime/org/perl6/nqp/io/FileHandle.java
Expand Up @@ -30,7 +30,8 @@ public FileHandle(ThreadContext tc, String filename, String mode) {
}
else if (mode.equals("w")) {
chan = FileChannel.open(p, StandardOpenOption.WRITE,
StandardOpenOption.CREATE);
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING);
}
else if (mode.equals("wa")) {
chan = FileChannel.open(p, StandardOpenOption.WRITE,
Expand Down Expand Up @@ -180,13 +181,30 @@ public boolean eof(ThreadContext tc) {
return eof;
}

public void write(ThreadContext tc, ByteBuffer buffer) {
public byte[] read(ThreadContext tc, int bytes) {
try {
ByteBuffer buffer = ByteBuffer.allocate(bytes);
chan.read(buffer);
buffer.flip();
byte[] res = new byte[buffer.limit()];
buffer.get(res);
return res;
} catch (IOException e) {
throw ExceptionHandling.dieInternal(tc, e);
}
}

public void write(ThreadContext tc, byte[] array) {
ByteBuffer buffer = ByteBuffer.wrap(array);
write(tc, buffer);
}

protected void write(ThreadContext tc, ByteBuffer buffer) {
try {
int toWrite = buffer.limit();
int written = 0;
while (written < toWrite) {
written += chan.write(buffer);
buffer.compact();
}
} catch (IOException e) {
throw ExceptionHandling.dieInternal(tc, e);
Expand Down
1 change: 1 addition & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/io/IIOSyncReadable.java
Expand Up @@ -5,5 +5,6 @@
public interface IIOSyncReadable {
public String slurp(ThreadContext tc);
public String readline(ThreadContext tc);
public byte[] read(ThreadContext tc, int bytes);
public boolean eof(ThreadContext tc);
}
4 changes: 1 addition & 3 deletions src/vm/jvm/runtime/org/perl6/nqp/io/IIOSyncWritable.java
@@ -1,11 +1,9 @@
package org.perl6.nqp.io;

import java.nio.ByteBuffer;

import org.perl6.nqp.runtime.ThreadContext;

public interface IIOSyncWritable {
public void print(ThreadContext tc, String s);
public void say(ThreadContext tc, String s);
public void write(ThreadContext tc, ByteBuffer bb);
public void write(ThreadContext tc, byte[] bytes);
}
16 changes: 16 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/io/StandardReadHandle.java
Expand Up @@ -43,6 +43,22 @@ public void setEncoding(ThreadContext tc, Charset cs) {
this.cs = cs;
}

public byte[] read(ThreadContext tc, int bytes) {
try {
byte[] array = new byte[bytes];
int read = 0;
int offset = 0;
while ((read = is.read(array, offset, bytes - offset)) != -1) {
offset += read;
}
byte[] compact = new byte[offset];
System.arraycopy(array, 0, compact, 0, offset);
return compact;
} catch (IOException e) {
throw ExceptionHandling.dieInternal(tc, e);
}
}

public synchronized String slurp(ThreadContext tc) {
try {
if (br == null)
Expand Down
5 changes: 2 additions & 3 deletions src/vm/jvm/runtime/org/perl6/nqp/io/StandardWriteHandle.java
Expand Up @@ -40,9 +40,8 @@ public void setEncoding(ThreadContext tc, Charset cs) {
dec = cs.newDecoder();
}

public void write(ThreadContext tc, ByteBuffer buffer) {
byte[] bytes = buffer.array();
ps.write(bytes, 0, buffer.limit());
public void write(ThreadContext tc, byte[] bytes) {
ps.write(bytes, 0, bytes.length);
}

public void print(ThreadContext tc, String s) {
Expand Down
33 changes: 30 additions & 3 deletions src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java
Expand Up @@ -403,19 +403,46 @@ public static long tellfh(SixModelObject obj, ThreadContext tc) {
}
}

public static SixModelObject readfh(SixModelObject io, SixModelObject res, long bytes, ThreadContext tc) {
if (io instanceof IOHandleInstance) {
IOHandleInstance h = (IOHandleInstance)io;
if (h.handle instanceof IIOSyncReadable) {
if (res instanceof VMArrayInstance_i8) {
VMArrayInstance_i8 arr = (VMArrayInstance_i8)res;

byte[] array = ((IIOSyncReadable)h.handle).read(tc, (int)bytes);
arr.elems = array.length;
arr.start = 0;
arr.slots = array;

return res;
} else {
throw ExceptionHandling.dieInternal(tc,
"readfh requires a buf with the VMArrayInstance_i8 REPR");
}
} else {
throw ExceptionHandling.dieInternal(tc,
"This handle does not support read");
}
} else {
throw ExceptionHandling.dieInternal(tc,
"readfh requires an object with the IOHandle REPR");
}
}

public static SixModelObject writefh(SixModelObject obj, SixModelObject buf, ThreadContext tc) {
ByteBuffer bb = decode8(buf, tc);
if (obj instanceof IOHandleInstance) {
IOHandleInstance h = (IOHandleInstance)obj;
if (h.handle instanceof IIOSyncWritable)
((IIOSyncWritable)h.handle).write(tc, bb);
((IIOSyncWritable)h.handle).write(tc, bb.array());
else
throw ExceptionHandling.dieInternal(tc,
"This handle does not support print");
"This handle does not support write");
}
else {
throw ExceptionHandling.dieInternal(tc,
"printfh requires an object with the IOHandle REPR");
"writefh requires an object with the IOHandle REPR");
}
return buf;
}
Expand Down

0 comments on commit 89894b0

Please sign in to comment.