View Javadoc

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              _ishut=false;
57              _oshut=false;
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  }