01: /*
02: * @(#)readLine.java 1.2 04/12/06
03: *
04: * Copyright (c) 2001-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.text;
10:
11: import pnuts.lang.*;
12: import pnuts.io.CharacterEncoding;
13: import java.io.*;
14:
15: /**
16: * readLine(Reader/InputStream)
17: */
18: public class readLine extends PnutsFunction {
19:
20: public readLine() {
21: super ("readLine");
22: }
23:
24: public boolean defined(int narg) {
25: return narg == 1;
26: }
27:
28: protected Object exec(Object args[], Context context) {
29: if (args.length != 1) {
30: undefined(args, context);
31: return null;
32: }
33: Object arg = args[0];
34: try {
35: if (arg instanceof BufferedReader) {
36: return ((BufferedReader) arg).readLine();
37: } else if (arg instanceof Reader) {
38: BufferedReader br = new BufferedReader((Reader) arg);
39: return br.readLine();
40: } else if (arg instanceof InputStream) {
41: BufferedReader reader = new BufferedReader(
42: CharacterEncoding.getReader((InputStream) arg,
43: context));
44: return reader.readLine();
45: } else {
46: throw new IllegalArgumentException();
47: }
48: } catch (IOException e) {
49: throw new PnutsException(e, context);
50: }
51: }
52:
53: public String toString() {
54: return "function readLine(Reader/InputStream)";
55: }
56: }
|