From f5728b1d24410f076777c1a6cd409c5e65d72051 Mon Sep 17 00:00:00 2001 From: ulrich Date: Sat, 03 Jul 2021 08:27:56 +0000 Subject: [PATCH] FileHandler aufgeraeumt --- src/de/uhilger/httpserver/base/HttpResponder.java | 30 ++++++++++++++++++------------ 1 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/de/uhilger/httpserver/base/HttpResponder.java b/src/de/uhilger/httpserver/base/HttpResponder.java index fee4aad..69cc132 100644 --- a/src/de/uhilger/httpserver/base/HttpResponder.java +++ b/src/de/uhilger/httpserver/base/HttpResponder.java @@ -24,7 +24,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.text.SimpleDateFormat; import java.util.Date; @@ -82,23 +81,30 @@ e.sendResponseHeaders(SC_OK, file.length()); if(HTTP_GET.equalsIgnoreCase(e.getRequestMethod())) { InputStream in = new FileInputStream(file); - OutputStream os = e.getResponseBody(); - byte[] b = new byte[4096]; - int bytesRead = in.read(b); - //int b = in.read(); - while (bytesRead > -1) { - os.write(b, 0, bytesRead); - bytesRead = in.read(b); - } - in.close(); - os.flush(); - os.close(); + OutputStream os = e.getResponseBody(); + write(in, os); + finish(in, os); } } else { sendNotFound(e, file.getName()); } } + public void write(InputStream in, OutputStream out) throws IOException { + byte[] b = new byte[4096]; + int bytesRead = in.read(b); + while (bytesRead > -1) { + out.write(b, 0, bytesRead); + bytesRead = in.read(b); + } + } + + public void finish(InputStream in, OutputStream out) throws IOException { + in.close(); + out.flush(); + out.close(); + } + /** * Die Header erzeugen, die unabhängig davon, ob der ganze * Inhalt oder nur Teile davon ausgeliefert werden sollen, in der -- Gitblit v1.9.3