PageRenderTime 25ms CodeModel.GetById 18ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/test/src/solidstack/httpclient/RequestWriter.java

http://solidstack.googlecode.com/
Java | 156 lines | 137 code | 17 blank | 2 comment | 7 complexity | 252345fdaac8d711a88a5bce7b91581b MD5 | raw file
Possible License(s): Apache-2.0
  1package solidstack.httpclient;
  2
  3import java.io.IOException;
  4import java.io.OutputStream;
  5import java.io.OutputStreamWriter;
  6import java.io.UnsupportedEncodingException;
  7import java.io.Writer;
  8
  9import solidstack.httpserver.HttpException;
 10
 11public class RequestWriter extends Writer
 12{
 13	protected OutputStreamWriter writer;
 14	protected String contentType;
 15
 16	public RequestWriter( OutputStream out, String charsetName, String contentType )
 17	{
 18		super( out /* = lock object */ );
 19
 20		this.contentType = contentType;
 21		try
 22		{
 23			this.writer = new OutputStreamWriter( out, charsetName );
 24		}
 25		catch( UnsupportedEncodingException e )
 26		{
 27			throw new HttpException( e );
 28		}
 29	}
 30
 31	public RequestWriter( OutputStream out, String charsetName )
 32	{
 33		this( out, charsetName, null );
 34	}
 35
 36	@Override
 37	public void write( char[] cbuf, int off, int len )
 38	{
 39		try
 40		{
 41			this.writer.write( cbuf, off, len );
 42		}
 43		catch( IOException e )
 44		{
 45			throw new HttpException( e );
 46		}
 47	}
 48
 49	@Override
 50	public void write( int c )
 51	{
 52		try
 53		{
 54			this.writer.write( c );
 55		}
 56		catch( IOException e )
 57		{
 58			throw new HttpException( e );
 59		}
 60	}
 61
 62	@Override
 63	public void write( char[] cbuf )
 64	{
 65		try
 66		{
 67			this.writer.write( cbuf );
 68		}
 69		catch( IOException e )
 70		{
 71			throw new HttpException( e );
 72		}
 73	}
 74
 75	@Override
 76	public void write( String str )
 77	{
 78		try
 79		{
 80			this.writer.write( str );
 81		}
 82		catch( IOException e )
 83		{
 84			throw new HttpException( e );
 85		}
 86	}
 87
 88	@Override
 89	public void write( String str, int off, int len )
 90	{
 91		try
 92		{
 93			this.writer.write( str, off, len );
 94		}
 95		catch( IOException e )
 96		{
 97			throw new HttpException( e );
 98		}
 99	}
100
101	public void writeEncoded( String str )
102	{
103		if( this.contentType != null && this.contentType.equals( "text/html" ) )
104		{
105			for( char ch : str.toCharArray() )
106			{
107				switch( ch )
108				{
109					case '<': write( "&lt;" ); break;
110					case '>': write( "&gt;" ); break;
111					case '&': write( "&amp;" ); break;
112					default: write( ch );
113				}
114			}
115		}
116		else
117			write( str );
118	}
119
120	public void writeEncoded( Object o )
121	{
122		if( o != null )
123			writeEncoded( o.toString() );
124	}
125
126	public void write( Object o )
127	{
128		write( o != null ? o.toString() : "null" );
129	}
130
131	@Override
132	public void flush()
133	{
134		try
135		{
136			this.writer.flush();
137		}
138		catch( IOException e )
139		{
140			throw new HttpException( e );
141		}
142		// The reponse writer does not need to flush.
143		// We don't want to trigger the flushing of the output stream.
144	}
145
146	@Override
147	public void close()
148	{
149		throw new UnsupportedOperationException();
150	}
151
152	public String getEncoding()
153	{
154		return this.writer.getEncoding();
155	}
156}