Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 9f89507

Browse files
authored
send file; add send tests (#102)
1 parent c416245 commit 9f89507

File tree

4 files changed

+332
-5
lines changed

4 files changed

+332
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SimpleHttpServer
2-
[![Maven Central](https://img.shields.io/maven-central/v/com.kttdevelopment/simplehttpserver)](https://mvnrepository.com/artifact/com.kttdevelopment/simplehttpserver)
32
[![Deploy](https://github.com/Ktt-Development/simplehttpserver/workflows/Deploy/badge.svg)](https://github.com/orgs/Ktt-Development/packages?repo_name=simplehttpserver)
43
[![Java CI](https://github.com/Ktt-Development/simplehttpserver/workflows/Java%20CI/badge.svg)](https://github.com/Ktt-Development/simplehttpserver/actions?query=workflow%3A%22Java+CI%22)
4+
[![Maven Central](https://img.shields.io/maven-central/v/com.kttdevelopment/simplehttpserver)](https://mvnrepository.com/artifact/com.kttdevelopment/simplehttpserver)
55
[![version](https://img.shields.io/github/v/release/ktt-development/simplehttpserver?include_prereleases)](https://github.com/Ktt-Development/simplehttpserver/releases)
66
[![license](https://img.shields.io/github/license/Ktt-Development/simplehttpserver)](https://github.com/Ktt-Development/simplehttpserver/blob/main/LICENSE)
77

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java

Lines changed: 138 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import com.kttdevelopment.simplehttpserver.var.RequestMethod;
55
import com.sun.net.httpserver.*;
66

7-
import java.io.IOException;
8-
import java.io.OutputStream;
7+
import java.io.*;
98
import java.net.InetSocketAddress;
109
import java.net.URI;
1110
import java.util.Map;
@@ -46,7 +45,7 @@
4645
*
4746
* @see HttpExchange
4847
* @since 02.00.00
49-
* @version 03.04.03
48+
* @version 4.0.0
5049
* @author Ktt Development
5150
*/
5251
@SuppressWarnings("SpellCheckingInspection")
@@ -386,6 +385,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
386385
* @see #send(String, boolean)
387386
* @see #send(String, int)
388387
* @see #send(String, int, boolean)
388+
* @see #send(File)
389+
* @see #send(File, boolean)
390+
* @see #send(File, int)
391+
* @see #send(File, int, boolean)
389392
* @since 02.00.00
390393
* @author Ktt Development
391394
*/
@@ -406,6 +409,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
406409
* @see #send(String, boolean)
407410
* @see #send(String, int)
408411
* @see #send(String, int, boolean)
412+
* @see #send(File)
413+
* @see #send(File, boolean)
414+
* @see #send(File, int)
415+
* @see #send(File, int, boolean)
409416
* @since 02.00.00
410417
* @author Ktt Development
411418
*/
@@ -427,6 +434,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
427434
* @see #send(String, boolean)
428435
* @see #send(String, int)
429436
* @see #send(String, int, boolean)
437+
* @see #send(File)
438+
* @see #send(File, boolean)
439+
* @see #send(File, int)
440+
* @see #send(File, int, boolean)
430441
* @since 02.00.00
431442
* @author Ktt Development
432443
*/
@@ -448,6 +459,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
448459
* @see #send(String, boolean)
449460
* @see #send(String, int)
450461
* @see #send(String, int, boolean)
462+
* @see #send(File)
463+
* @see #send(File, boolean)
464+
* @see #send(File, int)
465+
* @see #send(File, int, boolean)
451466
* @since 02.00.00
452467
* @author Ktt Development
453468
*/
@@ -470,6 +485,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
470485
* @see #send(String, boolean)
471486
* @see #send(String, int)
472487
* @see #send(String, int, boolean)
488+
* @see #send(File)
489+
* @see #send(File, boolean)
490+
* @see #send(File, int)
491+
* @see #send(File, int, boolean)
473492
* @since 02.00.00
474493
* @author Ktt Development
475494
*/
@@ -490,6 +509,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
490509
* @see #send(String, boolean)
491510
* @see #send(String, int)
492511
* @see #send(String, int, boolean)
512+
* @see #send(File)
513+
* @see #send(File, boolean)
514+
* @see #send(File, int)
515+
* @see #send(File, int, boolean)
493516
* @since 02.00.00
494517
* @author Ktt Development
495518
*/
@@ -511,6 +534,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
511534
* @see #send(String)
512535
* @see #send(String, int)
513536
* @see #send(String, int, boolean)
537+
* @see #send(File)
538+
* @see #send(File, boolean)
539+
* @see #send(File, int)
540+
* @see #send(File, int, boolean)
514541
* @since 02.00.00
515542
* @author Ktt Development
516543
*/
@@ -532,6 +559,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
532559
* @see #send(String)
533560
* @see #send(String, boolean)
534561
* @see #send(String, int, boolean)
562+
* @see #send(File)
563+
* @see #send(File, boolean)
564+
* @see #send(File, int)
565+
* @see #send(File, int, boolean)
535566
* @since 02.00.00
536567
* @author Ktt Development
537568
*/
@@ -554,11 +585,115 @@ static SimpleHttpExchange create(final HttpExchange exchange){
554585
* @see #send(String)
555586
* @see #send(String, boolean)
556587
* @see #send(String, int)
588+
* @see #send(File)
589+
* @see #send(File, boolean)
590+
* @see #send(File, int)
591+
* @see #send(File, int, boolean)
557592
* @since 02.00.00
558593
* @author Ktt Development
559594
*/
560595
public abstract void send(final String response, final int responseCode, final boolean gzip) throws IOException;
561596

597+
/**
598+
* Sends a file to the client.
599+
*
600+
* @param file file to send
601+
* @throws IOException internal server error or file read error
602+
*
603+
* @see #sendResponseHeaders(int, long)
604+
* @see #send(int)
605+
* @see #send(byte[])
606+
* @see #send(byte[], boolean)
607+
* @see #send(byte[], int)
608+
* @see #send(byte[], int, boolean)
609+
* @see #send(String)
610+
* @see #send(String, boolean)
611+
* @see #send(String, int)
612+
* @see #send(String, int, boolean)
613+
* @see #send(File, boolean)
614+
* @see #send(File, int)
615+
* @see #send(File, int, boolean)
616+
* @since 4.0.0
617+
* @author Ktt Development
618+
*/
619+
public abstract void send(final File file) throws IOException;
620+
621+
/**
622+
* Sends a file to the client.
623+
*
624+
* @param file file to send
625+
* @param gzip if the response should be gziped before sending it to the client
626+
* @throws IOException internal server error or file read error
627+
*
628+
* @see #sendResponseHeaders(int, long)
629+
* @see #send(int)
630+
* @see #send(byte[])
631+
* @see #send(byte[], boolean)
632+
* @see #send(byte[], int)
633+
* @see #send(byte[], int, boolean)
634+
* @see #send(String)
635+
* @see #send(String, boolean)
636+
* @see #send(String, int)
637+
* @see #send(String, int, boolean)
638+
* @see #send(File)
639+
* @see #send(File, int)
640+
* @see #send(File, int, boolean)
641+
* @since 4.0.0
642+
* @author Ktt Development
643+
*/
644+
public abstract void send(final File file, final boolean gzip) throws IOException;
645+
646+
/**
647+
* Sends a file with response code to the client.
648+
*
649+
* @param file file to send
650+
* @param responseCode response code
651+
* @throws IOException internal server error or file read error
652+
*
653+
* @see #sendResponseHeaders(int, long)
654+
* @see #send(int)
655+
* @see #send(byte[])
656+
* @see #send(byte[], boolean)
657+
* @see #send(byte[], int)
658+
* @see #send(byte[], int, boolean)
659+
* @see #send(String)
660+
* @see #send(String, boolean)
661+
* @see #send(String, int)
662+
* @see #send(String, int, boolean)
663+
* @see #send(File)
664+
* @see #send(File, boolean)
665+
* @see #send(File, int, boolean)
666+
* @since 4.0.0
667+
* @author Ktt Development
668+
*/
669+
public abstract void send(final File file, final int responseCode) throws IOException;
670+
671+
/**
672+
* Sends a file with response code to the client.
673+
*
674+
* @param file file to send
675+
* @param responseCode response code
676+
* @param gzip if the response should be gziped before sending it to the client
677+
* @throws IOException internal server error or file read error
678+
*
679+
* @see #sendResponseHeaders(int, long)
680+
* @see #send(int)
681+
* @see #send(byte[])
682+
* @see #send(byte[], boolean)
683+
* @see #send(byte[], int)
684+
* @see #send(byte[], int, boolean)
685+
* @see #send(String)
686+
* @see #send(String, boolean)
687+
* @see #send(String, int)
688+
* @see #send(String, int, boolean)
689+
* @see #send(File)
690+
* @see #send(File, boolean)
691+
* @see #send(File, int)
692+
* @since 4.0.0
693+
* @author Ktt Development
694+
*/
695+
public abstract void send(final File file, final int responseCode, final boolean gzip) throws IOException;
696+
562697
//
563698

564699
/**

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.*;
77
import java.net.*;
88
import java.nio.charset.StandardCharsets;
9+
import java.nio.file.Files;
910
import java.util.*;
1011
import java.util.function.Function;
1112
import java.util.regex.Matcher;
@@ -17,7 +18,7 @@
1718
*
1819
* @see SimpleHttpExchange
1920
* @since 02.00.00
20-
* @version 03.05.00
21+
* @version 4.0.0
2122
* @author Ktt Development
2223
*/
2324
@SuppressWarnings("SpellCheckingInspection")
@@ -390,6 +391,26 @@ public final void send(final String response, final int responseCode, final bool
390391
send(response.getBytes(StandardCharsets.UTF_8),responseCode,gzip);
391392
}
392393

394+
@Override
395+
public final void send(final File file) throws IOException{
396+
send(Files.readAllBytes(file.toPath()));
397+
}
398+
399+
@Override
400+
public final void send(final File file, final boolean gzip) throws IOException{
401+
send(Files.readAllBytes(file.toPath()), true);
402+
}
403+
404+
@Override
405+
public final void send(final File file, final int responseCode) throws IOException{
406+
send(Files.readAllBytes(file.toPath()),responseCode);
407+
}
408+
409+
@Override
410+
public final void send(final File file, final int responseCode, final boolean gzip) throws IOException{
411+
send(Files.readAllBytes(file.toPath()),responseCode,gzip);
412+
}
413+
393414
//
394415

395416
@Override

0 commit comments

Comments
 (0)