* 1. \r\n * 2. \n ** - * @param buffer {@link ByteBuffer} + * @param buffer {@link ByteBuffer} * @param maxLength 读取最大长度 * @return 末尾位置,未找到或达到最大长度返回-1 */ @@ -191,13 +191,13 @@ public class BufferUtil { /** * 读取一行,如果buffer中最后一部分并非完整一行,则返回null
* 1. \r\n * 2. \n *- * - * @param buffer ByteBuffer + * + * @param buffer ByteBuffer * @param charset 编码 * @return 一行 */ @@ -217,7 +217,7 @@ public class BufferUtil { /** * 创建新Buffer - * + * * @param data 数据 * @return {@link ByteBuffer} * @since 4.5.0 @@ -228,8 +228,8 @@ public class BufferUtil { /** * 从字符串创建新Buffer - * - * @param data 数据 + * + * @param data 数据 * @param charset 编码 * @return {@link ByteBuffer} * @since 4.5.0 @@ -237,10 +237,10 @@ public class BufferUtil { public static ByteBuffer create(CharSequence data, Charset charset) { return create(StrUtil.bytes(data, charset)); } - + /** * 从字符串创建新Buffer,使用UTF-8编码 - * + * * @param data 数据 * @return {@link ByteBuffer} * @since 4.5.0 @@ -248,4 +248,15 @@ public class BufferUtil { public static ByteBuffer createUtf8(CharSequence data) { return create(StrUtil.utf8Bytes(data)); } + + /** + * 创建{@link CharBuffer} + * + * @param capacity 容量 + * @return {@link CharBuffer} + * @since 5.5.7 + */ + public static CharBuffer createCharBuffer(int capacity) { + return CharBuffer.allocate(capacity); + } } diff --git a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java index d0b59ab37..c652deade 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java @@ -638,12 +638,10 @@ public class FileUtil extends PathUtil { * @return 父目录 */ public static File mkParentDirs(File file) { - final File parentFile = file.getParentFile(); - if (null != parentFile && false == parentFile.exists()) { - //noinspection ResultOfMethodCallIgnored - parentFile.mkdirs(); + if(null == file){ + return null; } - return parentFile; + return mkdir(file.getParentFile()); } /** @@ -1022,6 +1020,7 @@ public class FileUtil extends PathUtil { * @param isOverride 是否覆盖目标文件 * @return 目标文件 * @since 3.0.9 + * @see PathUtil#rename(Path, String, boolean) */ public static File rename(File file, String newName, boolean isRetainExt, boolean isOverride) { if (isRetainExt) { @@ -2914,7 +2913,21 @@ public class FileUtil extends PathUtil { * @throws IORuntimeException IO异常 */ public static File writeFromStream(InputStream in, File dest) throws IORuntimeException { - return FileWriter.create(dest).writeFromStream(in); + return writeFromStream(in, dest, true); + } + + /** + * 将流的内容写入文件 + * + * @param dest 目标文件 + * @param in 输入流 + * @param isCloseIn 是否关闭输入流 + * @return dest + * @throws IORuntimeException IO异常 + * @since 5.5.6 + */ + public static File writeFromStream(InputStream in, File dest, boolean isCloseIn) throws IORuntimeException { + return FileWriter.create(dest).writeFromStream(in, isCloseIn); } /** @@ -3169,7 +3182,22 @@ public class FileUtil extends PathUtil { * @since 4.1.15 */ public static String getMimeType(String filePath) { - return URLConnection.getFileNameMap().getContentTypeFor(filePath); + String contentType = URLConnection.getFileNameMap().getContentTypeFor(filePath); + if(null == contentType){ + // 补充一些常用的mimeType + if(filePath.endsWith(".css")){ + contentType = "text/css"; + } else if(filePath.endsWith(".js")){ + contentType = "application/x-javascript"; + } + } + + // 补充 + if(null == contentType){ + contentType = getMimeType(Paths.get(filePath)); + } + + return contentType; } /** diff --git a/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java index 1c877e6ef..e8588cc60 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java @@ -153,7 +153,8 @@ public class PathUtil { } /** - * 通过JDK7+的 {@link Files#copy(Path, Path, CopyOption...)} 方法拷贝文件 + * 通过JDK7+的 {@link Files#copy(Path, Path, CopyOption...)} 方法拷贝文件
null
+ * 克隆数组,如果非数组返回{@code null}
*
* @param '@'
*/
+ /** 字符常量:艾特 {@code '@'} */
public static final char AT = '@';
/**
diff --git a/hutool-core/src/main/java/cn/hutool/core/util/IdcardUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/IdcardUtil.java
index af9dc87de..e1bef751a 100644
--- a/hutool-core/src/main/java/cn/hutool/core/util/IdcardUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/util/IdcardUtil.java
@@ -147,7 +147,7 @@ public class IdcardUtil {
}
/**
- * 是否有效身份证号
+ * 是否有效身份证号,忽略X的大小写
*
* @param idCard 身份证号,支持18位、15位和港澳台的10位
* @return 是否有效
@@ -198,9 +198,46 @@ public class IdcardUtil {
*
*
* @param idcard 待验证的身份证
- * @return 是否有效的18位身份证
+ * @return 是否有效的18位身份证,忽略x的大小写
*/
public static boolean isValidCard18(String idcard) {
+ return isValidCard18(idcard, true);
+ }
+
+ /**
+ * + * 判断18位身份证的合法性 + *
+ * 根据〖中华人民共和国国家标准GB11643-1999〗中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成。+ * 顺序码: 表示在同一地址码所标识的区域范围内,对同年、同月、同 日出生的人编定的顺序号,顺序码的奇数分配给男性,偶数分配 给女性。 + *
+ *+ * 第十八位数字(校验码)的计算方法为: + *
* 最后发现原来是某些SSL协议没有开启
+ *
* @author MikaGuraNTK
*/
public class AndroidSupportSSLFactory extends CustomProtocolsSSLFactory {
// Android低版本不重置的话某些SSL访问就会失败
private static final String[] protocols = {SSLv3, TLSv1, TLSv11, TLSv12};
-
+
public AndroidSupportSSLFactory() throws KeyManagementException, NoSuchAlgorithmException {
super(protocols);
}
diff --git a/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java b/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java
index aabf78763..30036b80c 100644
--- a/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java
+++ b/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java
@@ -25,7 +25,7 @@ public class HttpRequestTest {
@Test
@Ignore
public void getHttpsTest() {
- String body = HttpRequest.get("https://www.gjifa.com/pc/").execute().body();
+ String body = HttpRequest.get("https://www.hutool.cn/").timeout(10).execute().body();
Console.log(body);
}
diff --git a/hutool-http/src/test/java/cn/hutool/http/HttpUtilTest.java b/hutool-http/src/test/java/cn/hutool/http/HttpUtilTest.java
index 3cc5d25c1..3f3ebd7b6 100644
--- a/hutool-http/src/test/java/cn/hutool/http/HttpUtilTest.java
+++ b/hutool-http/src/test/java/cn/hutool/http/HttpUtilTest.java
@@ -4,9 +4,6 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ReUtil;
-import cn.hutool.http.Header;
-import cn.hutool.http.HttpRequest;
-import cn.hutool.http.HttpUtil;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
diff --git a/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java b/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java
index bca649874..842ecccd0 100644
--- a/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java
+++ b/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java
@@ -11,6 +11,10 @@ public class SimpleServerTest {
public static void main(String[] args) {
HttpUtil.createServer(8888)
+ .addFilter(((req, res, chain) -> {
+ Console.log("Filter: " + req.getPath());
+ chain.doFilter(req.getHttpExchange());
+ }))
// 设置默认根目录,classpath/html
.setRoot(FileUtil.file("html"))
// get数据测试,返回请求的PATH
@@ -45,6 +49,11 @@ public class SimpleServerTest {
response.write(request.getMultipart().getParamMap().toString(), ContentType.TEXT_PLAIN.toString());
}
)
+ // 测试输出响应内容是否能正常返回Content-Length头信息
+ .addAction("test/zeroStr", (req, res)-> {
+ res.write("0");
+ Console.log("Write 0 OK");
+ })
.start();
}
}
diff --git a/hutool-json/pom.xml b/hutool-json/pom.xml
index 58b7cc493..99f484c82 100644
--- a/hutool-json/pom.xml
+++ b/hutool-json/pom.xml
@@ -9,7 +9,7 @@
JSONString
接口定义了一个toJSONString()
- * 实现此接口的类可以通过实现toJSONString()
方法来改变转JSON字符串的方式。
+ * {@code JSONString}接口定义了一个{@code toJSONString()}
+ * 实现此接口的类可以通过实现{@code toJSONString()}方法来改变转JSON字符串的方式。
*
* @author Looly
*
diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONSupport.java b/hutool-json/src/main/java/cn/hutool/json/JSONSupport.java
index b9a026b47..44e7155c1 100644
--- a/hutool-json/src/main/java/cn/hutool/json/JSONSupport.java
+++ b/hutool-json/src/main/java/cn/hutool/json/JSONSupport.java
@@ -35,7 +35,7 @@ public class JSONSupport implements JSONString{
* @return 美化的JSON
*/
public String toPrettyString() {
- return toJSON().toJSONString(4);
+ return toJSON().toStringPretty();
}
@Override
diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONUtil.java b/hutool-json/src/main/java/cn/hutool/json/JSONUtil.java
index 3afa65cc2..1715f9a4e 100644
--- a/hutool-json/src/main/java/cn/hutool/json/JSONUtil.java
+++ b/hutool-json/src/main/java/cn/hutool/json/JSONUtil.java
@@ -218,7 +218,7 @@ public final class JSONUtil {
json = (JSON) obj;
} else if (obj instanceof CharSequence) {
final String jsonStr = StrUtil.trim((CharSequence) obj);
- json = StrUtil.startWith(jsonStr, '[') ? parseArray(jsonStr) : parseObj(jsonStr);
+ json = isJsonArray(jsonStr) ? parseArray(jsonStr, config) : parseObj(jsonStr, config);
} else if (obj instanceof Iterable || obj instanceof Iterator || ArrayUtil.isArray(obj)) {// 列表
json = new JSONArray(obj, config);
} else {// 对象
diff --git a/hutool-json/src/test/java/cn/hutool/json/JSONArrayTest.java b/hutool-json/src/test/java/cn/hutool/json/JSONArrayTest.java
index 5b42385e9..d4954a092 100644
--- a/hutool-json/src/test/java/cn/hutool/json/JSONArrayTest.java
+++ b/hutool-json/src/test/java/cn/hutool/json/JSONArrayTest.java
@@ -11,6 +11,7 @@ import cn.hutool.core.util.CharsetUtil;
import cn.hutool.json.test.bean.Exam;
import cn.hutool.json.test.bean.JsonNode;
import cn.hutool.json.test.bean.KeyBean;
+import lombok.Data;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@@ -202,6 +203,14 @@ public class JSONArrayTest {
Assert.assertEquals("-0", nodeList.get(3).getName());
}
+ @Test
+ public void getByPathTest(){
+ String jsonStr = "[{\"id\": \"1\",\"name\": \"a\"},{\"id\": \"2\",\"name\": \"b\"}]";
+ final JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
+ Assert.assertEquals("b", jsonArray.getByPath("[1].name"));
+ Assert.assertEquals("b", JSONUtil.getByPath(jsonArray, "[1].name"));
+ }
+
private static Map
null
+ * 获取指定坐标单元格,单元格不存在时返回{@code null}
*
* @param locationRef 单元格地址标识符,例如A11,B5
* @return {@link Cell}
@@ -157,7 +157,7 @@ public class ExcelBasenull
+ * 获取指定坐标单元格,单元格不存在时返回{@code null}
*
* @param x X坐标,从0计数,即列号
* @param y Y坐标,从0计数,即行号
@@ -193,7 +193,7 @@ public class ExcelBasenull
+ * 获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回{@code null}
*
* @param locationRef 单元格地址标识符,例如A11,B5
* @param isCreateIfNotExist 单元格不存在时是否创建
@@ -206,7 +206,7 @@ public class ExcelBasenull
+ * 获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回{@code null}
*
* @param x X坐标,从0计数,即列号
* @param y Y坐标,从0计数,即行号
diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java
index 46dda3f2c..6585b78ab 100644
--- a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java
+++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java
@@ -233,7 +233,7 @@ public class ExcelWriter extends ExcelBase- * <sheet name="Sheet6" sheetId="4" r:id="6"/> + * <sheet name="Sheet6" sheetId="4" r:id="rId6"/> **
* 读取结果为:
@@ -36,8 +37,8 @@ public class SheetRidReader extends DefaultHandler {
private final static String SHEET_ID_ATTR = "sheetId";
private final static String NAME_ATTR = "name";
- private final Map