mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
a19eb17daa
commit
c7881c74fd
@ -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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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++;
|
||||||
|
@ -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++;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user