KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > eswrap > com > caucho > vfs > WriteStreamEcmaWrap


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.eswrap.com.caucho.vfs;
30
31 import com.caucho.es.Call;
32 import com.caucho.vfs.Path;
33 import com.caucho.vfs.ReadStream;
34 import com.caucho.vfs.ReadWritePair;
35 import com.caucho.vfs.WriteStream;
36
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39
40 public class WriteStreamEcmaWrap {
41   public static void writeByte(WriteStream os, int ch)
42   throws Throwable JavaDoc
43   {
44     os.write(ch);
45   }
46
47   public static void write(WriteStream os, Call call, int length)
48   throws Throwable JavaDoc
49   {
50     for (int i = 0; i < length; i++) {
51       String JavaDoc string = call.getArgString(i, length);
52
53       if (string == null)
54     string = "null";
55
56       os.print(string);
57     }
58   }
59
60   public static void writeln(WriteStream os, Call call, int length)
61   throws Throwable JavaDoc
62   {
63     for (int i = 0; i < length; i++) {
64       String JavaDoc string = call.getArgString(i, length);
65
66       if (string == null)
67     string = "null";
68
69       if (i + 1 == length)
70     os.println(string);
71       else
72     os.print(string);
73     }
74
75     if (length == 0)
76       os.println();
77   }
78
79   public static void printf(WriteStream os, Call eval, int length)
80     throws Throwable JavaDoc
81   {
82     if (length == 0)
83       return;
84
85     String JavaDoc result = eval.printf(length);
86     
87     os.print(result);
88   }
89
90   public static void writeFile(WriteStream os, Path path)
91     throws IOException JavaDoc
92   {
93     ReadStream stream = path.openRead();
94     
95     try {
96       os.writeStream(stream);
97     } finally {
98       stream.close();
99     }
100   }
101
102   public static void writeStream(WriteStream os, Call call, int length)
103   throws Throwable JavaDoc
104   {
105     if (length < 1)
106       return;
107
108     char []buf = new char[256];
109     int len;
110
111     Object JavaDoc obj = call.getArgObject(0, length);
112     if (obj instanceof ReadStream) {
113       ReadStream is = (ReadStream) obj;
114       os.writeStream(is);
115     }
116     else if (obj instanceof ReadWritePair) {
117       os.writeStream(((ReadWritePair) obj).getReadStream());
118     }
119     else if (obj instanceof InputStream JavaDoc) {
120       os.writeStream((InputStream JavaDoc) obj);
121     }
122     else
123       throw new IllegalArgumentException JavaDoc("expected stream at " +
124                      obj.getClass().getName());
125   }
126 }
127
128
Popular Tags