Source Code Cross Referenced for CSBufferedInputStream.java in  » Portal » Open-Portal » com » sun » portal » rproxy » connectionhandler » 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 » Portal » Open Portal » com.sun.portal.rproxy.connectionhandler 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * CSBufferedInputStream.java
003:         *
004:         * $Author: ss150821 $
005:         *
006:         * $Date: 2005/11/30 11:27:20 $ $Revision: 1.3 $
007:         *
008:         * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
009:         *
010:         * Developed by SunPS and SunIR
011:         */
012:
013:        package com.sun.portal.rproxy.connectionhandler;
014:
015:        import java.io.BufferedInputStream;
016:        import java.io.IOException;
017:        import java.io.InputStream;
018:
019:        /**
020:         * This class extends the BufferedInputStream. It adds a member reference to the
021:         * socket that is associated with the input stream.
022:         * 
023:         * Members _socket - the socket associated with this input stream
024:         * 
025:         * @author Kevin Hartig
026:         * @see CachedSocket
027:         */
028:
029:        public class CSBufferedInputStream extends BufferedInputStream {
030:
031:            private CachedSocket _socket = null;
032:
033:            private int _expectedLength = 0;
034:
035:            private int _currentPos = 0; // the current number of bytes read from the
036:
037:            // stream
038:
039:            private boolean _keepAlive = false;
040:
041:            /**
042:             * Constructor
043:             */
044:            public CSBufferedInputStream(InputStream in, CachedSocket socket,
045:                    int length) {
046:                this (in);
047:                _socket = socket;
048:                _expectedLength = length;
049:            }
050:
051:            /**
052:             * Constructor
053:             */
054:            public CSBufferedInputStream(InputStream in) {
055:                super (in);
056:            }
057:
058:            /**
059:             * Set the socket reference to be associated with this input stream.
060:             */
061:            public void setSocket(CachedSocket socket) {
062:                _socket = socket;
063:            }
064:
065:            /**
066:             * Get the socket reference associated with this buffered input stream.
067:             * 
068:             * @return a reference to the CachedSocket associated with this input
069:             *         stream.
070:             */
071:            public CachedSocket getSocket() {
072:                return _socket;
073:            }
074:
075:            /**
076:             * Set the length (in bytes) of the associated input stream.
077:             * 
078:             * @param length
079:             *            the number of bytes expected to be in the stream.
080:             */
081:            public void setLength(int length) {
082:                // System.out.println("Length set to ===== "+length);
083:                _expectedLength = length;
084:                _currentPos = 0;
085:            }
086:
087:            /**
088:             * Get the current expected number of bytes in the input stream
089:             * 
090:             * @return The number of bytes expected to be in the input stream.
091:             */
092:            public int getLength() {
093:                return _expectedLength;
094:            }
095:
096:            /**
097:             * Get the current position in the buffer.
098:             * 
099:             * @return Returns the current position in the buffer.
100:             */
101:            public int getPos() {
102:                return _currentPos;
103:            }
104:
105:            /**
106:             * Set the _keepAlive flag to true.
107:             */
108:            public void setKeepAlive() {
109:                _keepAlive = true;
110:            }
111:
112:            /**
113:             * Set the _keepalive flag to false. This indicates that the socket
114:             * associated with this stream needs to be closed after reading the buffer.
115:             */
116:            public void setClosed() {
117:                _keepAlive = false;
118:            }
119:
120:            /**
121:             * Check if trying to read passed the expected number of bytes in the
122:             * stream. otherwise just read from the stream normally.
123:             */
124:            public synchronized int read() {
125:                int b = -1;
126:                // System.out.println("cp, expectedLength = "+" "+_currentPos+"
127:                // "+_expectedLength);
128:                try {
129:                    if (_expectedLength != -1) {
130:                        if ((_currentPos == _expectedLength) && _keepAlive) {
131:                            readToEnd();
132:                            _currentPos = 0;
133:                            _socket.setIdle();
134:                            // _socket.close();
135:                            b = -1;
136:                        } else {
137:                            b = super .read();
138:                            _currentPos++;
139:                            if (b == -1) {
140:                                _currentPos = 0;
141:                                _socket.close();
142:                            }
143:                        }
144:                    } else {
145:                        b = super .read();
146:                        if (b == -1)
147:                            _socket.close();
148:                    }
149:                } catch (IOException ioe) {
150:                    _currentPos = 0;
151:                    b = -1;
152:                    try {
153:                        _socket.close();
154:                    } catch (IOException e) {
155:                    }
156:                }
157:                return b;
158:            }
159:
160:            /**
161:             * Check if trying to read passed the expected number of bytes in the
162:             * stream. otherwise just read from the stream normally.
163:             */
164:            public synchronized int read(byte b[], int off, int len) {
165:                int c = -1;
166:                try {
167:                    // System.out.println("len, cp, off, expectedLength = "+len+"
168:                    // "+_currentPos+" "+off+" "+_expectedLength);
169:                    if ((_expectedLength != -1 && _currentPos == _expectedLength)
170:                            && _keepAlive) {
171:                        readToEnd();
172:                        _currentPos = 0;
173:                        _socket.setIdle();
174:                        // _socket.close();
175:                        c = -1;
176:                    } else if (_expectedLength != -1) {
177:                        if (_currentPos + len > _expectedLength) {
178:                            len = _expectedLength - _currentPos;
179:                        }
180:                        if (len == 0) {
181:                            _socket.close();
182:                            c = -1;
183:                        } else {
184:                            c = super .read(b, off, len);
185:                            // System.out.println("Number bytes read = "+c);
186:                            if (c != -1) {
187:                                _currentPos = _currentPos + c;
188:                            } else {
189:                                _socket.close();
190:                            }
191:                        }
192:                    } else {
193:                        if (len == 0) {
194:                            _socket.close();
195:                            c = -1;
196:                        } else {
197:                            c = super .read(b, off, len);
198:                            if (c == -1) {
199:                                _socket.close();
200:                            }
201:                        }
202:                    }
203:                } // end try
204:                catch (IOException ioe) {
205:                    _currentPos = 0;
206:                    c = -1;
207:                    try {
208:                        _socket.close();
209:                    } catch (IOException e) {
210:                    }
211:                }
212:                return c;
213:            }
214:
215:            /**
216:             * 
217:             */
218:            public synchronized long skip(long n) {
219:                long numSkipped = 0;
220:
221:                try {
222:                    numSkipped = super .skip(n);
223:                    _currentPos += numSkipped;
224:                } catch (IOException ioe) {
225:                    try {
226:                        _socket.close();
227:                    } catch (Exception e) {
228:                    }
229:                }
230:                return numSkipped;
231:            }
232:
233:            public synchronized void readToEnd() {
234:                if (_expectedLength != -1) {
235:                    while (_currentPos < _expectedLength && read() != -1) {
236:                        // System.out.println("Reading to end of stream in readToEnd");
237:                    }
238:                }
239:            }
240:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.