KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.CharBuffer;
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 import java.util.Iterator JavaDoc;
40
41 public class ReadWritePairEcmaWrap {
42   public static void writeByte(ReadWritePair s, int ch)
43   throws Throwable JavaDoc
44   {
45     s.getWriteStream().write(ch);
46   }
47
48   public static void write(ReadWritePair s, Call call, int length)
49   throws Throwable JavaDoc
50   {
51     WriteStream os = s.getWriteStream();
52     for (int i = 0; i < length; i++) {
53       String JavaDoc string = call.getArgString(i, length);
54
55       os.print(string);
56     }
57   }
58
59   public static void writeln(ReadWritePair s, Call call, int length)
60   throws Throwable JavaDoc
61   {
62     WriteStream os = s.getWriteStream();
63     for (int i = 0; i < length; i++) {
64       String JavaDoc string = call.getArgString(i, length);
65
66       os.print(string);
67     }
68
69     os.println();
70   }
71
72   public static void writeStream(ReadWritePair s, Call call, int length)
73   throws Throwable JavaDoc
74   {
75     if (length < 1)
76       return;
77
78     WriteStream os = s.getWriteStream();
79     Object JavaDoc obj = call.getArgObject(0, length);
80
81     if (obj instanceof InputStream JavaDoc)
82       os.writeStream((InputStream JavaDoc) obj);
83     else if (obj instanceof ReadWritePair)
84       os.writeStream(((ReadWritePair) obj).getReadStream());
85     else
86       throw new IllegalArgumentException JavaDoc("expected read stream at " +
87                      obj.getClass().getName());
88   }
89
90   public static WriteStream getOutputStream(ReadWritePair s)
91   {
92     return s.getWriteStream();
93   }
94
95   public static void printf(ReadWritePair s, Call eval, int length)
96     throws Throwable JavaDoc
97   {
98     if (length == 0)
99       return;
100
101     WriteStream os = s.getWriteStream();
102     String JavaDoc result = eval.printf(length);
103     
104     os.print(result);
105   }
106
107   public static int readByte(ReadWritePair s)
108   throws IOException JavaDoc
109   {
110     ReadStream is = s.getReadStream();
111     return is.read();
112   }
113
114   public static String JavaDoc read(ReadWritePair s)
115   throws IOException JavaDoc
116   {
117     ReadStream is = s.getReadStream();
118     int ch = is.readChar();
119
120     if (ch < 0)
121       return null;
122
123     return String.valueOf((char) ch);
124   }
125
126   public static String JavaDoc read(ReadWritePair s, int length)
127   throws IOException JavaDoc
128   {
129     ReadStream is = s.getReadStream();
130     CharBuffer cb = new CharBuffer();
131
132     for (; length > 0; length--) {
133       int ch = is.readChar();
134
135       if (ch < 0)
136     break;
137
138       cb.append((char) ch);
139     }
140
141     if (cb.length() == 0)
142       return null;
143
144     return cb.toString();
145   }
146
147   public static int getAvailable(ReadWritePair s)
148     throws IOException JavaDoc
149   {
150     return s.getReadStream().available();
151   }
152
153   public static int available(ReadWritePair s)
154     throws IOException JavaDoc
155   {
156     return s.getReadStream().available();
157   }
158
159   public static String JavaDoc readAvailable(ReadWritePair s, int length)
160     throws IOException JavaDoc
161   {
162     ReadStream is = s.getReadStream();
163     CharBuffer cb = new CharBuffer();
164
165     while (is.getAvailable() > 0) {
166       int ch = is.readChar();
167
168       if (ch < 0)
169     break;
170
171       cb.append((char) ch);
172     }
173
174     if (cb.length() == 0)
175       return null;
176
177     return cb.toString();
178   }
179
180   public static String JavaDoc readln(ReadWritePair s)
181   throws IOException JavaDoc
182   {
183     ReadStream is = s.getReadStream();
184     CharBuffer cb = new CharBuffer();
185
186     if (! is.readln(cb))
187       return null;
188
189     return cb.toString();
190   }
191
192   public static ReadStream getInputStream(ReadWritePair s)
193   {
194     return s.getReadStream();
195   }
196
197   public static void setAttribute(ReadWritePair s, String JavaDoc key, Object JavaDoc value)
198     throws IOException JavaDoc
199   {
200     s.getWriteStream().setAttribute(key, value);
201   }
202
203   public static void removeAttribute(ReadWritePair s, String JavaDoc key)
204     throws IOException JavaDoc
205   {
206     s.getWriteStream().removeAttribute(key);
207   }
208
209   public static Object JavaDoc getAttribute(ReadWritePair s, String JavaDoc key)
210     throws IOException JavaDoc
211   {
212     return s.getReadStream().getAttribute(key);
213   }
214
215   public static Iterator JavaDoc getAttributeNames(ReadWritePair s)
216     throws IOException JavaDoc
217   {
218     return s.getReadStream().getAttributeNames();
219   }
220
221   public static void flush(ReadWritePair s)
222   throws IOException JavaDoc
223   {
224     s.getWriteStream().flush();
225   }
226
227   public static void close(ReadWritePair s)
228   throws IOException JavaDoc
229   {
230     s.getWriteStream().close();
231     s.getReadStream().close();
232   }
233 }
234
235
Popular Tags