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

Add method to send files. #102

Merged
merged 1 commit into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SimpleHttpServer
[![Maven Central](https://img.shields.io/maven-central/v/com.kttdevelopment/simplehttpserver)](https://mvnrepository.com/artifact/com.kttdevelopment/simplehttpserver)
[![Deploy](https://github.com/Ktt-Development/simplehttpserver/workflows/Deploy/badge.svg)](https://github.com/orgs/Ktt-Development/packages?repo_name=simplehttpserver)
[![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)
[![Maven Central](https://img.shields.io/maven-central/v/com.kttdevelopment/simplehttpserver)](https://mvnrepository.com/artifact/com.kttdevelopment/simplehttpserver)
[![version](https://img.shields.io/github/v/release/ktt-development/simplehttpserver?include_prereleases)](https://github.com/Ktt-Development/simplehttpserver/releases)
[![license](https://img.shields.io/github/license/Ktt-Development/simplehttpserver)](https://github.com/Ktt-Development/simplehttpserver/blob/main/LICENSE)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import com.kttdevelopment.simplehttpserver.var.RequestMethod;
import com.sun.net.httpserver.*;

import java.io.IOException;
import java.io.OutputStream;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.Map;
Expand Down Expand Up @@ -46,7 +45,7 @@
*
* @see HttpExchange
* @since 02.00.00
* @version 03.04.03
* @version 4.0.0
* @author Ktt Development
*/
@SuppressWarnings("SpellCheckingInspection")
Expand Down Expand Up @@ -386,6 +385,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
* @see #send(String, boolean)
* @see #send(String, int)
* @see #send(String, int, boolean)
* @see #send(File)
* @see #send(File, boolean)
* @see #send(File, int)
* @see #send(File, int, boolean)
* @since 02.00.00
* @author Ktt Development
*/
Expand All @@ -406,6 +409,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
* @see #send(String, boolean)
* @see #send(String, int)
* @see #send(String, int, boolean)
* @see #send(File)
* @see #send(File, boolean)
* @see #send(File, int)
* @see #send(File, int, boolean)
* @since 02.00.00
* @author Ktt Development
*/
Expand All @@ -427,6 +434,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
* @see #send(String, boolean)
* @see #send(String, int)
* @see #send(String, int, boolean)
* @see #send(File)
* @see #send(File, boolean)
* @see #send(File, int)
* @see #send(File, int, boolean)
* @since 02.00.00
* @author Ktt Development
*/
Expand All @@ -448,6 +459,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
* @see #send(String, boolean)
* @see #send(String, int)
* @see #send(String, int, boolean)
* @see #send(File)
* @see #send(File, boolean)
* @see #send(File, int)
* @see #send(File, int, boolean)
* @since 02.00.00
* @author Ktt Development
*/
Expand All @@ -470,6 +485,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
* @see #send(String, boolean)
* @see #send(String, int)
* @see #send(String, int, boolean)
* @see #send(File)
* @see #send(File, boolean)
* @see #send(File, int)
* @see #send(File, int, boolean)
* @since 02.00.00
* @author Ktt Development
*/
Expand All @@ -490,6 +509,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
* @see #send(String, boolean)
* @see #send(String, int)
* @see #send(String, int, boolean)
* @see #send(File)
* @see #send(File, boolean)
* @see #send(File, int)
* @see #send(File, int, boolean)
* @since 02.00.00
* @author Ktt Development
*/
Expand All @@ -511,6 +534,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
* @see #send(String)
* @see #send(String, int)
* @see #send(String, int, boolean)
* @see #send(File)
* @see #send(File, boolean)
* @see #send(File, int)
* @see #send(File, int, boolean)
* @since 02.00.00
* @author Ktt Development
*/
Expand All @@ -532,6 +559,10 @@ static SimpleHttpExchange create(final HttpExchange exchange){
* @see #send(String)
* @see #send(String, boolean)
* @see #send(String, int, boolean)
* @see #send(File)
* @see #send(File, boolean)
* @see #send(File, int)
* @see #send(File, int, boolean)
* @since 02.00.00
* @author Ktt Development
*/
Expand All @@ -554,11 +585,115 @@ static SimpleHttpExchange create(final HttpExchange exchange){
* @see #send(String)
* @see #send(String, boolean)
* @see #send(String, int)
* @see #send(File)
* @see #send(File, boolean)
* @see #send(File, int)
* @see #send(File, int, boolean)
* @since 02.00.00
* @author Ktt Development
*/
public abstract void send(final String response, final int responseCode, final boolean gzip) throws IOException;

/**
* Sends a file to the client.
*
* @param file file to send
* @throws IOException internal server error or file read error
*
* @see #sendResponseHeaders(int, long)
* @see #send(int)
* @see #send(byte[])
* @see #send(byte[], boolean)
* @see #send(byte[], int)
* @see #send(byte[], int, boolean)
* @see #send(String)
* @see #send(String, boolean)
* @see #send(String, int)
* @see #send(String, int, boolean)
* @see #send(File, boolean)
* @see #send(File, int)
* @see #send(File, int, boolean)
* @since 4.0.0
* @author Ktt Development
*/
public abstract void send(final File file) throws IOException;

/**
* Sends a file to the client.
*
* @param file file to send
* @param gzip if the response should be gziped before sending it to the client
* @throws IOException internal server error or file read error
*
* @see #sendResponseHeaders(int, long)
* @see #send(int)
* @see #send(byte[])
* @see #send(byte[], boolean)
* @see #send(byte[], int)
* @see #send(byte[], int, boolean)
* @see #send(String)
* @see #send(String, boolean)
* @see #send(String, int)
* @see #send(String, int, boolean)
* @see #send(File)
* @see #send(File, int)
* @see #send(File, int, boolean)
* @since 4.0.0
* @author Ktt Development
*/
public abstract void send(final File file, final boolean gzip) throws IOException;

/**
* Sends a file with response code to the client.
*
* @param file file to send
* @param responseCode response code
* @throws IOException internal server error or file read error
*
* @see #sendResponseHeaders(int, long)
* @see #send(int)
* @see #send(byte[])
* @see #send(byte[], boolean)
* @see #send(byte[], int)
* @see #send(byte[], int, boolean)
* @see #send(String)
* @see #send(String, boolean)
* @see #send(String, int)
* @see #send(String, int, boolean)
* @see #send(File)
* @see #send(File, boolean)
* @see #send(File, int, boolean)
* @since 4.0.0
* @author Ktt Development
*/
public abstract void send(final File file, final int responseCode) throws IOException;

/**
* Sends a file with response code to the client.
*
* @param file file to send
* @param responseCode response code
* @param gzip if the response should be gziped before sending it to the client
* @throws IOException internal server error or file read error
*
* @see #sendResponseHeaders(int, long)
* @see #send(int)
* @see #send(byte[])
* @see #send(byte[], boolean)
* @see #send(byte[], int)
* @see #send(byte[], int, boolean)
* @see #send(String)
* @see #send(String, boolean)
* @see #send(String, int)
* @see #send(String, int, boolean)
* @see #send(File)
* @see #send(File, boolean)
* @see #send(File, int)
* @since 4.0.0
* @author Ktt Development
*/
public abstract void send(final File file, final int responseCode, final boolean gzip) throws IOException;

//

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;
Expand All @@ -17,7 +18,7 @@
*
* @see SimpleHttpExchange
* @since 02.00.00
* @version 03.05.00
* @version 4.0.0
* @author Ktt Development
*/
@SuppressWarnings("SpellCheckingInspection")
Expand Down Expand Up @@ -390,6 +391,26 @@ public final void send(final String response, final int responseCode, final bool
send(response.getBytes(StandardCharsets.UTF_8),responseCode,gzip);
}

@Override
public final void send(final File file) throws IOException{
send(Files.readAllBytes(file.toPath()));
}

@Override
public final void send(final File file, final boolean gzip) throws IOException{
send(Files.readAllBytes(file.toPath()), true);
}

@Override
public final void send(final File file, final int responseCode) throws IOException{
send(Files.readAllBytes(file.toPath()),responseCode);
}

@Override
public final void send(final File file, final int responseCode, final boolean gzip) throws IOException{
send(Files.readAllBytes(file.toPath()),responseCode,gzip);
}

//

@Override
Expand Down
Loading