From 36e560842d9d1977274d554328b797982cf0a765 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 26 Oct 2022 12:34:05 +0800 Subject: [PATCH] fix code --- .../cn/hutool/core/io/CircularByteBuffer.java | 239 ++++++++++++++++++ .../main/java/cn/hutool/core/io/FileUtil.java | 128 ++-------- .../main/java/cn/hutool/core/io/IoUtil.java | 13 + .../cn/hutool/core/io/file/FileNameUtil.java | 121 ++++++++- .../java/cn/hutool/core/util/SystemUtil.java | 19 ++ .../java/cn/hutool/core/io/FileUtilTest.java | 32 +-- .../hutool/core/io/file/FileNameUtilTest.java | 27 ++ .../hutool/http/client/body/RequestBody.java | 13 + .../hutool/http/client/body/ResourceBody.java | 6 + ...Entity.java => HttpClient4BodyEntity.java} | 15 +- .../engine/httpclient4/HttpClient4Engine.java | 2 +- ...Entity.java => HttpClient5BodyEntity.java} | 15 +- .../engine/httpclient5/HttpClient5Engine.java | 2 +- 13 files changed, 463 insertions(+), 169 deletions(-) create mode 100755 hutool-core/src/main/java/cn/hutool/core/io/CircularByteBuffer.java rename hutool-http/src/main/java/cn/hutool/http/client/engine/httpclient4/{RequestBodyEntity.java => HttpClient4BodyEntity.java} (66%) rename hutool-http/src/main/java/cn/hutool/http/client/engine/httpclient5/{RequestBodyEntity.java => HttpClient5BodyEntity.java} (66%) diff --git a/hutool-core/src/main/java/cn/hutool/core/io/CircularByteBuffer.java b/hutool-core/src/main/java/cn/hutool/core/io/CircularByteBuffer.java new file mode 100755 index 000000000..72508e1d7 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/io/CircularByteBuffer.java @@ -0,0 +1,239 @@ +package cn.hutool.core.io; + +import java.util.Objects; + +/** + * 循环缓冲区 + * + * @author apache commons io + */ +public class CircularByteBuffer { + private final byte[] buffer; + private int startOffset; + private int endOffset; + private int currentNumberOfBytes; + + /** + * 默认缓冲大小的构造({@link IoUtil#DEFAULT_BUFFER_SIZE}) + */ + public CircularByteBuffer() { + this(IoUtil.DEFAULT_BUFFER_SIZE); + } + + /** + * 构造 + * + * @param pSize 缓冲大小 + */ + public CircularByteBuffer(final int pSize) { + buffer = new byte[pSize]; + startOffset = 0; + endOffset = 0; + currentNumberOfBytes = 0; + } + + /** + * 从buffer中读取下一个byte,同时移除这个bytes。 + * + * @return The byte + * @throws IllegalStateException buffer为空抛出,使用{@link #hasBytes()},或 {@link #getCurrentNumberOfBytes()}判断 + */ + public byte read() { + if (currentNumberOfBytes <= 0) { + throw new IllegalStateException("No bytes available."); + } + final byte b = buffer[startOffset]; + --currentNumberOfBytes; + if (++startOffset == buffer.length) { + startOffset = 0; + } + return b; + } + + /** + * Returns the given number of bytes from the buffer by storing them in + * the given byte array at the given offset. + * 从buffer中获取指定长度的bytes,从给定的targetBuffer的targetOffset位置写出 + * + * @param targetBuffer 目标bytes + * @param targetOffset 目标数组开始位置 + * @param length 读取长度 + * @throws NullPointerException 提供的数组为{@code null} + * @throws IllegalArgumentException {@code targetOffset}或{@code length} 为负数或{@code targetBuffer}太小 + * @throws IllegalStateException buffer中的byte不足,使用{@link #getCurrentNumberOfBytes()}判断。 + */ + public void read(final byte[] targetBuffer, final int targetOffset, final int length) { + Objects.requireNonNull(targetBuffer); + if (targetOffset < 0 || targetOffset >= targetBuffer.length) { + throw new IllegalArgumentException("Invalid offset: " + targetOffset); + } + if (length < 0 || length > buffer.length) { + throw new IllegalArgumentException("Invalid length: " + length); + } + if (targetOffset + length > targetBuffer.length) { + throw new IllegalArgumentException("The supplied byte array contains only " + + targetBuffer.length + " bytes, but offset, and length would require " + + (targetOffset + length - 1)); + } + if (currentNumberOfBytes < length) { + throw new IllegalStateException("Currently, there are only " + currentNumberOfBytes + + "in the buffer, not " + length); + } + int offset = targetOffset; + for (int i = 0; i < length; i++) { + targetBuffer[offset++] = buffer[startOffset]; + --currentNumberOfBytes; + if (++startOffset == buffer.length) { + startOffset = 0; + } + } + } + + /** + * 增加byte到buffer中 + * + * @param value The byte + * @throws IllegalStateException buffer已满. 用{@link #hasSpace()}或{@link #getSpace()}判断。 + */ + public void add(final byte value) { + if (currentNumberOfBytes >= buffer.length) { + throw new IllegalStateException("No space available"); + } + buffer[endOffset] = value; + ++currentNumberOfBytes; + if (++endOffset == buffer.length) { + endOffset = 0; + } + } + + /** + * Returns, whether the next bytes in the buffer are exactly those, given by + * {@code sourceBuffer}, {@code offset}, and {@code length}. No bytes are being + * removed from the buffer. If the result is true, then the following invocations + * of {@link #read()} are guaranteed to return exactly those bytes. + * + * @param sourceBuffer the buffer to compare against + * @param offset start offset + * @param length length to compare + * @return True, if the next invocations of {@link #read()} will return the + * bytes at offsets {@code pOffset}+0, {@code pOffset}+1, ..., + * {@code pOffset}+{@code pLength}-1 of byte array {@code pBuffer}. + * @throws IllegalArgumentException Either of {@code pOffset}, or {@code pLength} is negative. + * @throws NullPointerException The byte array {@code pBuffer} is null. + */ + public boolean peek(final byte[] sourceBuffer, final int offset, final int length) { + Objects.requireNonNull(sourceBuffer, "Buffer"); + if (offset < 0 || offset >= sourceBuffer.length) { + throw new IllegalArgumentException("Invalid offset: " + offset); + } + if (length < 0 || length > buffer.length) { + throw new IllegalArgumentException("Invalid length: " + length); + } + if (length < currentNumberOfBytes) { + return false; + } + int localOffset = startOffset; + for (int i = 0; i < length; i++) { + if (buffer[localOffset] != sourceBuffer[i + offset]) { + return false; + } + if (++localOffset == buffer.length) { + localOffset = 0; + } + } + return true; + } + + /** + * Adds the given bytes to the buffer. This is the same as invoking {@link #add(byte)} + * for the bytes at offsets {@code offset+0}, {@code offset+1}, ..., + * {@code offset+length-1} of byte array {@code targetBuffer}. + * + * @param targetBuffer the buffer to copy + * @param offset start offset + * @param length length to copy + * @throws IllegalStateException The buffer doesn't have sufficient space. Use + * {@link #getSpace()} to prevent this exception. + * @throws IllegalArgumentException Either of {@code pOffset}, or {@code pLength} is negative. + * @throws NullPointerException The byte array {@code pBuffer} is null. + */ + public void add(final byte[] targetBuffer, final int offset, final int length) { + Objects.requireNonNull(targetBuffer, "Buffer"); + if (offset < 0 || offset >= targetBuffer.length) { + throw new IllegalArgumentException("Invalid offset: " + offset); + } + if (length < 0) { + throw new IllegalArgumentException("Invalid length: " + length); + } + if (currentNumberOfBytes + length > buffer.length) { + throw new IllegalStateException("No space available"); + } + for (int i = 0; i < length; i++) { + buffer[endOffset] = targetBuffer[offset + i]; + if (++endOffset == buffer.length) { + endOffset = 0; + } + } + currentNumberOfBytes += length; + } + + /** + * Returns, whether there is currently room for a single byte in the buffer. + * Same as {@link #hasSpace(int) hasSpace(1)}. + * + * @return true if there is space for a byte + * @see #hasSpace(int) + * @see #getSpace() + */ + public boolean hasSpace() { + return currentNumberOfBytes < buffer.length; + } + + /** + * Returns, whether there is currently room for the given number of bytes in the buffer. + * + * @param count the byte count + * @return true if there is space for the given number of bytes + * @see #hasSpace() + * @see #getSpace() + */ + public boolean hasSpace(final int count) { + return currentNumberOfBytes + count <= buffer.length; + } + + /** + * Returns, whether the buffer is currently holding, at least, a single byte. + * + * @return true if the buffer is not empty + */ + public boolean hasBytes() { + return currentNumberOfBytes > 0; + } + + /** + * Returns the number of bytes, that can currently be added to the buffer. + * + * @return the number of bytes that can be added + */ + public int getSpace() { + return buffer.length - currentNumberOfBytes; + } + + /** + * Returns the number of bytes, that are currently present in the buffer. + * + * @return the number of bytes + */ + public int getCurrentNumberOfBytes() { + return currentNumberOfBytes; + } + + /** + * Removes all bytes from the buffer. + */ + public void clear() { + startOffset = 0; + endOffset = 0; + currentNumberOfBytes = 0; + } +} 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 a3ddb92e3..40a55393f 100755 --- a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java @@ -1,6 +1,6 @@ package cn.hutool.core.io; -import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.compress.ZipUtil; import cn.hutool.core.io.file.FileCopier; import cn.hutool.core.io.file.FileMode; import cn.hutool.core.io.file.FileNameUtil; @@ -14,15 +14,15 @@ import cn.hutool.core.io.resource.ResourceUtil; import cn.hutool.core.io.unit.DataSizeUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.func.SerConsumer; +import cn.hutool.core.net.url.URLUtil; +import cn.hutool.core.reflect.ClassUtil; +import cn.hutool.core.regex.ReUtil; +import cn.hutool.core.text.StrUtil; import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.CharUtil; import cn.hutool.core.util.CharsetUtil; -import cn.hutool.core.reflect.ClassUtil; -import cn.hutool.core.regex.ReUtil; -import cn.hutool.core.text.StrUtil; -import cn.hutool.core.net.url.URLUtil; -import cn.hutool.core.compress.ZipUtil; +import cn.hutool.core.util.SystemUtil; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; @@ -52,7 +52,6 @@ import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Collection; import java.util.Date; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -410,16 +409,6 @@ public class FileUtil extends PathUtil { return new File(URLUtil.toURI(url)); } - /** - * 获取临时文件路径(绝对路径) - * - * @return 临时文件路径 - * @since 4.0.6 - */ - public static String getTmpDirPath() { - return System.getProperty("java.io.tmpdir"); - } - /** * 获取临时文件目录 * @@ -427,17 +416,7 @@ public class FileUtil extends PathUtil { * @since 4.0.6 */ public static File getTmpDir() { - return file(getTmpDirPath()); - } - - /** - * 获取用户路径(绝对路径) - * - * @return 用户路径 - * @since 4.0.6 - */ - public static String getUserHomePath() { - return System.getProperty("user.home"); + return file(SystemUtil.getTmpDirPath()); } /** @@ -447,7 +426,7 @@ public class FileUtil extends PathUtil { * @since 4.0.6 */ public static File getUserHomeDir() { - return file(getUserHomePath()); + return file(SystemUtil.getUserHomePath()); } /** @@ -1232,7 +1211,7 @@ public class FileUtil extends PathUtil { if (path == null) { normalPath = StrUtil.EMPTY; } else { - normalPath = normalize(path); + normalPath = FileNameUtil.normalize(path); if (isAbsolutePath(normalPath)) { // 给定的路径已经是绝对路径了 return normalPath; @@ -1243,7 +1222,7 @@ public class FileUtil extends PathUtil { final URL url = ResourceUtil.getResourceUrl(normalPath, baseClass); if (null != url) { // 对于jar中文件包含file:前缀,需要去掉此类前缀,在此做标准化,since 3.0.8 解决中文或空格路径被编码的问题 - return FileUtil.normalize(URLUtil.getDecodedPath(url)); + return FileNameUtil.normalize(URLUtil.getDecodedPath(url)); } // 如果资源不存在,则返回一个拼接的资源绝对路径 @@ -1255,7 +1234,7 @@ public class FileUtil extends PathUtil { } // 资源不存在的情况下使用标准化路径有问题,使用原始路径拼接后标准化路径 - return normalize(classPath.concat(Objects.requireNonNull(path))); + return FileNameUtil.normalize(classPath.concat(Objects.requireNonNull(path))); } /** @@ -1290,7 +1269,7 @@ public class FileUtil extends PathUtil { /** * 给定路径已经是绝对路径
- * 此方法并没有针对路径做标准化,建议先执行{@link #normalize(String)}方法标准化路径后判断
+ * 此方法并没有针对路径做标准化,建议先执行{@link FileNameUtil#normalize(String)}方法标准化路径后判断
* 绝对路径判断条件是: *