KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > eswrap > java > io > WriterEcmaWrap


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