Skip to content

Commit

Permalink
新增 Assert、Stream 模块
Browse files Browse the repository at this point in the history
  • Loading branch information
xuxueli committed Oct 27, 2024
1 parent e427da7 commit 8f062c2
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 5 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(输入/输出)操作的工具类及方法。
... | ...


Expand Down
8 changes: 5 additions & 3 deletions doc/XXL-TOOL官方文档.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Excel模块 | 一个灵活的Java对象和Excel文档相互转换的工具。一
Emoji模块 | 一个灵活可扩展的Emoji表情编解码库,可快速实现Emoji表情的编解码
Fiber模块 | Java协程库,基于quasar封装实现
Freemarker模块 | 模板引擎工具,支持根据模板文件生成文本、生成文件…等。
IO模块 | 一系列处理IO(输入/输出)操作的工具类及方法。
... | ...

### 1.4 下载
Expand Down Expand Up @@ -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模块
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/xxl/tool/core/AssertTool.java
Original file line number Diff line number Diff line change
@@ -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}.
* <pre class="code">Assert.isTrue(i &gt; 0, "The value must be greater than zero");</pre>
* @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}.
*
* <pre>
* Assert.isNull(value, "The value must be null");
* </pre>
*
* @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}.
*
* <pre>
* Assert.notNull(clazz, "The class must not be null");
* </pre>
*
* @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);
}
}


}
96 changes: 96 additions & 0 deletions src/main/java/com/xxl/tool/io/StreamTool.java
Original file line number Diff line number Diff line change
@@ -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
}
}


}
21 changes: 21 additions & 0 deletions src/test/java/com/xxl/tool/test/core/AssertToolTest.java
Original file line number Diff line number Diff line change
@@ -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");
}

}
28 changes: 28 additions & 0 deletions src/test/java/com/xxl/tool/test/io/StreamToolTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

}

0 comments on commit 8f062c2

Please sign in to comment.