From 9d3717abd59e1672f5d8d7888ce613afdc7fb3c5 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Wed, 24 Jan 2024 13:00:11 +0000
Subject: [PATCH] HttpResponder.finish aufgeteilt

---
 src/de/uhilger/httpserver/base/HttpResponder.java |   57 ++++++++++++++++++++++++---------------------------------
 1 files changed, 24 insertions(+), 33 deletions(-)

diff --git a/src/de/uhilger/httpserver/base/HttpResponder.java b/src/de/uhilger/httpserver/base/HttpResponder.java
index 6d9a85e..88ad962 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;
@@ -43,15 +42,10 @@
   public static final String CONTENT_TYPE = "Content-Type";
   public static final String LAST_MODIFIED_DATE_HEADER = "Last-Modified";
 
-  //public static final String RANGE_HEADER = "Range";
-  //public static final String CONTENT_RANGE_HEADER = "Content-Range";
-
   /* Statuscodes */
   public static final int SC_OK = 200;
   public static final int SC_NOT_FOUND = 404;
   
-  //public static final int SC_PARTIAL_CONTENT = 206;
-
   /* HTTP Methoden */
   public static final String HTTP_GET = "GET";
   
@@ -59,13 +53,6 @@
   public static final String STR_BYTES = "bytes";
   public static final String STR_NOT_FOUND = " not found.";
   public static final String LM_PATTERN = "EEE, dd MMM yyyy HH:mm:ss zzz";
-  
-  //public static final String STR_BLANK = " ";
-  //public static final String STR_DASH = "-";
-  //public static final String STR_COMMA = ",";
-  //public static final String STR_DOT = ".";
-  //public static final String RANGE_PATTERN = "[^\\d-,]";
-  //public static final String WELCOME_FILE = "index.html";
   
   /**
    * Den Inhalt einer Datei ausliefern
@@ -82,23 +69,34 @@
       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();
+    finish(out);
+  }
+
+  public void finish(OutputStream out) throws IOException {
+    out.flush();
+    out.close();
+  }
+  
   /**
    * Die Header erzeugen, die unabhängig davon, ob der ganze 
    * Inhalt oder nur Teile davon ausgeliefert werden sollen, in der 
@@ -130,13 +128,7 @@
    * @throws IOException falls etwas schief geht entsteht dieser Fehler
    */
   public void sendNotFound(HttpExchange e, String fname) throws IOException {
-    OutputStream os = e.getResponseBody();
-    String response = fname + STR_NOT_FOUND;
-    byte[] bytes = response.getBytes(StandardCharsets.UTF_8);
-    e.sendResponseHeaders(SC_NOT_FOUND, bytes.length);
-    os.write(bytes);
-    os.flush();
-    os.close();
+    antwortSenden(e, SC_NOT_FOUND, fname + STR_NOT_FOUND);
   }  
 
   public void antwortSenden(HttpExchange exchange, int code, String antwort) throws IOException {
@@ -144,8 +136,7 @@
     exchange.sendResponseHeaders(code, bytes.length);
     OutputStream os = exchange.getResponseBody();
     os.write(bytes);
-    os.flush();
-    os.close();
+    finish(os);
   }
 
 

--
Gitblit v1.9.3