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; 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.text.StrUtil;
import org.dromara.hutool.core.util.ByteUtil; import org.dromara.hutool.core.util.ByteUtil;
import org.dromara.hutool.core.text.CharUtil;
import org.dromara.hutool.core.util.CharsetUtil; import org.dromara.hutool.core.util.CharsetUtil;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -36,26 +36,56 @@ import java.util.Arrays;
*/ */
public class BufferUtil { public class BufferUtil {
// region ----- of
/** /**
* {@link ByteBuffer} 转byte数组 * 创建新Buffer
* *
* @param bytebuffer {@link ByteBuffer} * @param data 数据
* @return byte数组 * @return {@link ByteBuffer}
* @since 4.5.0
*/ */
public static byte[] toBytes(final ByteBuffer bytebuffer) { public static ByteBuffer of(final byte[] data) {
if (bytebuffer.hasArray()) { return ByteBuffer.wrap(data);
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;
}
} }
/**
* 从字符串创建新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 * 拷贝到一个新的ByteBuffer
* *
@ -105,6 +135,9 @@ public class BufferUtil {
System.arraycopy(src.array(), srcStart, dest.array(), destStart, length); System.arraycopy(src.array(), srcStart, dest.array(), destStart, length);
return dest; return dest;
} }
// endregion
// region ----- read
/** /**
* 读取剩余部分并转为UTF-8编码字符串 * 读取剩余部分并转为UTF-8编码字符串
@ -174,6 +207,81 @@ public class BufferUtil {
return bs; 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到结束位置 * 一行的末尾位置查找位置时位移ByteBuffer到结束位置
* *
@ -226,76 +334,5 @@ public class BufferUtil {
// 读到结束位置 // 读到结束位置
return -1; return -1;
} }
// endregion
/**
* 读取一行如果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);
}
} }

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

View File

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