Skip to content

Instantly share code, notes, and snippets.

@usev6
Last active May 16, 2016 17:26
Show Gist options
  • Save usev6/788d5ee384897674733aeef2a83aab22 to your computer and use it in GitHub Desktop.
Save usev6/788d5ee384897674733aeef2a83aab22 to your computer and use it in GitHub Desktop.
$fh.get and $fh.slurp-rest(:bin) on rakudo-j
diff --git a/src/vm/jvm/runtime/org/perl6/nqp/io/SyncHandle.java b/src/vm/jvm/runtime/org/perl6/nqp/io/SyncHandle
index 5bc61db..7e31abc 100644
--- a/src/vm/jvm/runtime/org/perl6/nqp/io/SyncHandle.java
+++ b/src/vm/jvm/runtime/org/perl6/nqp/io/SyncHandle.java
@@ -222,12 +222,19 @@ public abstract class SyncHandle implements IIOClosable, IIOEncodable,
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;
+ if ( readBuffer != null ) {
+ byte[] res = new byte[readBuffer.limit() - readBuffer.position()];
+ readBuffer.get(res);
readBuffer = null;
+ return res;
+ }
+ else {
+ 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);
}
@usev6
Copy link
Author

usev6 commented May 16, 2016

added line 18
readBuffer = null

@usev6
Copy link
Author

usev6 commented May 16, 2016

Test-Code for nqp (I construct an VMArrayInstance_u8 there):

$ ./nqp-j -e 'my $fh := nqp::open("data", "r"); nqp::say(nqp::readlinechompfh($fh)); class int8 is repr("P6int") {}; class buf8 is repr("VMArray") {}; nqp::composetype(int8, nqp::hash("integer", nqp::hash("bits", 8, "unsigned", 1))); nqp::composetype( buf8, nqp::hash( "array", nqp::hash("type", int8),)); nqp::say(nqp::decode(nqp::readfh($fh,buf8.new,100),"utf8")); nqp::closefh($fh)'

or with a loop:

./nqp-j -e 'my $fh := nqp::open("data", "r"); nqp::say(nqp::readlinechompfh($fh)); class int8 is repr("P6int") {}; class buf8 is repr("VMArray") {}; nqp::composetype(int8, nqp::hash("integer", nqp::hash("bits", 8, "unsigned", 1))); nqp::composetype( buf8, nqp::hash( "array", nqp::hash("type", int8),)); my $data; while ( nqp::eoffh($fh) == 0 ) { $data := nqp::readfh($fh,buf8.new,100); nqp::print(nqp::decode($data,"utf8")) }; nqp::closefh($fh)'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment