99import express .utils .Utils ;
1010
1111import java .io .File ;
12+ import java .io .FileInputStream ;
1213import java .io .IOException ;
1314import java .io .OutputStream ;
14- import java .nio .file .Files ;
1515import java .util .List ;
1616
1717/**
@@ -24,10 +24,18 @@ public class Response {
2424 private final OutputStream BODY ;
2525 private final Headers HEADER ;
2626
27- private String contentType = MediaType ._txt .getMIME ();
28- private boolean isClose = false ;
29- private long contentLength = 0 ;
30- private int status = 200 ;
27+ private String contentType ;
28+ private boolean isClose ;
29+ private long contentLength ;
30+ private int status ;
31+
32+ {
33+ // Initialize with default data
34+ contentType = MediaType ._txt .getMIME ();
35+ isClose = false ;
36+ contentLength = 0 ;
37+ status = 200 ;
38+ }
3139
3240 public Response (HttpExchange exchange ) {
3341 this .HTTP_EXCHANGE = exchange ;
@@ -121,17 +129,17 @@ public String getContentType() {
121129 *
122130 * @param contentType - The contentType
123131 */
124- public void setContentType (MediaType contentType ) {
125- this .contentType = contentType . getMIME () ;
132+ public void setContentType (String contentType ) {
133+ this .contentType = contentType ;
126134 }
127135
128136 /**
129137 * Set the contentType for this response.
130138 *
131139 * @param contentType - The contentType
132140 */
133- public void setContentType (String contentType ) {
134- this .contentType = contentType ;
141+ public void setContentType (MediaType contentType ) {
142+ this .contentType = contentType . getMIME () ;
135143 }
136144
137145 /**
@@ -151,7 +159,9 @@ public void send() {
151159 */
152160 public void send (String s ) {
153161 if (checkIfClosed ()) return ;
154- this .contentLength += s .length ();
162+ byte [] data = s .getBytes ();
163+
164+ this .contentLength = data .length ;
155165 sendHeaders ();
156166
157167 try {
@@ -172,15 +182,26 @@ public void send(String s) {
172182 */
173183 public void send (@ NotNull File file ) {
174184 if (checkIfClosed ()) return ;
175- this .contentLength += file .length ();
185+ try {
186+ this .contentLength = file .length ();
176187
177- MediaType mediaType = Utils .getContentType (file );
178- this .contentType = mediaType == null ? null : mediaType .getMIME ();
179- sendHeaders ();
188+ // Detect content type
189+ MediaType mediaType = Utils .getContentType (file );
190+ this .contentType = mediaType == null ? null : mediaType .getMIME ();
191+
192+ // Send header
193+ sendHeaders ();
194+
195+ // Send file
196+ FileInputStream fis = new FileInputStream (file );
197+ byte [] buffer = new byte [1024 ];
198+ int n ;
199+ while ((n = fis .read (buffer )) != -1 ) {
200+ this .BODY .write (buffer , 0 , n );
201+ }
202+
203+ fis .close ();
180204
181- try {
182- byte [] bytes = Files .readAllBytes (file .toPath ());
183- this .BODY .write (bytes );
184205 } catch (IOException e ) {
185206 // TODO: Handle error
186207 e .printStackTrace ();
0 commit comments