Skip to content

Commit fbf9be3

Browse files
committed
Start stubbing in new IO bits.
1 parent d23d885 commit fbf9be3

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.perl6.nqp.io;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.channels.FileChannel;
6+
import java.nio.charset.Charset;
7+
import java.nio.charset.CharsetDecoder;
8+
import java.nio.charset.CharsetEncoder;
9+
import java.nio.file.Path;
10+
import java.nio.file.StandardOpenOption;
11+
12+
import org.perl6.nqp.runtime.ExceptionHandling;
13+
import org.perl6.nqp.runtime.ThreadContext;
14+
15+
public class FileHandle implements IIOClosable, IIOSeekable, IIOEncodable {
16+
private FileChannel chan;
17+
private CharsetEncoder enc;
18+
private CharsetDecoder dec;
19+
20+
public FileHandle(ThreadContext tc, String filename, String mode) {
21+
try {
22+
Path p = new File(filename).toPath();
23+
if (mode.equals("r")) {
24+
chan = FileChannel.open(p, StandardOpenOption.READ);
25+
}
26+
else if (mode.equals("w")) {
27+
chan = FileChannel.open(p, StandardOpenOption.WRITE,
28+
StandardOpenOption.CREATE);
29+
}
30+
else if (mode.equals("wa")) {
31+
chan = FileChannel.open(p, StandardOpenOption.WRITE,
32+
StandardOpenOption.CREATE,
33+
StandardOpenOption.APPEND);
34+
}
35+
else {
36+
ExceptionHandling.dieInternal(tc, "Unhandled file open mode '" + mode + "'");
37+
}
38+
setEncoding(tc, Charset.forName("UTF-8"));
39+
} catch (IOException e) {
40+
throw ExceptionHandling.dieInternal(tc, e);
41+
}
42+
}
43+
44+
public void close(ThreadContext tc) {
45+
try {
46+
chan.close();
47+
} catch (IOException e) {
48+
throw ExceptionHandling.dieInternal(tc, e);
49+
}
50+
}
51+
52+
public void seek(ThreadContext tc, long offset, long whence) {
53+
try {
54+
switch ((int)whence) {
55+
case 0:
56+
chan.position(offset);
57+
break;
58+
case 1:
59+
chan.position(chan.position() + offset);
60+
break;
61+
case 2:
62+
chan.position(chan.size());
63+
break;
64+
default:
65+
throw ExceptionHandling.dieInternal(tc, "Invalid seek mode");
66+
}
67+
} catch (IOException e) {
68+
throw ExceptionHandling.dieInternal(tc, e);
69+
}
70+
}
71+
72+
public long tell(ThreadContext tc) {
73+
try {
74+
return chan.position();
75+
} catch (IOException e) {
76+
throw ExceptionHandling.dieInternal(tc, e);
77+
}
78+
}
79+
80+
public void setEncoding(ThreadContext tc, Charset cs) {
81+
enc = cs.newEncoder();
82+
dec = cs.newDecoder();
83+
}
84+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.perl6.nqp.io;
2+
3+
import org.perl6.nqp.runtime.ThreadContext;
4+
5+
public interface IIOClosable {
6+
public void close(ThreadContext tc);
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.perl6.nqp.io;
2+
3+
import java.nio.charset.Charset;
4+
5+
import org.perl6.nqp.runtime.ThreadContext;
6+
7+
public interface IIOEncodable {
8+
public void setEncoding(ThreadContext tc, Charset cs);
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.perl6.nqp.io;
2+
3+
import org.perl6.nqp.runtime.ThreadContext;
4+
5+
public interface IIOSeekable {
6+
public void seek(ThreadContext tc, long offset, long whence);
7+
public long tell(ThreadContext tc);
8+
}

tools/build/Makefile-JVM.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CP = $(PERL) -MExtUtils::Command -e cp
1212
CHMOD = $(PERL) -MExtUtils::Command -e chmod
1313

1414
RUNTIME_JAVAS = \
15+
src/vm/jvm/runtime/org/perl6/nqp/io/*.java \
1516
src/vm/jvm/runtime/org/perl6/nqp/jast2bc/*.java \
1617
src/vm/jvm/runtime/org/perl6/nqp/runtime/*.java \
1718
src/vm/jvm/runtime/org/perl6/nqp/sixmodel/*.java \

0 commit comments

Comments
 (0)