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:
035: import java.io.IOException;
036: import java.io.InputStream;
037: import java.io.Reader;
038: import java.io.Writer;
039:
040: public class WriterEcmaWrap {
041: public static void writeByte(Writer os, int ch) throws Exception {
042: os.write((char) ch);
043: }
044:
045: public static void write(Writer os, Call call, int length)
046: throws Exception {
047: for (int i = 0; i < length; i++) {
048: os.write(call.getArg(i, length).toString());
049: }
050: }
051:
052: public static void writeln(Writer os, Call call, int length)
053: throws Exception {
054: for (int i = 0; i < length; i++) {
055: os.write(call.getArg(i, length).toString());
056: }
057:
058: os.write('\n');
059: }
060:
061: public static void printf(Writer os, Call eval, int length)
062: throws Throwable {
063: if (length == 0)
064: return;
065:
066: String result = eval.printf(length);
067:
068: os.write(result);
069: }
070:
071: public static void writeFile(Writer os, Path path)
072: throws IOException {
073: char[] buf = new char[256];
074:
075: ReadStream stream = path.openRead();
076: try {
077: int length;
078: while ((length = stream.read(buf, 0, buf.length)) > 0) {
079: os.write(buf, 0, length);
080: }
081: } finally {
082: stream.close();
083: }
084: }
085:
086: public static void writeStream(Writer os, Call call, int length)
087: throws Throwable {
088: if (length < 1)
089: return;
090:
091: char[] buf = new char[256];
092: int len;
093:
094: Object obj = call.getArgObject(0, length);
095: if (obj instanceof ReadStream) {
096: ReadStream is = (ReadStream) obj;
097: while ((len = is.read(buf, 0, buf.length)) > 0) {
098: os.write(buf, 0, len);
099: }
100: } else if (obj instanceof InputStream) {
101: int ch;
102: InputStream is = (InputStream) obj;
103: while ((ch = is.read()) >= 0)
104: os.write(ch);
105: } else if (obj instanceof Reader) {
106: int ch;
107: Reader is = (Reader) obj;
108: while ((len = is.read(buf, 0, buf.length)) > 0) {
109: os.write(buf, 0, len);
110: }
111: } else
112: throw new IllegalArgumentException("expected stream at "
113: + obj.getClass().getName());
114: }
115: }
|