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 import java.io.IOException;
19
20 import org.eclipse.jetty.util.StringUtil;
21
22 /**
23 *
24 *
25 * To change the template for this generated type comment go to
26 * Window - Preferences - Java - Code Generation - Code and Comments
27 */
28 public class StringEndPoint extends StreamEndPoint
29 {
30 String _encoding=StringUtil.__UTF8;
31 ByteArrayInputStream _bin = new ByteArrayInputStream(new byte[0]);
32 ByteArrayOutputStream _bout = new ByteArrayOutputStream();
33
34 public StringEndPoint()
35 {
36 super(null,null);
37 _in=_bin;
38 _out=_bout;
39 }
40
41 public StringEndPoint(String encoding)
42 throws IOException
43 {
44 this();
45 if (encoding!=null)
46 _encoding=encoding;
47 }
48
49 public void setInput(String s)
50 {
51 try
52 {
53 byte[] bytes = s.getBytes(_encoding);
54 _bin=new ByteArrayInputStream(bytes);
55 _in=_bin;
56 _bout = new ByteArrayOutputStream();
57 _out=_bout;
58 }
59 catch(Exception e)
60 {
61 throw new IllegalStateException(e.toString());
62 }
63 }
64
65 public String getOutput()
66 {
67 try
68 {
69 String s = new String(_bout.toByteArray(),_encoding);
70 _bout.reset();
71 return s;
72 }
73 catch(Exception e)
74 {
75 e.printStackTrace();
76 throw new IllegalStateException(_encoding+": "+e.toString());
77 }
78 }
79
80 /**
81 * @return <code>true</code> if there are bytes remaining to be read from the encoded input
82 */
83 public boolean hasMore()
84 {
85 return _bin.available()>0;
86 }
87 }