KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > file > AbstractBinaryOutput


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.lib.file;
31
32 import com.caucho.quercus.QuercusModuleException;
33 import com.caucho.quercus.env.BooleanValue;
34 import com.caucho.quercus.env.Value;
35 import com.caucho.vfs.TempBuffer;
36
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.OutputStream JavaDoc;
40
41 /**
42  * Represents a PHP open file
43  */

44 abstract public class AbstractBinaryOutput
45   extends OutputStream JavaDoc
46   implements BinaryOutput
47 {
48   private int lockedShared = 0;
49   private boolean lockedExclusive = false;
50
51   /**
52    * Returns self as the output stream.
53    */

54   public OutputStream JavaDoc getOutputStream()
55   {
56     return this;
57   }
58
59   /**
60    * Writes to a stream.
61    */

62   public int write(InputStream JavaDoc is, int length)
63   {
64     int writeLength = 0;
65
66     TempBuffer tb = TempBuffer.allocate();
67     byte []buffer = tb.getBuffer();
68
69     try {
70       while (length > 0) {
71     int sublen;
72
73     if (length < buffer.length)
74       sublen = length;
75     else
76       sublen = buffer.length;
77
78     sublen = is.read(buffer, 0, sublen);
79
80     if (sublen < 0)
81       break;
82
83     write(buffer, 0, sublen);
84
85     writeLength += sublen;
86     length -= sublen;
87       }
88
89       return writeLength;
90     } catch (IOException JavaDoc e) {
91       throw new QuercusModuleException(e);
92     } finally {
93       TempBuffer.free(tb);
94     }
95   }
96   
97   /**
98    * Prints a string to a file.
99    */

100   public void print(char v)
101     throws IOException JavaDoc
102   {
103     write((byte) v);
104   }
105
106   /**
107    * Prints a string to a file.
108    */

109   public void print(String JavaDoc v)
110     throws IOException JavaDoc
111   {
112     for (int i = 0; i < v.length(); i++)
113       write(v.charAt(i));
114   }
115
116   /**
117    * Flushes the output.
118    */

119   public void flush()
120     throws IOException JavaDoc
121   {
122   }
123
124
125   /**
126    * Closes the file.
127    */

128   public void closeWrite()
129   {
130     close();
131   }
132
133   /**
134    * Closes the stream.
135    */

136   public void close()
137   {
138   }
139
140   /**
141    * Returns false always for output streams
142    */

143   public boolean isEOF()
144   {
145     return false;
146   }
147
148   /**
149    * Tells the position in the stream
150    */

151   public long getPosition()
152   {
153     return 0;
154   }
155
156   /**
157    * Sets the position.
158    */

159   public boolean setPosition(long offset)
160   {
161     return false;
162   }
163
164   public String JavaDoc getResourceType()
165   {
166     return "stream";
167   }
168
169   public Value stat()
170   {
171     return BooleanValue.FALSE;
172   }
173 }
174
175
Popular Tags