Source Code Cross Referenced for AbstractBinaryInputOutput.java in  » EJB-Server-resin-3.1.5 » quercus » com » caucho » quercus » lib » file » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » EJB Server resin 3.1.5 » quercus » com.caucho.quercus.lib.file 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003:         *
004:         * This file is part of Resin(R) Open Source
005:         *
006:         * Each copy or derived work must preserve the copyright notice and this
007:         * notice unmodified.
008:         *
009:         * Resin Open Source is free software; you can redistribute it and/or modify
010:         * it under the terms of the GNU General Public License as published by
011:         * the Free Software Foundation; either version 2 of the License, or
012:         * (at your option) any later version.
013:         *
014:         * Resin Open Source is distributed in the hope that it will be useful,
015:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
016:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017:         * of NON-INFRINGEMENT.  See the GNU General Public License for more
018:         * details.
019:         *
020:         * You should have received a copy of the GNU General Public License
021:         * along with Resin Open Source; if not, write to the
022:         *
023:         *   Free Software Foundation, Inc.
024:         *   59 Temple Place, Suite 330
025:         *   Boston, MA 02111-1307  USA
026:         *
027:         * @author Scott Ferguson
028:         */
029:
030:        package com.caucho.quercus.lib.file;
031:
032:        import com.caucho.quercus.QuercusModuleException;
033:        import com.caucho.quercus.env.*;
034:        import com.caucho.vfs.ReadStream;
035:        import com.caucho.vfs.TempBuffer;
036:        import com.caucho.vfs.WriteStream;
037:
038:        import java.io.IOException;
039:        import java.io.InputStream;
040:        import java.io.OutputStream;
041:        import java.io.UnsupportedEncodingException;
042:        import java.util.logging.Level;
043:        import java.util.logging.Logger;
044:
045:        /**
046:         * Represents a Quercus file open for reading
047:         */
048:        public class AbstractBinaryInputOutput implements  BinaryInput,
049:                BinaryOutput {
050:            private static final Logger log = Logger
051:                    .getLogger(AbstractBinaryInputOutput.class.getName());
052:
053:            private Env _env;
054:            private final LineReader _lineReader;
055:
056:            private ReadStream _is;
057:            private WriteStream _os;
058:
059:            protected AbstractBinaryInputOutput(Env env) {
060:                _env = env;
061:                _lineReader = new LineReader(env);
062:            }
063:
064:            protected AbstractBinaryInputOutput(Env env, ReadStream is,
065:                    WriteStream os) {
066:                this (env);
067:                init(is, os);
068:            }
069:
070:            public void init(ReadStream is, WriteStream os) {
071:                _is = is;
072:                _os = os;
073:            }
074:
075:            //
076:            // read methods
077:            //
078:
079:            /**
080:             * Returns the input stream.
081:             */
082:            public InputStream getInputStream() {
083:                return _is;
084:            }
085:
086:            /**
087:             * Opens a copy.
088:             */
089:            public BinaryInput openCopy() throws IOException {
090:                throw new UnsupportedOperationException(getClass().getName());
091:            }
092:
093:            public void setEncoding(String encoding)
094:                    throws UnsupportedEncodingException {
095:                if (_is != null)
096:                    _is.setEncoding(encoding);
097:            }
098:
099:            /**
100:             *
101:             */
102:            public void unread() throws IOException {
103:                if (_is != null)
104:                    _is.unread();
105:            }
106:
107:            /**
108:             * Reads a character from a file, returning -1 on EOF.
109:             */
110:            public int read() throws IOException {
111:                if (_is != null)
112:                    return _is.read();
113:                else
114:                    return -1;
115:            }
116:
117:            /**
118:             * Reads a buffer from a file, returning -1 on EOF.
119:             */
120:            public int read(byte[] buffer, int offset, int length)
121:                    throws IOException {
122:                if (_is != null) {
123:                    return _is.read(buffer, offset, length);
124:                } else
125:                    return -1;
126:            }
127:
128:            /**
129:             * Reads a buffer from a file, returning -1 on EOF.
130:             */
131:            public int read(char[] buffer, int offset, int length)
132:                    throws IOException {
133:                if (_is != null) {
134:                    return _is.read(buffer, offset, length);
135:                } else
136:                    return -1;
137:            }
138:
139:            /**
140:             * Reads into a binary builder.
141:             */
142:            public StringValue read(int length) throws IOException {
143:                if (_is == null)
144:                    return null;
145:
146:                StringValue bb = _env.createBinaryBuilder();
147:
148:                if (bb.appendRead(_is, length) > 0)
149:                    return bb;
150:                else
151:                    return null;
152:            }
153:
154:            /**
155:             * Reads the optional linefeed character from a \r\n
156:             */
157:            public boolean readOptionalLinefeed() throws IOException {
158:                if (_is == null)
159:                    return false;
160:
161:                int ch = _is.read();
162:
163:                if (ch == '\n') {
164:                    return true;
165:                } else {
166:                    _is.unread();
167:                    return false;
168:                }
169:            }
170:
171:            public void writeToStream(OutputStream os, int length)
172:                    throws IOException {
173:                if (_is != null) {
174:                    _is.writeToStream(os, length);
175:                }
176:            }
177:
178:            /**
179:             * Reads a line from a file, returning null on EOF.
180:             */
181:            public StringValue readLine(long length) throws IOException {
182:                return _lineReader.readLine(_env, this , length);
183:            }
184:
185:            /**
186:             * Appends to a string builder.
187:             */
188:            public StringValue appendTo(StringValue builder) throws IOException {
189:                if (_is != null)
190:                    return builder.append(_is);
191:                else
192:                    return builder;
193:            }
194:
195:            /**
196:             * Returns true on the EOF.
197:             */
198:            public boolean isEOF() {
199:                if (_is == null)
200:                    return true;
201:                else {
202:                    try {
203:                        // XXX: not quite right for sockets
204:                        return _is.available() <= 0;
205:                    } catch (IOException e) {
206:                        log.log(Level.FINE, e.toString(), e);
207:
208:                        return true;
209:                    }
210:                }
211:            }
212:
213:            /**
214:             * Returns the current location in the file.
215:             */
216:            public long getPosition() {
217:                if (_is == null)
218:                    return -1;
219:                else
220:                    return _is.getPosition();
221:            }
222:
223:            /**
224:             * Returns the current location in the file.
225:             */
226:            public boolean setPosition(long offset) {
227:                if (_is == null)
228:                    return false;
229:
230:                try {
231:                    return _is.setPosition(offset);
232:                } catch (IOException e) {
233:                    throw new QuercusModuleException(e);
234:                }
235:            }
236:
237:            public long seek(long offset, int whence) {
238:                long position;
239:
240:                switch (whence) {
241:                case BinaryInput.SEEK_CUR:
242:                    position = getPosition() + offset;
243:                    break;
244:                case BinaryInput.SEEK_END:
245:                    // don't necessarily have an end
246:                    position = getPosition();
247:                    break;
248:                case BinaryInput.SEEK_SET:
249:                default:
250:                    position = offset;
251:                    break;
252:                }
253:
254:                if (!setPosition(position))
255:                    return -1L;
256:                else
257:                    return position;
258:            }
259:
260:            public Value stat() {
261:                return BooleanValue.FALSE;
262:            }
263:
264:            /**
265:             * Closes the stream for reading.
266:             */
267:            public void closeRead() {
268:                ReadStream is = _is;
269:                _is = null;
270:
271:                if (is != null)
272:                    is.close();
273:            }
274:
275:            //
276:            // write methods
277:            //
278:
279:            /**
280:             * Returns self as the output stream.
281:             */
282:            public OutputStream getOutputStream() {
283:                return _os;
284:            }
285:
286:            public void write(int ch) throws IOException {
287:                _os.write(ch);
288:            }
289:
290:            public void write(byte[] buffer, int offset, int length)
291:                    throws IOException {
292:                _os.write(buffer, offset, length);
293:            }
294:
295:            /**
296:             * Read length bytes of data from the InputStream
297:             * argument and write them to this output stream.
298:             */
299:            public int write(InputStream is, int length) {
300:                int writeLength = 0;
301:
302:                TempBuffer tb = TempBuffer.allocate();
303:                byte[] buffer = tb.getBuffer();
304:
305:                try {
306:                    while (length > 0) {
307:                        int sublen;
308:
309:                        if (length < buffer.length)
310:                            sublen = length;
311:                        else
312:                            sublen = buffer.length;
313:
314:                        sublen = is.read(buffer, 0, sublen);
315:
316:                        if (sublen < 0)
317:                            break;
318:
319:                        write(buffer, 0, sublen);
320:
321:                        writeLength += sublen;
322:                        length -= sublen;
323:                    }
324:
325:                    return writeLength;
326:                } catch (IOException e) {
327:                    throw new QuercusModuleException(e);
328:                } finally {
329:                    TempBuffer.free(tb);
330:                }
331:            }
332:
333:            /**
334:             * Prints a string to a file.
335:             */
336:            public void print(char v) throws IOException {
337:                write((byte) v);
338:            }
339:
340:            /**
341:             * Prints a string to a file.
342:             */
343:            public void print(String v) throws IOException {
344:                _os.print(v);
345:            }
346:
347:            /**
348:             * Flushes the output.
349:             */
350:            public void flush() throws IOException {
351:                _os.flush();
352:            }
353:
354:            /**
355:             * Closes the file.
356:             */
357:            public void closeWrite() {
358:                try {
359:                    WriteStream os = _os;
360:                    _os = null;
361:
362:                    if (os != null)
363:                        os.close();
364:                } catch (IOException e) {
365:                    log.log(Level.FINE, e.toString(), e);
366:                }
367:            }
368:
369:            /**
370:             * Closes the file.
371:             */
372:            public void close() {
373:                closeRead();
374:                closeWrite();
375:            }
376:
377:            public Object toJavaObject() {
378:                return this ;
379:            }
380:
381:            public void setTimeout(long timeout) {
382:            }
383:
384:            public String getResourceType() {
385:                return "stream";
386:            }
387:
388:            protected Env getEnv() {
389:                return _env;
390:            }
391:
392:            /**
393:             * Converts to a string.
394:             */
395:            public String toString() {
396:                if (_is != null)
397:                    return "AbstractBinaryInputOutput[" + _is.getPath() + "]";
398:                else
399:                    return "AbstractBinaryInputOutput[closed]";
400:            }
401:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.