Permalink
Browse files

o add deliverBytes behavior

  • Loading branch information...
1 parent 1b2be1b commit cb4b204429500eb168d362ba9f660d29e0a430ae @nabcos nabcos committed Jun 26, 2012
@@ -41,6 +41,17 @@ public static Pause pause( Time time )
return Pause.pause( time );
}
+ public static Content content( byte[] content )
+ {
+ return Content.content( content );
+ }
+
+ public static Content content( byte[] content, String type )
+ {
+ return Content.content( content, type );
+ }
+
+
public static Content content( String content )
{
return Content.content( content );
@@ -40,6 +40,19 @@
private File file;
+ private byte[] bytes;
+
+ public Content( final byte[] content )
+ {
+ this( content, "application/octet-stream" );
+ }
+
+ public Content( final byte[] content, String type )
+ {
+ this.bytes = content;
+ this.type = type;
+ }
+
public Content( final File content )
{
this( content, "application/octet-stream" );
@@ -71,6 +84,15 @@ public static Content content( String content, String type )
return new Content( content, type );
}
+ public static Content content( byte[] content )
+ {
+ return new Content( content );
+ }
+
+ public static Content content( byte[] content, String type ) {
+ return new Content( content, type );
+ }
+
public Content()
{
this.type = "text/plain";
@@ -92,7 +114,9 @@ public boolean execute( HttpServletRequest request, HttpServletResponse response
{
if ( "GET".equals( request.getMethod() ) )
{
- if ( file != null )
+ if ( bytes != null ) {
+ deliverBytes( response, bytes );
+ } else if ( file != null )
{
deliverFile( request, response );
}
@@ -111,9 +135,16 @@ public boolean execute( HttpServletRequest request, HttpServletResponse response
private void deliverFile( final HttpServletRequest request, final HttpServletResponse response )
throws IOException
{
+
+ deliverBytes( response, Files.toByteArray( file ) );
+ }
+
+ private void deliverBytes( final HttpServletResponse response, final byte[] b )
+ throws IOException
+ {
response.setContentType( type );
- response.setContentLength( (int) file.length() );
- response.getOutputStream().write( Files.toByteArray( file ) );
+ response.setContentLength( b.length );
+ response.getOutputStream().write( b );
}
private void deliverString( final HttpServletRequest request, final HttpServletResponse response,

0 comments on commit cb4b204

Please sign in to comment.