This commit is contained in:
Looly 2024-09-30 17:47:44 +08:00
parent a19eb17daa
commit c7881c74fd
4 changed files with 137 additions and 100 deletions

View File

@ -16,9 +16,9 @@
package org.dromara.hutool.core.io.buffer;
import org.dromara.hutool.core.text.CharUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ByteUtil;
import org.dromara.hutool.core.text.CharUtil;
import org.dromara.hutool.core.util.CharsetUtil;
import java.nio.ByteBuffer;
@ -36,26 +36,56 @@ import java.util.Arrays;
*/
public class BufferUtil {
// region ----- of
/**
* {@link ByteBuffer} 转byte数组
* 创建新Buffer
*
* @param bytebuffer {@link ByteBuffer}
* @return byte数组
* @param data 数据
* @return {@link ByteBuffer}
* @since 4.5.0
*/
public static byte[] toBytes(final ByteBuffer bytebuffer) {
if (bytebuffer.hasArray()) {
return Arrays.copyOfRange(bytebuffer.array(), bytebuffer.position(), bytebuffer.limit());
} else {
final int oldPosition = bytebuffer.position();
bytebuffer.position(0);
final int size = bytebuffer.limit();
final byte[] buffers = new byte[size];
bytebuffer.get(buffers);
bytebuffer.position(oldPosition);
return buffers;
public static ByteBuffer of(final byte[] data) {
return ByteBuffer.wrap(data);
}
/**
* 从字符串创建新Buffer
*
* @param data 数据
* @param charset 编码
* @return {@link ByteBuffer}
* @since 4.5.0
*/
public static ByteBuffer of(final CharSequence data, final Charset charset) {
return of(ByteUtil.toBytes(data, charset));
}
/**
* 从字符串创建新Buffer使用UTF-8编码
*
* @param data 数据
* @return {@link ByteBuffer}
* @since 4.5.0
*/
public static ByteBuffer ofUtf8(final CharSequence data) {
return of(ByteUtil.toUtf8Bytes(data));
}
/**
* 创建{@link CharBuffer}
*
* @param capacity 容量
* @return {@link CharBuffer}
* @since 5.5.7
*/
public static CharBuffer ofCharBuffer(final int capacity) {
return CharBuffer.allocate(capacity);
}
// endregion
// region ----- copy
/**
* 拷贝到一个新的ByteBuffer
*
@ -105,6 +135,9 @@ public class BufferUtil {
System.arraycopy(src.array(), srcStart, dest.array(), destStart, length);
return dest;
}
// endregion
// region ----- read
/**
* 读取剩余部分并转为UTF-8编码字符串
@ -174,6 +207,81 @@ public class BufferUtil {
return bs;
}
/**
* 读取一行如果buffer中最后一部分并非完整一行则返回null<br>
* 支持的换行符如下
*
* <pre>
* 1. \r\n
* 2. \n
* </pre>
*
* @param buffer ByteBuffer
* @param charset 编码
* @return 一行
*/
public static String readLine(final ByteBuffer buffer, final Charset charset) {
final int startPosition = buffer.position();
final int endPosition = lineEnd(buffer);
if (endPosition > startPosition) {
final byte[] bs = readBytes(buffer, startPosition, endPosition);
return StrUtil.str(bs, charset);
} else if (endPosition == startPosition) {
return StrUtil.EMPTY;
}
return null;
}
// endregion
// region ----- toXXX
/**
* {@link ByteBuffer} 转byte数组
*
* @param bytebuffer {@link ByteBuffer}
* @return byte数组
*/
public static byte[] toBytes(final ByteBuffer bytebuffer) {
if (bytebuffer.hasArray()) {
return Arrays.copyOfRange(bytebuffer.array(), bytebuffer.position(), bytebuffer.limit());
} else {
final int oldPosition = bytebuffer.position();
bytebuffer.position(0);
final int size = bytebuffer.limit();
final byte[] buffers = new byte[size];
bytebuffer.get(buffers);
bytebuffer.position(oldPosition);
return buffers;
}
}
/**
* {@link CharBuffer} {@link ByteBuffer}
*
* @param charBuffer {@link CharBuffer}
* @param charset 编码
* @return {@link ByteBuffer}
*/
public static ByteBuffer toByteBuffer(final CharBuffer charBuffer, final Charset charset) {
return charset.encode(charBuffer);
}
/**
* {@link ByteBuffer} {@link CharBuffer}
*
* @param byteBuffer {@link ByteBuffer}
* @param charset 编码
* @return {@link CharBuffer}
*/
public static CharBuffer toCharBuffer(final ByteBuffer byteBuffer, final Charset charset) {
return charset.decode(byteBuffer);
}
// endregion
// region ----- lineEnd
/**
* 一行的末尾位置查找位置时位移ByteBuffer到结束位置
*
@ -226,76 +334,5 @@ public class BufferUtil {
// 读到结束位置
return -1;
}
/**
* 读取一行如果buffer中最后一部分并非完整一行则返回null<br>
* 支持的换行符如下
*
* <pre>
* 1. \r\n
* 2. \n
* </pre>
*
* @param buffer ByteBuffer
* @param charset 编码
* @return 一行
*/
public static String readLine(final ByteBuffer buffer, final Charset charset) {
final int startPosition = buffer.position();
final int endPosition = lineEnd(buffer);
if (endPosition > startPosition) {
final byte[] bs = readBytes(buffer, startPosition, endPosition);
return StrUtil.str(bs, charset);
} else if (endPosition == startPosition) {
return StrUtil.EMPTY;
}
return null;
}
/**
* 创建新Buffer
*
* @param data 数据
* @return {@link ByteBuffer}
* @since 4.5.0
*/
public static ByteBuffer of(final byte[] data) {
return ByteBuffer.wrap(data);
}
/**
* 从字符串创建新Buffer
*
* @param data 数据
* @param charset 编码
* @return {@link ByteBuffer}
* @since 4.5.0
*/
public static ByteBuffer of(final CharSequence data, final Charset charset) {
return of(ByteUtil.toBytes(data, charset));
}
/**
* 从字符串创建新Buffer使用UTF-8编码
*
* @param data 数据
* @return {@link ByteBuffer}
* @since 4.5.0
*/
public static ByteBuffer ofUtf8(final CharSequence data) {
return of(ByteUtil.toUtf8Bytes(data));
}
/**
* 创建{@link CharBuffer}
*
* @param capacity 容量
* @return {@link CharBuffer}
* @since 5.5.7
*/
public static CharBuffer ofCharBuffer(final int capacity) {
return CharBuffer.allocate(capacity);
}
// endregion
}

View File

@ -107,9 +107,9 @@ public abstract class FastBuffer {
}
/**
* 分配下一个缓冲区不会小于1024
* 检查现有缓冲区是否满足capacity不满足则分配新的区域分配下一个缓冲区不会小于1024
*
* @param newSize 理想缓冲区字节数
* @param capacity 理想缓冲区字节数
*/
abstract protected void needNewBuffer(final int newSize);
abstract protected void ensureCapacity(final int capacity);
}

View File

@ -84,7 +84,7 @@ public class FastByteBuffer extends FastBuffer {
if (remaining > 0) {
// still some data left
// ask for new buffer
needNewBuffer(newSize);
ensureCapacity(newSize);
// then copy remaining
// but this time we are sure that it will fit
@ -115,7 +115,7 @@ public class FastByteBuffer extends FastBuffer {
*/
public FastByteBuffer append(final byte element) {
if ((currentBuffer == null) || (offset == currentBuffer.length)) {
needNewBuffer(size + 1);
ensureCapacity(size + 1);
}
currentBuffer[offset] = element;
@ -248,8 +248,8 @@ public class FastByteBuffer extends FastBuffer {
}
@Override
protected void needNewBuffer(final int newSize) {
final int delta = newSize - size;
protected void ensureCapacity(final int capacity) {
final int delta = capacity - size;
final int newBufferSize = Math.max(minChunkLen, delta);
currentBufferIndex++;

View File

@ -83,7 +83,7 @@ public class FastCharBuffer extends FastBuffer implements CharSequence, Appendab
if (remaining > 0) {
// still some data left
// ask for new buffer
needNewBuffer(newSize);
ensureCapacity(newSize);
// then copy remaining
// but this time we are sure that it will fit
@ -114,7 +114,7 @@ public class FastCharBuffer extends FastBuffer implements CharSequence, Appendab
*/
public FastCharBuffer append(final char element) {
if ((currentBuffer == null) || (offset == currentBuffer.length)) {
needNewBuffer(size + 1);
ensureCapacity(size + 1);
}
currentBuffer[offset] = element;
@ -310,7 +310,7 @@ public class FastCharBuffer extends FastBuffer implements CharSequence, Appendab
if (remaining > 0) {
// still some data left
// ask for new buffer
needNewBuffer(newSize);
ensureCapacity(newSize);
// then copy remaining
// but this time we are sure that it will fit
@ -324,8 +324,8 @@ public class FastCharBuffer extends FastBuffer implements CharSequence, Appendab
}
@Override
protected void needNewBuffer(final int newSize) {
final int delta = newSize - size;
protected void ensureCapacity(final int capacity) {
final int delta = capacity - size;
final int newBufferSize = Math.max(minChunkLen, delta);
currentBufferIndex++;