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