diff --git a/README.md b/README.md index 088b9d3..bf2659b 100644 --- a/README.md +++ b/README.md @@ -40,13 +40,15 @@ XXL-TOOL 是一个Java工具类库,致力于让Java开发更高效。包含 模块 | 说明 --- | --- Core模块 | 包含集合、缓存、日期……等基础组件工具 -Excel模块 | 一个灵活的Java对象和Excel文档相互转换的工具。一行代码完成Java对象和Excel之间的转换 Gson模块 | json序列化、反序列化工具封装,基于Gson -Json模块 | json序列化、反序列化自研工具 +Json模块 | json序列化、反序列化自研工具 Response模块 | 统一响应数据结构体,标准化数据结构、状态码等,降低协作成本 Pipeline模块 | 高扩展性流程编排引擎 +Excel模块 | 一个灵活的Java对象和Excel文档相互转换的工具。一行代码完成Java对象和Excel之间的转换 Emoji模块 | 一个灵活可扩展的Emoji表情编解码库,可快速实现Emoji表情的编解码 Fiber模块 | Java协程库,基于quasar封装实现 +Freemarker模块 | 模板引擎工具,支持根据模板文件生成文本、生成文件…等。 +IO模块 | 一系列处理IO(输入/输出)操作的工具类及方法。 ... | ... diff --git "a/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" "b/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" index 5eb54e8..eb9d652 100644 --- "a/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" +++ "b/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" @@ -28,6 +28,7 @@ Excel模块 | 一个灵活的Java对象和Excel文档相互转换的工具。一 Emoji模块 | 一个灵活可扩展的Emoji表情编解码库,可快速实现Emoji表情的编解码 Fiber模块 | Java协程库,基于quasar封装实现 Freemarker模块 | 模板引擎工具,支持根据模板文件生成文本、生成文件…等。 +IO模块 | 一系列处理IO(输入/输出)操作的工具类及方法。 ... | ... ### 1.4 下载 @@ -417,9 +418,10 @@ logger.info(text); - 4、新增Freemarker模块,模板引擎工具,支持根据模板文件生成文本、生成文件…等。 ### 3.6 v1.3.1 Release Notes[迭代中] -- 1、现有工具完善,包括:StringTool、GsonTool等; -- 2、新增工具,包括:FreemarkerTool、CookieTool、PageModel、CacheTool等; -- 3、完善单测; +- 1、现有工具完善,包括:StringTool、GsonTool 等; +- 2、新增多个工具类模块,包括:FreemarkerTool、CookieTool、PageModel、CacheTool、StreamTool 等; +- 3、工具类单测完善; + ### TODO LIST - excel模块 diff --git a/src/main/java/com/xxl/tool/core/AssertTool.java b/src/main/java/com/xxl/tool/core/AssertTool.java new file mode 100644 index 0000000..e878ce2 --- /dev/null +++ b/src/main/java/com/xxl/tool/core/AssertTool.java @@ -0,0 +1,56 @@ +package com.xxl.tool.core; + +public class AssertTool { + + + /** + * Assert a boolean expression, throwing an {@code IllegalArgumentException} + * if the expression evaluates to {@code false}. + *
Assert.isTrue(i > 0, "The value must be greater than zero");+ * @param expression a boolean expression + * @param message the exception message to use if the assertion fails + * @throws IllegalArgumentException if {@code expression} is {@code false} + */ + public static void isTrue(boolean expression, String message) { + if (!expression) { + throw new IllegalArgumentException(message); + } + } + + /** + * Assert that an object is {@code null}. + * + *
+ * Assert.isNull(value, "The value must be null"); + *+ * + * @param object the object to check + * @param message the exception message to use if the assertion fails + * @throws IllegalArgumentException if the object is not {@code null} + */ + public static void isNull(Object object, String message) { + if (object != null) { + throw new IllegalArgumentException(message); + } + } + + + /** + * Assert that an object is not {@code null}. + * + *
+ * Assert.notNull(clazz, "The class must not be null"); + *+ * + * @param object the object to check + * @param message the exception message to use if the assertion fails + * @throws IllegalArgumentException if the object is {@code null} + */ + public static void notNull(Object object, String message) { + if (object == null) { + throw new IllegalArgumentException(message); + } + } + + +} diff --git a/src/main/java/com/xxl/tool/io/StreamTool.java b/src/main/java/com/xxl/tool/io/StreamTool.java new file mode 100644 index 0000000..e1105fd --- /dev/null +++ b/src/main/java/com/xxl/tool/io/StreamTool.java @@ -0,0 +1,96 @@ +package com.xxl.tool.io; + +import com.xxl.tool.core.AssertTool; + +import java.io.*; + +public class StreamTool { + + /** + * The default buffer size used when copying bytes. + */ + public static final int BUFFER_SIZE = 1024 * 4; + + + // ---------------------- copy ---------------------- + + /** + * Copy from InputStream to OutputStream, Closes both streams when done. + * + * @param in InputStream + * @param out OutputStream + * @return the number of bytes copied + * @throws IOException in case of I/O errors + */ + public static int copy(InputStream in, OutputStream out) throws IOException { + AssertTool.notNull(in, "No InputStream specified"); + AssertTool.notNull(out, "No OutputStream specified"); + + try { + int byteCount = 0; + byte[] buffer = new byte[BUFFER_SIZE]; + int bytesRead; + while ((bytesRead = in.read(buffer)) != -1) { + out.write(buffer, 0, bytesRead); + byteCount += bytesRead; + } + out.flush(); + return byteCount; + } finally { + close(in); + close(out); + } + } + + /** + * Copy from byte array to OutputStream, Closes the stream when done. + * + * @param in the byte array + * @param out OutputStream + * @throws IOException in case of I/O errors + */ + public static void copy(byte[] in, OutputStream out) throws IOException { + AssertTool.notNull(in, "No input byte array specified"); + AssertTool.notNull(out, "No OutputStream specified"); + + try { + out.write(in); + } + finally { + close(out); + } + } + + /** + * Copy the contents of the given InputStream into a new byte array. + * Closes the stream when done. + * @param in the stream to copy from (may be {@code null} or empty) + * @return the new byte array that has been copied to (possibly empty) + * @throws IOException in case of I/O errors + */ + public static byte[] copyToByteArray(InputStream in) throws IOException { + if (in == null) { + return new byte[0]; + } + + ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE); + copy(in, out); + return out.toByteArray(); + } + + /** + * Attempt to close the supplied {@link Closeable}, ignore exceptions + * + * @param closeable the {@code Closeable} to close + */ + private static void close(Closeable closeable) { + try { + closeable.close(); + } + catch (IOException ex) { + // ignore + } + } + + +} diff --git a/src/test/java/com/xxl/tool/test/core/AssertToolTest.java b/src/test/java/com/xxl/tool/test/core/AssertToolTest.java new file mode 100644 index 0000000..d79e6ef --- /dev/null +++ b/src/test/java/com/xxl/tool/test/core/AssertToolTest.java @@ -0,0 +1,21 @@ +package com.xxl.tool.test.core; + +import com.xxl.tool.core.AssertTool; +import com.xxl.tool.core.StringTool; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AssertToolTest { + + @Test + public void isTrueTest() { + AssertTool.isTrue(true, "not true"); + } + + @Test + public void isNullTest() { + AssertTool.notNull(new Object(), "not null"); + } + +} diff --git a/src/test/java/com/xxl/tool/test/io/StreamToolTest.java b/src/test/java/com/xxl/tool/test/io/StreamToolTest.java new file mode 100644 index 0000000..a7a35fe --- /dev/null +++ b/src/test/java/com/xxl/tool/test/io/StreamToolTest.java @@ -0,0 +1,28 @@ +package com.xxl.tool.test.io; + +import com.xxl.tool.io.StreamTool; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringBufferInputStream; + +public class StreamToolTest { + private static final Logger logger = LoggerFactory.getLogger(StreamToolTest.class); + + @Test + public void testJson() { + try { + InputStream inputStream = new ByteArrayInputStream("input".getBytes()); + + byte[] result = StreamTool.copyToByteArray(inputStream); + logger.info(new String(result)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + +}