readLine.java :  » Scripting » Pnuts » org » pnuts » text » Java Open Source

Java Open Source » Scripting » Pnuts 
Pnuts » org » pnuts » text » readLine.java
/*
 * @(#)readLine.java 1.2 04/12/06
 *
 * Copyright (c) 2001-2004 Sun Microsystems, Inc. All Rights Reserved.
 *
 * See the file "LICENSE.txt" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */
package org.pnuts.text;

import pnuts.lang.*;
import pnuts.io.CharacterEncoding;
import java.io.*;

/**
 * readLine(Reader/InputStream)
 */
public class readLine extends PnutsFunction {

  public readLine(){
    super("readLine");
  }

  public boolean defined(int narg){
    return narg == 1;
  }

  protected Object exec(Object args[], Context context){
    if (args.length != 1){
      undefined(args, context);
      return null;
    }
    Object arg = args[0];
    try {
      if (arg instanceof BufferedReader){
        return ((BufferedReader)arg).readLine();
      } else if (arg instanceof Reader){
        BufferedReader br = new BufferedReader((Reader)arg);
        return br.readLine();
      } else if (arg instanceof InputStream){
        BufferedReader reader = new BufferedReader(CharacterEncoding.getReader((InputStream)arg, context));
        return reader.readLine();
      } else {
        throw new IllegalArgumentException();
      }
    } catch (IOException e){
      throw new PnutsException(e, context);
    }
  }

  public String toString(){
    return "function readLine(Reader/InputStream)";
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.