01: //========================================================================
02: //$Id: StringEndPoint.java,v 1.1 2005/10/05 14:09:39 janb Exp $
03: //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
04: //------------------------------------------------------------------------
05: //Licensed under the Apache License, Version 2.0 (the "License");
06: //you may not use this file except in compliance with the License.
07: //You may obtain a copy of the License at
08: //http://www.apache.org/licenses/LICENSE-2.0
09: //Unless required by applicable law or agreed to in writing, software
10: //distributed under the License is distributed on an "AS IS" BASIS,
11: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: //See the License for the specific language governing permissions and
13: //limitations under the License.
14: //========================================================================
15:
16: package org.mortbay.io.bio;
17:
18: import java.io.ByteArrayInputStream;
19: import java.io.ByteArrayOutputStream;
20: import java.io.IOException;
21:
22: import org.mortbay.util.StringUtil;
23:
24: /**
25: * @author gregw
26: *
27: * To change the template for this generated type comment go to
28: * Window - Preferences - Java - Code Generation - Code and Comments
29: */
30: public class StringEndPoint extends StreamEndPoint {
31: String _encoding = StringUtil.__UTF8;
32: ByteArrayInputStream _bin = new ByteArrayInputStream(new byte[0]);
33: ByteArrayOutputStream _bout = new ByteArrayOutputStream();
34:
35: public StringEndPoint() {
36: super (null, null);
37: _in = _bin;
38: _out = _bout;
39: }
40:
41: public StringEndPoint(String encoding) throws IOException {
42: this ();
43: if (encoding != null)
44: _encoding = encoding;
45: }
46:
47: public void setInput(String s) {
48: try {
49: byte[] bytes = s.getBytes(_encoding);
50: _bin = new ByteArrayInputStream(bytes);
51: _in = _bin;
52: _bout = new ByteArrayOutputStream();
53: _out = _bout;
54: } catch (Exception e) {
55: throw new IllegalStateException(e.toString());
56: }
57: }
58:
59: public String getOutput() {
60: try {
61: String s = new String(_bout.toByteArray(), _encoding);
62: _bout.reset();
63: return s;
64: } catch (Exception e) {
65: e.printStackTrace();
66: throw new IllegalStateException(_encoding + ": "
67: + e.toString());
68: }
69: }
70:
71: /**
72: * @return <code>true</code> if there are bytes remaining to be read from the encoded input
73: */
74: public boolean hasMore() {
75: return _bin.available() > 0;
76: }
77: }
|