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  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      {
43          this();
44          if (encoding!=null)
45              _encoding=encoding;
46      }
47      
48      public void setInput(String s) 
49      {
50          try
51          {
52              byte[] bytes = s.getBytes(_encoding);
53              _bin=new ByteArrayInputStream(bytes);
54              _in=_bin;
55              _bout = new ByteArrayOutputStream();
56              _out=_bout;
57          }
58          catch(Exception e)
59          {
60              throw new IllegalStateException(e.toString());
61          }
62      }
63      
64      public String getOutput() 
65      {
66          try
67          {
68              String s = new String(_bout.toByteArray(),_encoding);
69              _bout.reset();
70        	  return s;
71          }
72          catch(Exception e)
73          {
74              e.printStackTrace();
75              throw new IllegalStateException(_encoding+": "+e.toString());
76          }
77      }
78  
79      /**
80       * @return <code>true</code> if there are bytes remaining to be read from the encoded input
81       */
82      public boolean hasMore()
83      {
84          return _bin.available()>0;
85      }   
86  }