title: jersey - StreamingOutput as Response entity
body: <p>I had implemented streaming output in my Jersey Resource class.</p>
  
  <pre><code>@GET
  @Path("xxxxx")
  @Produces(BulkConstants.TEXT_XML_MEDIA_TYPE})   
  public Response getFile() {
  
      FeedReturnStreamingOutput sout = new FeedReturnStreamingOutput();
      response = Response.ok(sout).build();
      return response;
  }
  
  class FeedReturnStreamingOutput implements StreamingOutput {
  
      public FeedReturnStreamingOutput()
  
      @Override
      public void write(OutputStream outputStream)  {
          //write into Output Stream
      }
  }
  </code></pre>
  
  <p>The problem is eventhough a response is sent back from the resource before FeedReturnStreamingOutput is called Jersey client waits until FeedReturnStreamingOutput execution is completed.</p>
  
  <p>Client Code :</p>
  
  <pre><code>Client client = Client.create();
  
  ClientResponse response = webResource
      //headers
      .get(ClientResponse.class);
  
  //The codes underneath executes after FeedReturnStreamingOutput is executed which undermines the necessity of streaming
  
  OutputStream os = new FileOutputStream("c:\\test\\feedoutput5.txt");
  System.out.println(new Date() + " : Reached point A");
  
  if (response.getStatus() == 200) {
      System.out.println(new Date() + " : Reached point B");
      InputStream io = response.getEntityInputStream();
  
      byte[] buff = new byte[1024000];
      int count = 0;
  
      while ((count = io.read(buff, 0, buff.length)) != -1) {
          os.write(buff, 0, count);
      }
  
      os.close();
      io.close();
  
  } else {
      System.out.println("Response code :" + response.getStatus());
  }
  
  System.out.println("Time taken --&gt;&gt; "+(System.currentTimeMillis()-startTime)+" ms");
  </code></pre>