View Javadoc

    1   //========================================================================
    2   //$Id: StringEndPoint.java,v 1.1 2005/10/05 14:09:39 janb Exp $
    3   //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
    4   //------------------------------------------------------------------------
    5   //Licensed under the Apache License, Version 2.0 (the "License");
    6   //you may not use this file except in compliance with the License.
    7   //You may obtain a copy of the License at
    8   //http://www.apache.org/licenses/LICENSE-2.0
    9   //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  {
    32      String _encoding=StringUtil.__UTF8;
    33      ByteArrayInputStream _bin = new ByteArrayInputStream(new byte[0]);
    34      ByteArrayOutputStream _bout = new ByteArrayOutputStream();
    35
    36      public StringEndPoint()
    37      {
    38          super(null,null);
    39          _in=_bin;
    40          _out=_bout;
    41      }
    42
    43      public StringEndPoint(String encoding)
    44      	throws IOException
    45      {
    46          this();
    47          if (encoding!=null)
    48              _encoding=encoding;
    49      }
    50
    51      public void setInput(String s)
    52      {
    53          try
    54          {
    55              byte[] bytes = s.getBytes(_encoding);
    56              _bin=new ByteArrayInputStream(bytes);
    57              _in=_bin;
    58              _bout = new ByteArrayOutputStream();
    59              _out=_bout;
    60          }
    61          catch(Exception e)
    62          {
    63              throw new IllegalStateException(e.toString());
    64          }
    65      }
    66
    67      public String getOutput()
    68      {
    69          try
    70          {
    71              String s = new String(_bout.toByteArray(),_encoding);
    72              _bout.reset();
    73        	  return s;
    74          }
    75          catch(Exception e)
    76          {
    77              e.printStackTrace();
    78              throw new IllegalStateException(_encoding+": "+e.toString());
    79          }
    80      }
    81
    82      /**
    83       * @return <code>true</code> if there are bytes remaining to be read from the encoded input
    84       */
    85      public boolean hasMore()
    86      {
    87          return _bin.available()>0;
    88      }
    89  }