Code example for OutputStreamWriter

Methods: append, write

0
	@Override 
	protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException,
			HttpMessageNotWritableException 
	{ 
 
		OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(),
				getCharset(outputMessage.getHeaders()));
 
		try 
		{ 
			Type typeOfSrc = getType();
 
			if (LOG.isTraceEnabled()) 
			{ 
				StringBuilder sb = new StringBuilder();
				if (this.prefixJson)
				{ 
					sb.append("{} && ");
				} 
 
				if (typeOfSrc != null)
				{ 
					sb.append(gson.toJson(o, typeOfSrc));
				} 
				else 
				{ 
					sb.append(gson.toJson(o));
				} 
 
				LOG.debug("Json response:\n" + sb.toString());
				writer.write(sb.toString());
			} 
			else 
			{ 
				if (this.prefixJson)
				{ 
					writer.append("{} && ");
				} 
 
				if (typeOfSrc != null)
				{ 
					this.gson.toJson(o, typeOfSrc, writer);
				} 
				else 
				{ 
					this.gson.toJson(o, writer);
				} 
			} 
		} 
		catch (JsonIOException ex)
		{ 
			throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
		} 
		finally 
		{ 
			IOUtils.closeQuietly(writer);
		} 
 
	} 
 
	// helpers