001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.eswrap.java.io;
030:
031: import com.caucho.es.Call;
032: import com.caucho.vfs.Path;
033: import com.caucho.vfs.ReadStream;
034: import com.caucho.vfs.ReadWritePair;
035: import com.caucho.vfs.WriteStream;
036:
037: import java.io.IOException;
038: import java.io.InputStream;
039: import java.io.OutputStream;
040:
041: public class OutputStreamEcmaWrap {
042: public static void writeByte(OutputStream os, int ch)
043: throws Throwable {
044: os.write(ch);
045: }
046:
047: public static void write(OutputStream os, Call call, int length)
048: throws Throwable {
049: for (int i = 0; i < length; i++) {
050: String string = call.getArgString(i, length);
051:
052: if (string == null)
053: string = "null";
054:
055: byte[] bytes = string.getBytes();
056:
057: os.write(bytes, 0, bytes.length);
058: }
059: }
060:
061: public static void writeBytes(OutputStream os, byte[] buffer,
062: int offset, int length) throws Throwable {
063: os.write(buffer, offset, length);
064: }
065:
066: public static void writeln(OutputStream os, Call call, int length)
067: throws Throwable {
068: for (int i = 0; i < length; i++) {
069: String string = call.getArgString(i, length);
070:
071: if (string == null)
072: string = "null";
073:
074: byte[] bytes = string.getBytes();
075:
076: os.write(bytes, 0, bytes.length);
077: }
078:
079: os.write('\n');
080: }
081:
082: public static void printf(OutputStream os, Call eval, int length)
083: throws Throwable {
084: if (length == 0)
085: return;
086:
087: String result = eval.printf(length);
088: byte[] bytes = result.getBytes();
089:
090: os.write(bytes, 0, bytes.length);
091: }
092:
093: public static void writeFile(OutputStream os, Path path)
094: throws IOException {
095: ReadStream stream = path.openRead();
096:
097: try {
098: stream.writeToStream(os);
099: } finally {
100: stream.close();
101: }
102: }
103:
104: public static void writeStream(OutputStream os, Call call,
105: int length) throws Throwable {
106: if (length < 1)
107: return;
108:
109: char[] buf = new char[256];
110: int len;
111:
112: Object obj = call.getArgObject(0, length);
113: if (obj instanceof ReadStream) {
114: ReadStream is = (ReadStream) obj;
115: is.writeToStream(os);
116: } else if (obj instanceof ReadWritePair) {
117: ((ReadWritePair) obj).getReadStream().writeToStream(os);
118: } else if (obj instanceof InputStream) {
119: if (os instanceof WriteStream) {
120: ((WriteStream) os).writeStream((InputStream) obj);
121: } else {
122: int ch;
123: InputStream is = (InputStream) obj;
124: while ((ch = is.read()) >= 0)
125: os.write(ch);
126: }
127: } else
128: throw new IllegalArgumentException("expected stream at "
129: + obj.getClass().getName());
130: }
131: }
|