public Predicate exec(Prolog engine) {
engine.setB0();
File file;
Term alias = null;
Term opts = SYM_NIL;
JavaObjectTerm streamObject;
Term a1, a2, a3, a4;
a1 = arg1;
a2 = arg2;
a3 = arg3;
a4 = arg4;
// stream
a3 = a3.dereference();
if (!a3.isVariable()) {
throw new IllegalTypeException(this, 3, "variable", a3);
}
// source_sink
a1 = a1.dereference();
if (a1.isVariable()) {
throw new PInstantiationException(this, 1);
}
if (!a1.isSymbol()) {
throw new IllegalDomainException(this, 1, "source_sink", a1);
}
file = new File(((SymbolTerm) a1).name());
// io_mode
a2 = a2.dereference();
if (a2.isVariable()) {
throw new PInstantiationException(this, 2);
}
if (!a2.isSymbol()) {
throw new IllegalTypeException(this, 2, "atom", a2);
}
try {
if (a2.equals(SYM_READ)) {
if (!file.exists()) {
throw new ExistenceException(this, 1, "source_sink", a1, "");
}
PushbackReader in = new PushbackReader(new BufferedReader(
new FileReader(file)), Prolog.PUSHBACK_SIZE);
streamObject = new JavaObjectTerm(in);
opts = new ListTerm(SYM_INPUT, opts);
} else if (a2.equals(SYM_WRITE)) {
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter(file, false)));
streamObject = new JavaObjectTerm(out);
opts = new ListTerm(SYM_OUTPUT, opts);
} else if (a2.equals(SYM_APPEND)) {
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter(file, true)));
streamObject = new JavaObjectTerm(out);
opts = new ListTerm(SYM_OUTPUT, opts);
} else {
throw new IllegalDomainException(this, 2, "io_mode", a2);
}
} catch (IOException e) {