Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e8dc4a8

Browse files
committed
add sendBytes and streamFrom methods to Response
1 parent 90b3164 commit e8dc4a8

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

‎README.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,17 @@ res.setContentType(String type); // Set the content type
304304
res.isClosed(); // Check if the response is already closed
305305
res.getHeader(String key); // Get the value from an header field via key
306306
res.setHeader(String key, String val); // Add an specific response header
307-
res.sendAttachment(Path file) // Sends an file as attachment
308-
res.send(String str); // Send an string as response
309-
res.send(Path path); // Send an file as response
307+
res.sendAttachment(Path file) // Sends a file as attachment
308+
res.send(String str); // Send a string as response
309+
res.send(Path path); // Send a file as response
310+
res.send(byte[] bytes) // Send bytes as response
310311
res.send(); // Send empty response
311312
res.redirect(String location); // Redirect the request to another url
312313
res.setCookie(Cookie cookie); // Add an cookie to the response
313314
res.sendStatus(Status status); // Set the response status and send an empty response
314315
res.getStatus(); // Returns the current status
315316
res.setStatus(Status status); // Set the repose status
317+
res.streamFrom(long contentLength, InputStream is, MediaType mediaType) // Send a inputstream with known length and type
316318
```
317319
The response object calls are comments because **you can only call the .send(xy) once each request!**
318320

‎src/main/java/express/DynExpress.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
@Target(ElementType.METHOD)
1616
public @interface DynExpress {
1717
RequestMethod method() default RequestMethod.GET;
18+
1819
String context() default "/";
1920
}

‎src/main/java/express/http/response/Response.java‎

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,87 @@ public boolean send(Path file) {
238238

239239
} catch (IOException e) {
240240
logger.log(Level.INFO, "Failed to pipe file to outputstream.", e);
241+
return false;
242+
} finally {
241243
close();
244+
}
245+
246+
return true;
247+
}
248+
249+
/**
250+
* Send a byte array as response. Content type will be
251+
* set to application/octet-streamFrom
252+
*
253+
* @param bytes Byte arraa
254+
* @return If operation was successful
255+
*/
256+
public boolean sendBytes(byte[] bytes) {
257+
258+
if (isClosed() || bytes == null) {
242259
return false;
243260
}
244261

245-
close();
262+
try {
263+
this.contentLength = bytes.length;
264+
265+
// Set content type to octet streamFrom
266+
this.contentType = MediaType._bin.getMIME();
267+
268+
// Send header
269+
sendHeaders();
270+
271+
// Write bytes to body
272+
this.body.write(bytes);
273+
} catch (IOException e) {
274+
logger.log(Level.INFO, "Failed to pipe file to outputstream.", e);
275+
return false;
276+
} finally {
277+
close();
278+
}
279+
280+
return true;
281+
}
282+
283+
/**
284+
* Streams a inputstream to the client.
285+
* Requires a contentLength as well as a MediaType
286+
*
287+
* @param contentLength Total size
288+
* @param is Inputstream
289+
* @param mediaType Stream type
290+
* @return If operation was successful
291+
*/
292+
public boolean streamFrom(long contentLength, InputStream is, MediaType mediaType) {
293+
294+
if (isClosed() || is == null) {
295+
return false;
296+
}
297+
298+
try {
299+
this.contentLength = contentLength;
300+
301+
// Set content type to octet-stream
302+
this.contentType = mediaType.getMIME();
303+
304+
// Send header
305+
sendHeaders();
306+
307+
// Write bytes to body
308+
byte[] buffer = new byte[4096];
309+
int n;
310+
while ((n = is.read(buffer)) != -1) {
311+
this.body.write(buffer, 0, n);
312+
}
313+
314+
is.close();
315+
} catch (IOException e) {
316+
logger.log(Level.INFO, "Failed to pipe file to outputstream.", e);
317+
return false;
318+
} finally {
319+
close();
320+
}
321+
246322
return true;
247323
}
248324

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /