Source Code Cross Referenced for BufferedContent.java in  » Web-Server » simple » simple » http » serve » 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 » Web Server » simple » simple.http.serve 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * BufferedContent.java November 2002
003:         *
004:         * Copyright (C) 2002, Niall Gallagher <niallg@users.sf.net>
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation.
009:         *
010:         * This library is distributed in the hope that it will be useful,
011:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
013:         * GNU Lesser General Public License for more details.
014:         *
015:         * You should have received a copy of the GNU Lesser General 
016:         * Public License along with this library; if not, write to the 
017:         * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
018:         * Boston, MA  02111-1307  USA
019:         */
020:
021:        package simple.http.serve;
022:
023:        import java.io.OutputStream;
024:        import java.io.InputStream;
025:        import java.io.IOException;
026:
027:        /** 
028:         * The <code>BufferedContent</code> represents content that is stored 
029:         * within an internal buffer. This acquires the meta data of the file
030:         * using the issued <code>Context</code> object and buffers the
031:         * contents using a byte array. This is provided so that files can 
032:         * be cached in memory so that repeated requests for a popular file 
033:         * can be done quicker.
034:         * <p>
035:         * This implementation of the <code>Content</code> interface is used
036:         * when the <code>FileContentFactory.getInstance</code> method is 
037:         * given a size that is less than the size of the file. For a caching
038:         * scheme the file size should not be too large to conserve memory.
039:         *
040:         * @author Niall Gallagher
041:         *
042:         * @see simple.http.serve.CacheContentFactory
043:         */
044:        final class BufferedContent extends IndexedContent {
045:
046:            /**
047:             * The buffer that contains the contents of the file.
048:             */
049:            private volatile byte[] cache;
050:
051:            /**
052:             * Constructor for the <code>BufferedContent</code> acquires the
053:             * bytes for the specified file using an <code>InputStream</code>
054:             * retrieved from the <code>IndexedContent</code>. This is then
055:             * used to acquire the bytes of the file and cache them in main 
056:             * memory so that invocations of the <code>write</code> method 
057:             * can be done without refering to the file system.
058:             *
059:             * @param context the <code>Context</code> used for indexing
060:             * @param target the request URI that refers to the file
061:             */
062:            public BufferedContent(Context context, String target) {
063:                super (context, target);
064:            }
065:
066:            /**
067:             * The <code>write</code> method writes the contents of the file
068:             * to the issued <code>OutputStream</code>. This is done using the
069:             * allocated buffer to which involves a single write operation.
070:             *
071:             * @param out the <code>OutputStream</code> to write the contents
072:             *
073:             * @exception IOException this is thrown if the issued stream has
074:             * an I/O problem writing the contents
075:             */
076:            public void write(OutputStream out) throws IOException {
077:                if (cache == null) {
078:                    int size = getLength();
079:
080:                    if (size > 0) {
081:                        write(out, size);
082:                    }
083:                } else {
084:                    out.write(cache);
085:                }
086:            }
087:
088:            /**
089:             * This <code>write</code> method writes a specific number of bytes
090:             * from the contents of the file. This is simply used as a driver
091:             * for acquiring the cache of the file. This will acquire the file
092:             * input stream, delegate to the caching write and close the file.
093:             * 
094:             * @param out the <code>OutputStream</code> to write the contents
095:             * @param size this is the number of bytes to read from the file
096:             *
097:             * @exception IOException this is thrown if the issued stream has
098:             * an I/O problem writing the contents
099:             */
100:            private void write(OutputStream out, int size) throws IOException {
101:                InputStream in = getInputStream();
102:
103:                if (size > 0) {
104:                    write(out, in, size);
105:                }
106:                in.close();
107:            }
108:
109:            /**
110:             * The <code>write</code> method acts as a pipe, in that it will
111:             * transfer the contents of the provided input stream to the ouput
112:             * stream. As this transfers the contents of the resource to the
113:             * provided output stream it will buffer the contents of the file
114:             * within its internal byte buffer so future requests are quicker.
115:             *
116:             * @param out the <code>OutputStream</code> to write the contents
117:             * @param in this is the <code>InputStream</code> to read from
118:             * @param size this is the number of bytes to read from the file 
119:             *
120:             * @exception IOException this is thrown if the issued stream has
121:             * an I/O problem writing the contents
122:             */
123:            private void write(OutputStream out, InputStream in, int size)
124:                    throws IOException {
125:                byte[] buf = new byte[size];
126:
127:                for (int off = 0; size > 0;) {
128:                    int len = in.read(buf, off, size);
129:
130:                    if (len < 0) {
131:                        break;
132:                    }
133:                    out.write(buf, off, len);
134:                    size -= len;
135:                    off += len;
136:                }
137:                cache = buf;
138:            }
139:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.