KickJava   Java API By Example, From Geeks To Geeks.

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


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 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.io.OutputStream JavaDoc;
40
41 public class OutputStreamEcmaWrap {
42   public static void writeByte(OutputStream JavaDoc os, int ch)
43   throws Throwable JavaDoc
44   {
45     os.write(ch);
46   }
47
48   public static void write(OutputStream JavaDoc os, Call call, int length)
49   throws Throwable JavaDoc
50   {
51     for (int i = 0; i < length; i++) {
52       String JavaDoc string = call.getArgString(i, length);
53
54       if (string == null)
55     string = "null";
56
57       byte []bytes = string.getBytes();
58
59       os.write(bytes, 0, bytes.length);
60     }
61   }
62
63   public static void writeBytes(OutputStream JavaDoc os, byte []buffer, int offset, int length)
64   throws Throwable JavaDoc
65   {
66     os.write(buffer, offset, length);
67   }
68
69   public static void writeln(OutputStream JavaDoc os, Call call, int length)
70   throws Throwable JavaDoc
71   {
72     for (int i = 0; i < length; i++) {
73       String JavaDoc string = call.getArgString(i, length);
74
75       if (string == null)
76     string = "null";
77
78       byte []bytes = string.getBytes();
79
80       os.write(bytes, 0, bytes.length);
81     }
82
83     os.write('\n');
84   }
85
86   public static void printf(OutputStream JavaDoc os, Call eval, int length)
87     throws Throwable JavaDoc
88   {
89     if (length == 0)
90       return;
91
92     String JavaDoc result = eval.printf(length);
93     byte []bytes = result.getBytes();
94     
95     os.write(bytes, 0, bytes.length);
96   }
97
98   public static void writeFile(OutputStream JavaDoc os, Path path)
99     throws IOException JavaDoc
100   {
101     ReadStream stream = path.openRead();
102     
103     try {
104       stream.writeToStream(os);
105     } finally {
106       stream.close();
107     }
108   }
109
110   public static void writeStream(OutputStream JavaDoc os, Call call, int length)
111   throws Throwable JavaDoc
112   {
113     if (length < 1)
114       return;
115
116     char []buf = new char[256];
117     int len;
118
119     Object JavaDoc obj = call.getArgObject(0, length);
120     if (obj instanceof ReadStream) {
121       ReadStream is = (ReadStream) obj;
122       is.writeToStream(os);
123     }
124     else if (obj instanceof ReadWritePair) {
125       ((ReadWritePair) obj).getReadStream().writeToStream(os);
126     }
127     else if (obj instanceof InputStream JavaDoc) {
128       if (os instanceof WriteStream) {
129     ((WriteStream) os).writeStream((InputStream JavaDoc) obj);
130       }
131       else {
132     int ch;
133     InputStream JavaDoc is = (InputStream JavaDoc) obj;
134     while ((ch = is.read()) >= 0)
135       os.write(ch);
136       }
137     }
138     else
139       throw new IllegalArgumentException JavaDoc("expected stream at " +
140                      obj.getClass().getName());
141   }
142 }
143
144
Popular Tags