1 // ========================================================================
2 // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // ========================================================================
13
14 package org.eclipse.jetty.io.bio;
15
16 import java.io.ByteArrayInputStream;
17 import java.io.ByteArrayOutputStream;
18
19 import org.eclipse.jetty.util.StringUtil;
20
21 /**
22 *
23 *
24 * To change the template for this generated type comment go to
25 * Window - Preferences - Java - Code Generation - Code and Comments
26 */
27 public class StringEndPoint extends StreamEndPoint
28 {
29 String _encoding=StringUtil.__UTF8;
30 ByteArrayInputStream _bin = new ByteArrayInputStream(new byte[0]);
31 ByteArrayOutputStream _bout = new ByteArrayOutputStream();
32
33 public StringEndPoint()
34 {
35 super(null,null);
36 _in=_bin;
37 _out=_bout;
38 }
39
40 public StringEndPoint(String encoding)
41 {
42 this();
43 if (encoding!=null)
44 _encoding=encoding;
45 }
46
47 public void setInput(String s)
48 {
49 try
50 {
51 byte[] bytes = s.getBytes(_encoding);
52 _bin=new ByteArrayInputStream(bytes);
53 _in=_bin;
54 _bout = new ByteArrayOutputStream();
55 _out=_bout;
56 }
57 catch(Exception e)
58 {
59 throw new IllegalStateException(e.toString());
60 }
61 }
62
63 public String getOutput()
64 {
65 try
66 {
67 String s = new String(_bout.toByteArray(),_encoding);
68 _bout.reset();
69 return s;
70 }
71 catch(Exception e)
72 {
73 e.printStackTrace();
74 throw new IllegalStateException(_encoding+": "+e.toString());
75 }
76 }
77
78 /**
79 * @return <code>true</code> if there are bytes remaining to be read from the encoded input
80 */
81 public boolean hasMore()
82 {
83 return _bin.available()>0;
84 }
85 }