01: /*
02: * @(#)readObject.java 1.1 05/06/14
03: *
04: * Copyright (c) 1997-2005 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.io;
10:
11: import pnuts.lang.*;
12: import pnuts.io.PnutsObjectInputStream;
13: import org.pnuts.lib.PathHelper;
14: import java.io.*;
15:
16: /*
17: * readObject(fileOrStream)
18: */
19: public class readObject extends PnutsFunction {
20:
21: public readObject() {
22: super ("readObject");
23: }
24:
25: public boolean defined(int narg) {
26: return narg == 1;
27: }
28:
29: protected Object exec(Object[] args, Context context) {
30: int nargs = args.length;
31: if (nargs != 1) {
32: undefined(args, context);
33: return null;
34: }
35: Object arg0 = args[0];
36: PnutsObjectInputStream in;
37: InputStream toClose = null;
38: try {
39: if (arg0 instanceof InputStream) {
40: in = new PnutsObjectInputStream((InputStream) arg0,
41: context);
42: } else if (arg0 instanceof File) {
43: toClose = new FileInputStream((File) arg0);
44: in = new PnutsObjectInputStream(toClose, context);
45: } else if (arg0 instanceof String) {
46: toClose = new FileInputStream(PathHelper.getFile(
47: (String) arg0, context));
48: in = new PnutsObjectInputStream(toClose, context);
49: } else {
50: throw new IllegalArgumentException(String.valueOf(arg0));
51: }
52:
53: return in.readObject();
54: } catch (Exception ioe) {
55: throw new PnutsException(ioe, context);
56: } finally {
57: if (toClose != null) {
58: try {
59: toClose.close();
60: } catch (IOException e) {
61: }
62: }
63: }
64: }
65:
66: public String toString() {
67: return "function readObject(InputStream|String|File)";
68: }
69: }
|