001: //========================================================================
002: //$Id: StreamEndPoint.java,v 1.1 2005/10/05 14:09:39 janb Exp $
003: //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.io.bio;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.io.OutputStream;
021:
022: import org.mortbay.io.Buffer;
023: import org.mortbay.io.EndPoint;
024:
025: /**
026: * @author gregw
027: *
028: * To change the template for this generated type comment go to
029: * Window - Preferences - Java - Code Generation - Code and Comments
030: */
031: public class StreamEndPoint implements EndPoint {
032: InputStream _in;
033: OutputStream _out;
034:
035: /**
036: *
037: */
038: public StreamEndPoint(InputStream in, OutputStream out) {
039: _in = in;
040: _out = out;
041: }
042:
043: public boolean isBlocking() {
044: return true;
045: }
046:
047: public boolean blockReadable(long millisecs) throws IOException {
048: return true;
049: }
050:
051: public boolean blockWritable(long millisecs) throws IOException {
052: return true;
053: }
054:
055: /*
056: * @see org.mortbay.io.BufferIO#isOpen()
057: */
058: public boolean isOpen() {
059: return _in != null;
060: }
061:
062: /*
063: * @see org.mortbay.io.BufferIO#close()
064: */
065: public void close() throws IOException {
066: if (_in != null)
067: _in.close();
068: _in = null;
069: if (_out != null)
070: _out.close();
071: _out = null;
072: }
073:
074: /* (non-Javadoc)
075: * @see org.mortbay.io.BufferIO#fill(org.mortbay.io.Buffer)
076: */
077: public int fill(Buffer buffer) throws IOException {
078: // TODO handle null array()
079: if (_in == null)
080: return 0;
081:
082: int space = buffer.space();
083: if (space <= 0) {
084: if (buffer.hasContent())
085: return 0;
086: throw new IOException("FULL");
087: }
088:
089: int len = buffer.readFrom(_in, space);
090:
091: return len;
092: }
093:
094: /* (non-Javadoc)
095: * @see org.mortbay.io.BufferIO#flush(org.mortbay.io.Buffer)
096: */
097: public int flush(Buffer buffer) throws IOException {
098: // TODO handle null array()
099: if (_out == null)
100: return -1;
101: int length = buffer.length();
102: if (length > 0)
103: buffer.writeTo(_out);
104: buffer.clear();
105: return length;
106: }
107:
108: /* (non-Javadoc)
109: * @see org.mortbay.io.BufferIO#flush(org.mortbay.io.Buffer, org.mortbay.io.Buffer, org.mortbay.io.Buffer)
110: */
111: public int flush(Buffer header, Buffer buffer, Buffer trailer)
112: throws IOException {
113: int len = 0;
114:
115: // TODO consider copying buffer and trailer into header if there is space.
116:
117: if (header != null) {
118: int tw = header.length();
119: if (tw > 0) {
120: int f = flush(header);
121: len = f;
122: if (f < tw)
123: return len;
124: }
125: }
126:
127: if (buffer != null) {
128: int tw = buffer.length();
129: if (tw > 0) {
130: int f = flush(buffer);
131: if (f < 0)
132: return len > 0 ? len : f;
133: len += f;
134: if (f < tw)
135: return len;
136: }
137: }
138:
139: if (trailer != null) {
140: int tw = trailer.length();
141: if (tw > 0) {
142: int f = flush(trailer);
143: if (f < 0)
144: return len > 0 ? len : f;
145: len += f;
146: }
147: }
148: return len;
149: }
150:
151: /* ------------------------------------------------------------ */
152: /*
153: * @see org.mortbay.io.EndPoint#getLocalAddr()
154: */
155: public String getLocalAddr() {
156: return null;
157: }
158:
159: /* ------------------------------------------------------------ */
160: /*
161: * @see org.mortbay.io.EndPoint#getLocalHost()
162: */
163: public String getLocalHost() {
164: return null;
165: }
166:
167: /* ------------------------------------------------------------ */
168: /*
169: * @see org.mortbay.io.EndPoint#getLocalPort()
170: */
171: public int getLocalPort() {
172: return 0;
173: }
174:
175: /* ------------------------------------------------------------ */
176: /*
177: * @see org.mortbay.io.EndPoint#getRemoteAddr()
178: */
179: public String getRemoteAddr() {
180: return null;
181: }
182:
183: /* ------------------------------------------------------------ */
184: /*
185: * @see org.mortbay.io.EndPoint#getRemoteHost()
186: */
187: public String getRemoteHost() {
188: return null;
189: }
190:
191: /* ------------------------------------------------------------ */
192: /*
193: * @see org.mortbay.io.EndPoint#getRemotePort()
194: */
195: public int getRemotePort() {
196: return 0;
197: }
198:
199: /* ------------------------------------------------------------ */
200: /*
201: * @see org.mortbay.io.EndPoint#getConnection()
202: */
203: public Object getTransport() {
204: return null;
205: }
206:
207: /* ------------------------------------------------------------ */
208: public InputStream getInputStream() {
209: return _in;
210: }
211:
212: /* ------------------------------------------------------------ */
213: public void setInputStream(InputStream in) {
214: _in = in;
215: }
216:
217: /* ------------------------------------------------------------ */
218: public OutputStream getOutputStream() {
219: return _out;
220: }
221:
222: /* ------------------------------------------------------------ */
223: public void setOutputStream(OutputStream out) {
224: _out = out;
225: }
226:
227: /* ------------------------------------------------------------ */
228: public void flush() throws IOException {
229: _out.flush();
230: }
231:
232: /* ------------------------------------------------------------ */
233: public boolean isBufferingInput() {
234: return false;
235: }
236:
237: /* ------------------------------------------------------------ */
238: public boolean isBufferingOutput() {
239: return false;
240: }
241:
242: /* ------------------------------------------------------------ */
243: public boolean isBufferred() {
244: return false;
245: }
246:
247: }
|