001: //========================================================================
002: //$Id: ByteArrayEndPoint.java,v 1.2 2005/11/05 08:32:41 gregwilkins 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;
017:
018: import java.io.IOException;
019:
020: /* ------------------------------------------------------------ */
021: /** ByteArrayEndPoint.
022: * @author gregw
023: *
024: */
025: public class ByteArrayEndPoint implements EndPoint {
026: byte[] _inBytes;
027: ByteArrayBuffer _in;
028: ByteArrayBuffer _out;
029: boolean _closed;
030:
031: /* ------------------------------------------------------------ */
032: /**
033: *
034: */
035: public ByteArrayEndPoint() {
036: }
037:
038: /* ------------------------------------------------------------ */
039: /**
040: *
041: */
042: public ByteArrayEndPoint(byte[] input, int outputSize) {
043: _inBytes = input;
044: _in = new ByteArrayBuffer(input);
045: _out = new ByteArrayBuffer(outputSize);
046: }
047:
048: /* ------------------------------------------------------------ */
049: /**
050: * @return Returns the in.
051: */
052: public ByteArrayBuffer getIn() {
053: return _in;
054: }
055:
056: /* ------------------------------------------------------------ */
057: /**
058: * @param in The in to set.
059: */
060: public void setIn(ByteArrayBuffer in) {
061: _in = in;
062: }
063:
064: /* ------------------------------------------------------------ */
065: /**
066: * @return Returns the out.
067: */
068: public ByteArrayBuffer getOut() {
069: return _out;
070: }
071:
072: /* ------------------------------------------------------------ */
073: /**
074: * @param out The out to set.
075: */
076: public void setOut(ByteArrayBuffer out) {
077: _out = out;
078: }
079:
080: /* ------------------------------------------------------------ */
081: /*
082: * @see org.mortbay.io.EndPoint#isOpen()
083: */
084: public boolean isOpen() {
085: return !_closed;
086: }
087:
088: /* ------------------------------------------------------------ */
089: /*
090: * @see org.mortbay.io.EndPoint#isBlocking()
091: */
092: public boolean isBlocking() {
093: return false;
094: }
095:
096: /* ------------------------------------------------------------ */
097: public boolean blockReadable(long millisecs) {
098: return true;
099: }
100:
101: /* ------------------------------------------------------------ */
102: public boolean blockWritable(long millisecs) {
103: return true;
104: }
105:
106: /* ------------------------------------------------------------ */
107: /*
108: * @see org.mortbay.io.EndPoint#close()
109: */
110: public void close() throws IOException {
111: _closed = true;
112: }
113:
114: /* ------------------------------------------------------------ */
115: /*
116: * @see org.mortbay.io.EndPoint#fill(org.mortbay.io.Buffer)
117: */
118: public int fill(Buffer buffer) throws IOException {
119: if (_closed)
120: throw new IOException("CLOSED");
121: if (_in.length() <= 0)
122: return -1;
123: int len = buffer.put(_in);
124: _in.skip(len);
125: return len;
126: }
127:
128: /* ------------------------------------------------------------ */
129: /*
130: * @see org.mortbay.io.EndPoint#flush(org.mortbay.io.Buffer)
131: */
132: public int flush(Buffer buffer) throws IOException {
133: if (_closed)
134: throw new IOException("CLOSED");
135: int len = _out.put(buffer);
136: buffer.skip(len);
137: return len;
138: }
139:
140: /* ------------------------------------------------------------ */
141: /*
142: * @see org.mortbay.io.EndPoint#flush(org.mortbay.io.Buffer, org.mortbay.io.Buffer, org.mortbay.io.Buffer)
143: */
144: public int flush(Buffer header, Buffer buffer, Buffer trailer)
145: throws IOException {
146: if (_closed)
147: throw new IOException("CLOSED");
148: int flushed = 0;
149: if (header != null && header.length() > 0) {
150: int len = _out.put(header);
151: header.skip(len);
152: flushed += len;
153: }
154:
155: if (header == null || header.length() == 0) {
156: if (buffer != null && buffer.length() > 0) {
157: int len = _out.put(buffer);
158: buffer.skip(len);
159: flushed += len;
160: }
161:
162: if (buffer == null || buffer.length() == 0) {
163: if (trailer != null && trailer.length() > 0) {
164: int len = _out.put(trailer);
165: trailer.skip(len);
166: flushed += len;
167: }
168: }
169: }
170:
171: return flushed;
172: }
173:
174: /* ------------------------------------------------------------ */
175: /**
176: *
177: */
178: public void reset() {
179: _closed = false;
180: _in.clear();
181: _out.clear();
182: if (_inBytes != null)
183: _in.setPutIndex(_inBytes.length);
184: }
185:
186: /* ------------------------------------------------------------ */
187: /*
188: * @see org.mortbay.io.EndPoint#getLocalAddr()
189: */
190: public String getLocalAddr() {
191: return null;
192: }
193:
194: /* ------------------------------------------------------------ */
195: /*
196: * @see org.mortbay.io.EndPoint#getLocalHost()
197: */
198: public String getLocalHost() {
199: return null;
200: }
201:
202: /* ------------------------------------------------------------ */
203: /*
204: * @see org.mortbay.io.EndPoint#getLocalPort()
205: */
206: public int getLocalPort() {
207: return 0;
208: }
209:
210: /* ------------------------------------------------------------ */
211: /*
212: * @see org.mortbay.io.EndPoint#getRemoteAddr()
213: */
214: public String getRemoteAddr() {
215: return null;
216: }
217:
218: /* ------------------------------------------------------------ */
219: /*
220: * @see org.mortbay.io.EndPoint#getRemoteHost()
221: */
222: public String getRemoteHost() {
223: return null;
224: }
225:
226: /* ------------------------------------------------------------ */
227: /*
228: * @see org.mortbay.io.EndPoint#getRemotePort()
229: */
230: public int getRemotePort() {
231: return 0;
232: }
233:
234: /* ------------------------------------------------------------ */
235: /*
236: * @see org.mortbay.io.EndPoint#getConnection()
237: */
238: public Object getTransport() {
239: return _inBytes;
240: }
241:
242: /* ------------------------------------------------------------ */
243: public void flush() throws IOException {
244: }
245:
246: /* ------------------------------------------------------------ */
247: public boolean isBufferingInput() {
248: return false;
249: }
250:
251: /* ------------------------------------------------------------ */
252: public boolean isBufferingOutput() {
253: return false;
254: }
255:
256: /* ------------------------------------------------------------ */
257: public boolean isBufferred() {
258: return false;
259: }
260:
261: }
|