io.github.nichetoolkit.rest.helper.IoStreamHelper Maven / Gradle / Ivy
Show all versions of rest-toolkit-utils Show documentation
package io.github.nichetoolkit.rest.helper;
import io.github.nichetoolkit.rest.error.often.IoStreamReadException;
import io.github.nichetoolkit.rest.error.often.IoStreamTransferException;
import io.github.nichetoolkit.rest.error.often.IoStreamWriteException;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* IoStreamHelper
* The io stream helper class.
* @author Cyan ([email protected])
* @since Jdk1.8
*/
public class IoStreamHelper {
/**
* transfer
* The transfer method.
* @param multipartFile {@link org.springframework.web.multipart.MultipartFile} The multipart file parameter is MultipartFile
type.
* @param transferFilePath {@link java.lang.String} The transfer file path parameter is String
type.
* @throws IoStreamTransferException {@link io.github.nichetoolkit.rest.error.often.IoStreamTransferException} The io stream transfer exception is IoStreamTransferException
type.
* @see org.springframework.web.multipart.MultipartFile
* @see java.lang.String
* @see io.github.nichetoolkit.rest.error.often.IoStreamTransferException
*/
public static void transfer(MultipartFile multipartFile, String transferFilePath) throws IoStreamTransferException {
File transferFile = new File(transferFilePath);
transfer(multipartFile, transferFile);
}
/**
* transfer
* The transfer method.
* @param multipartFile {@link org.springframework.web.multipart.MultipartFile} The multipart file parameter is MultipartFile
type.
* @param transferFile {@link java.io.File} The transfer file parameter is File
type.
* @throws IoStreamTransferException {@link io.github.nichetoolkit.rest.error.often.IoStreamTransferException} The io stream transfer exception is IoStreamTransferException
type.
* @see org.springframework.web.multipart.MultipartFile
* @see java.io.File
* @see io.github.nichetoolkit.rest.error.often.IoStreamTransferException
*/
public static void transfer(MultipartFile multipartFile, File transferFile) throws IoStreamTransferException {
try {
multipartFile.transferTo(transferFile);
} catch (IOException exception) {
throw new IoStreamTransferException(exception.getMessage());
}
}
/**
* transfer
* The transfer method.
* @param inputStream {@link java.io.InputStream} The input stream parameter is InputStream
type.
* @param outputStream {@link java.io.OutputStream} The output stream parameter is OutputStream
type.
* @param isClose boolean The is close parameter is boolean
type.
* @throws IoStreamTransferException {@link io.github.nichetoolkit.rest.error.often.IoStreamTransferException} The io stream transfer exception is IoStreamTransferException
type.
* @see java.io.InputStream
* @see java.io.OutputStream
* @see io.github.nichetoolkit.rest.error.often.IoStreamTransferException
*/
public static void transfer(InputStream inputStream, OutputStream outputStream, boolean isClose) throws IoStreamTransferException {
try {
int length;
byte[] buffer = new byte[1024];
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
outputStream.flush();
}
} catch (IOException exception) {
throw new IoStreamTransferException(exception.getMessage());
} finally {
if (isClose) {
CloseableHelper.close(inputStream, outputStream);
}
}
}
/**
* read
* The read method.
* @param inputStream {@link java.io.InputStream} The input stream parameter is InputStream
type.
* @return {@link java.lang.String} The read return object is String
type.
* @throws IoStreamReadException {@link io.github.nichetoolkit.rest.error.often.IoStreamReadException} The io stream read exception is IoStreamReadException
type.
* @see java.io.InputStream
* @see java.lang.String
* @see io.github.nichetoolkit.rest.error.often.IoStreamReadException
*/
public static String read(InputStream inputStream) throws IoStreamReadException {
try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
StringBuilder stringBuilder = new StringBuilder();
String tempString;
while ((tempString = bufferedReader.readLine()) != null) {
stringBuilder.append(tempString);
}
return stringBuilder.toString();
} catch (IOException exception) {
throw new IoStreamReadException(exception.getMessage());
}
}
/**
* bytes
* The bytes method.
* @param inputStream {@link java.io.InputStream} The input stream parameter is InputStream
type.
* @return byte The bytes return object is byte
type.
* @throws IoStreamReadException {@link io.github.nichetoolkit.rest.error.often.IoStreamReadException} The io stream read exception is IoStreamReadException
type.
* @see java.io.InputStream
* @see io.github.nichetoolkit.rest.error.often.IoStreamReadException
*/
public static byte[] bytes(InputStream inputStream) throws IoStreamReadException {
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
int length;
byte[] buffer = new byte[1024];
while ((length = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, length);
}
return byteArrayOutputStream.toByteArray();
} catch (IOException exception) {
throw new IoStreamReadException(exception.getMessage());
}
}
/**
* write
* The write method.
* @param outputStream {@link java.io.OutputStream} The output stream parameter is OutputStream
type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @throws IoStreamWriteException {@link io.github.nichetoolkit.rest.error.often.IoStreamWriteException} The io stream write exception is IoStreamWriteException
type.
* @see java.io.OutputStream
* @see java.lang.String
* @see io.github.nichetoolkit.rest.error.often.IoStreamWriteException
*/
public static void write(OutputStream outputStream, String json) throws IoStreamWriteException {
try {
outputStream.write(json.getBytes(StandardCharsets.UTF_8));
outputStream.flush();
} catch (IOException exception) {
throw new IoStreamWriteException(exception.getMessage());
} finally {
CloseableHelper.close(outputStream);
}
}
/**
* write
* The write method.
* @param response {@link javax.servlet.http.HttpServletResponse} The response parameter is HttpServletResponse
type.
* @param file {@link java.io.File} The file parameter is File
type.
* @throws IoStreamWriteException {@link io.github.nichetoolkit.rest.error.often.IoStreamWriteException} The io stream write exception is IoStreamWriteException
type.
* @see javax.servlet.http.HttpServletResponse
* @see java.io.File
* @see io.github.nichetoolkit.rest.error.often.IoStreamWriteException
*/
public static void write(HttpServletResponse response, File file) throws IoStreamWriteException {
try (FileInputStream inputStream = new FileInputStream(file);
ServletOutputStream outputStream = response.getOutputStream()) {
write(outputStream, inputStream);
} catch (IOException exception) {
throw new IoStreamWriteException(exception.getMessage());
}
}
/**
* write
* The write method.
* @param response {@link javax.servlet.http.HttpServletResponse} The response parameter is HttpServletResponse
type.
* @param json {@link java.lang.String} The json parameter is String
type.
* @throws IoStreamWriteException {@link io.github.nichetoolkit.rest.error.often.IoStreamWriteException} The io stream write exception is IoStreamWriteException
type.
* @see javax.servlet.http.HttpServletResponse
* @see java.lang.String
* @see io.github.nichetoolkit.rest.error.often.IoStreamWriteException
*/
public static void write(HttpServletResponse response, String json) throws IoStreamWriteException {
try (OutputStream outputStream = response.getOutputStream()) {
response.setHeader("Content-type", "text/html;charset=UTF-8");
outputStream.write(json.getBytes(StandardCharsets.UTF_8));
outputStream.flush();
} catch (IOException exception) {
throw new IoStreamWriteException(exception.getMessage());
}
}
/**
* write
* The write method.
* @param response {@link javax.servlet.http.HttpServletResponse} The response parameter is HttpServletResponse
type.
* @param data byte The data parameter is byte
type.
* @throws IoStreamWriteException {@link io.github.nichetoolkit.rest.error.often.IoStreamWriteException} The io stream write exception is IoStreamWriteException
type.
* @see javax.servlet.http.HttpServletResponse
* @see io.github.nichetoolkit.rest.error.often.IoStreamWriteException
*/
public static void write(HttpServletResponse response, byte[] data) throws IoStreamWriteException {
try (OutputStream outputStream = response.getOutputStream()){
outputStream.write(data);
outputStream.flush();
} catch (IOException exception) {
throw new IoStreamWriteException(exception.getMessage());
}
}
/**
* write
* The write method.
* @param outputStream {@link java.io.OutputStream} The output stream parameter is OutputStream
type.
* @param data byte The data parameter is byte
type.
* @throws IoStreamWriteException {@link io.github.nichetoolkit.rest.error.often.IoStreamWriteException} The io stream write exception is IoStreamWriteException
type.
* @see java.io.OutputStream
* @see io.github.nichetoolkit.rest.error.often.IoStreamWriteException
*/
public static void write(OutputStream outputStream, byte[] data) throws IoStreamWriteException {
InputStream inputStream = new ByteArrayInputStream(data);
write(outputStream, inputStream);
}
/**
* write
* The write method.
* @param outputStream {@link java.io.OutputStream} The output stream parameter is OutputStream
type.
* @param inputStream {@link java.io.InputStream} The input stream parameter is InputStream
type.
* @throws IoStreamWriteException {@link io.github.nichetoolkit.rest.error.often.IoStreamWriteException} The io stream write exception is IoStreamWriteException
type.
* @see java.io.OutputStream
* @see java.io.InputStream
* @see io.github.nichetoolkit.rest.error.often.IoStreamWriteException
*/
public static void write(OutputStream outputStream, InputStream inputStream) throws IoStreamWriteException {
try {
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
outputStream.flush();
} catch (IOException exception) {
throw new IoStreamWriteException(exception.getMessage());
} finally {
CloseableHelper.close(outputStream, inputStream);
}
}
/**
* write
* The write method.
* @param file {@link java.io.File} The file parameter is File
type.
* @param inputStream {@link java.io.InputStream} The input stream parameter is InputStream
type.
* @throws IoStreamWriteException {@link io.github.nichetoolkit.rest.error.often.IoStreamWriteException} The io stream write exception is IoStreamWriteException
type.
* @see java.io.File
* @see java.io.InputStream
* @see io.github.nichetoolkit.rest.error.often.IoStreamWriteException
*/
public static void write(File file, InputStream inputStream) throws IoStreamWriteException {
try (OutputStream outputStream = Files.newOutputStream(file.toPath())) {
write(outputStream, inputStream);
} catch (IOException exception) {
throw new IoStreamWriteException(exception.getMessage());
}
}
/**
* write
* The write method.
* @param filename {@link java.lang.String} The filename parameter is String
type.
* @param inputStream {@link java.io.InputStream} The input stream parameter is InputStream
type.
* @throws IoStreamWriteException {@link io.github.nichetoolkit.rest.error.often.IoStreamWriteException} The io stream write exception is IoStreamWriteException
type.
* @see java.lang.String
* @see java.io.InputStream
* @see io.github.nichetoolkit.rest.error.often.IoStreamWriteException
*/
public static void write(final String filename,InputStream inputStream) throws IoStreamWriteException {
try (OutputStream outputStream = Files.newOutputStream(Paths.get(filename))) {
write(outputStream, inputStream);
} catch (IOException exception) {
throw new IoStreamWriteException(exception.getMessage());
}
}
/**
* write
* The write method.
* @param file {@link java.io.File} The file parameter is File
type.
* @param data byte The data parameter is byte
type.
* @throws IoStreamWriteException {@link io.github.nichetoolkit.rest.error.often.IoStreamWriteException} The io stream write exception is IoStreamWriteException
type.
* @see java.io.File
* @see io.github.nichetoolkit.rest.error.often.IoStreamWriteException
*/
public static void write(File file, byte[] data) throws IoStreamWriteException {
try ( OutputStream outputStream = Files.newOutputStream(file.toPath())){
write(outputStream, data);
} catch (IOException exception) {
throw new IoStreamWriteException(exception.getMessage());
}
}
/**
* write
* The write method.
* @param filename {@link java.lang.String} The filename parameter is String
type.
* @param data byte The data parameter is byte
type.
* @throws IoStreamWriteException {@link io.github.nichetoolkit.rest.error.often.IoStreamWriteException} The io stream write exception is IoStreamWriteException
type.
* @see java.lang.String
* @see io.github.nichetoolkit.rest.error.often.IoStreamWriteException
*/
public static void write(final String filename, byte[] data) throws IoStreamWriteException {
try (OutputStream outputStream = Files.newOutputStream(Paths.get(filename))) {
write(outputStream, data);
} catch (IOException exception) {
throw new IoStreamWriteException(exception.getMessage());
}
}
}