Sun HttpServer not returning binary entity with status code 500

7 views
Skip to first unread message

Albert Latacz

unread,
Jun 17, 2014, 9:03:23 AM6/17/14
to utter...@googlegroups.com
We have spotted an anomaly with Sun's HttpServer whereby it doesn't return any response for binary entity when status code is 500.

for the code below

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.*;
import java.net.InetSocketAddress;

public class Learning {
  private static void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
    int read;
    byte[] buffer = new byte[4096];
    while ((read = inputStream.read(buffer)) > 0) {
      outputStream.write(buffer, 0, read);
    }
  }

  public static void main(String[] args) throws IOException {
    HttpServer server = HttpServer.create(new InetSocketAddress("0.0.0.0", 9003), 0);
    server.createContext("/", new HttpHandler() {

      @Override
      public void handle(HttpExchange httpExchange) throws IOException {
        httpExchange.getResponseHeaders().add("Content-Type", "application/octet-stream");
        InputStream inputStream = getClass().getResourceAsStream("example.xls");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        copy(inputStream, outputStream);
        byte[] bytes = outputStream.toByteArray();

        httpExchange.sendResponseHeaders(500, bytes.length);
        copy(new ByteArrayInputStream(bytes), httpExchange.getResponseBody());

        httpExchange.close();
      }
    });
    server.start();
  }
}

use example.xls file attached.

Is there something we're missing?

Thanks
Albert & Mike



example.xls
Reply all
Reply to author
Forward
0 new messages