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: }
|