001: /*
002: * open.java
003: *
004: * Copyright (c) 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.io;
010:
011: import pnuts.lang.Context;
012: import pnuts.lang.PnutsFunction;
013: import pnuts.lang.PnutsException;
014: import org.pnuts.lib.PathHelper;
015: import java.io.InputStream;
016: import java.io.OutputStream;
017: import java.io.ByteArrayInputStream;
018: import java.io.ByteArrayOutputStream;
019: import java.io.BufferedInputStream;
020: import java.io.BufferedOutputStream;
021: import java.io.IOException;
022: import java.io.FileInputStream;
023: import java.io.FileOutputStream;
024: import java.io.File;
025: import java.net.URL;
026: import java.net.URLConnection;
027:
028: /*
029: * function open(input {, mode })
030: */
031: public class open extends PnutsFunction {
032:
033: public open() {
034: super ("open");
035: }
036:
037: public boolean defined(int nargs) {
038: return nargs == 1 || nargs == 2;
039: }
040:
041: static File getFile(Object input, Context context) {
042: File file;
043: if (input instanceof String) {
044: file = PathHelper.getFile((String) input, context);
045: } else if (input instanceof File) {
046: file = (File) input;
047: } else {
048: throw new IllegalArgumentException(String.valueOf(input));
049: }
050: if (file.getPath().length() == 0) {
051: throw new PnutsException("pnuts.io.errors",
052: "empty.fileName", new Object[] {}, context);
053: }
054: return file;
055: }
056:
057: protected Object exec(Object[] args, Context context) {
058: int nargs = args.length;
059: try {
060: if (nargs == 1) {
061: Object arg0 = args[0];
062: if (arg0 instanceof InputStream) {
063: return arg0;
064: } else if (arg0 instanceof URL) {
065: return ((URL) arg0).openStream();
066: } else if (arg0 instanceof byte[]) {
067: return new ByteArrayInputStream((byte[]) arg0);
068: } else {
069: File file = getFile(args[0], context);
070: return new BufferedInputStream(new FileInputStream(
071: file));
072: }
073: } else if (nargs == 2) {
074: Object input = args[0];
075: Object mode = args[1];
076: if ("r".equals(mode) || "R".equals(mode)) {
077: if (input instanceof InputStream) {
078: return input;
079: } else if (input instanceof URL) {
080: return ((URL) input).openStream();
081: } else if (input instanceof byte[]) {
082: return new ByteArrayInputStream((byte[]) input);
083: } else {
084: File file = getFile(input, context);
085: return new BufferedInputStream(
086: new FileInputStream(file));
087: }
088: } else if ("w".equals(mode) || "W".equals(mode)) {
089: if (input instanceof OutputStream) {
090: return input;
091: } else if (input instanceof URL) {
092: URLConnection con = ((URL) input)
093: .openConnection();
094: con.setDoOutput(true);
095: return con.getOutputStream();
096: } else if (input instanceof byte[]) {
097: return new ByteArrayOutputStream();
098: } else {
099: File file = getFile(input, context);
100: PathHelper.ensureBaseDirectory(file);
101: return new BufferedOutputStream(
102: new FileOutputStream(file));
103: }
104: } else if ("a".equals(mode) || "A".equals(mode)) {
105: if (input instanceof URL) {
106: throw new IllegalArgumentException(String
107: .valueOf(input));
108: } else if (input instanceof byte[]) {
109: ByteArrayOutputStream bout = new ByteArrayOutputStream();
110: byte[] bytes = (byte[]) input;
111: bout.write(bytes);
112: return bout;
113: } else {
114: File file = getFile(input, context);
115: PathHelper.ensureBaseDirectory(file);
116: return new BufferedOutputStream(
117: new FileOutputStream(file.toString(),
118: true));
119: }
120: } else {
121: throw new IllegalArgumentException(String
122: .valueOf(mode));
123: }
124: } else {
125: undefined(args, context);
126: return null;
127: }
128: } catch (IOException e) {
129: throw new PnutsException(e, context);
130: }
131: }
132:
133: public String toString() {
134: return "function open((String|File|URL|byte[]) {, (\"r\"|\"w\"|\"a\") } )";
135: }
136: }
|