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) {
throw new PermissionException(this, "open", "source_sink", a1, "");
}
if (engine.getStreamManager().containsKey(streamObject)) {
throw new InternalException("stream object is duplicated");
}
// stream_options
a4 = a4.dereference();
Term tmp = a4;
while (!tmp.isNil()) {
if (tmp.isVariable()) {
throw new PInstantiationException(this, 4);
}
if (!tmp.isList()) {
throw new IllegalTypeException(this, 4, "list", a4);
}
Term car = ((ListTerm) tmp).car().dereference();
if (car.isVariable()) {
throw new PInstantiationException(this, 4);
}
if (car.isStructure()) {
SymbolTerm functor = ((StructureTerm) car).functor();
Term[] args = ((StructureTerm) car).args();
if (functor.equals(SYM_ALIAS_1)) {