title: Passing along a data stream using Netflix Feign
body: <p>At work we use Netflix's <a href="https://github.com/Netflix/feign/blob/710191fe53fb619ce266ebd110e6d54a315720a3/core/src/main/java/feign/Client.java" rel="noreferrer">Feign Client</a> to help with requests between services. However, I'm confused about its apparent lack of ability to stream data, especially given Netflix's well known business model of streaming video. I clearly am missing something here.</p>
  
  <p>To explain, say <code>Service A</code> asks the Feign Client of <code>Service B</code> for a stream of data and <code>Service B</code> sends the stream in the response. At this point, the <code>execute()</code> method in the Feign Client gets called:</p>
  
  <pre><code>@Override public Response execute(Request request, Options options) throws IOException {
    HttpURLConnection connection = convertAndSend(request, options);
    return convertResponse(connection);
  }
  
  HttpURLConnection convertAndSend(Request request, Options options) throws IOException {
    final HttpURLConnection connection = (HttpURLConnection) new URL(request.url()).openConnection();
  
    /** SNIP **/
  
    if (request.body() != null) {
      if (contentLength != null) {
        connection.setFixedLengthStreamingMode(contentLength);
      } else {
        connection.setChunkedStreamingMode(8196);
      }
      connection.setDoOutput(true);
      OutputStream out = connection.getOutputStream();
      if (gzipEncodedRequest) {
        out = new GZIPOutputStream(out);
      }
      try {
        out.write(request.body()); // PROBLEM
      } finally {
        try {
          out.close();
        } catch (IOException suppressed) {
        }
      }
    }
    return connection;
  }
  </code></pre>
  
  <p>The line labelled <code>PROBLEM</code> is what confuses me.</p>
  
  <ol>
  <li>The <code>request</code> object doesn't even have any sort of stream to read, just a <code>byte[] body</code>.</li>
  <li>On the outgoing end, the entire body is written into the <code>OutputStream</code> at once. Shouldn't it chunk the data instead?</li>
  </ol>
  
  <p>For example</p>
  
  <pre><code>// pseudocode
  try {
    location = 0
    bufferSize = 2048
    buffer = request.body().read(location, bufferSize)
    while(out.readyToRead() &amp;&amp; buffer.length &gt; 0) {
      out.write(buffer)
      location += bufferSize
      buffer = request.body().read(location, bufferSize)
    }
  }
  </code></pre>
  
  <p>If the request had a stream instead of just <code>byte[] body</code>, you could improve that even further to send data as it becomes available.</p>
  
  <p>I'm very new to this area of service architecture. What am I missing?</p>