This commit is contained in:
Looly 2023-04-18 18:21:15 +08:00
parent aba953cfd2
commit ff382aff85
3 changed files with 30 additions and 1 deletions

View File

@ -12,6 +12,8 @@
package org.dromara.hutool.core.io.buffer;
import org.dromara.hutool.core.io.IORuntimeException;
/**
* 代码移植自<a href="https://github.com/biezhi/blade">blade</a><br>
* 快速缓冲将数据存放在缓冲集中取代以往的单一数组
@ -264,6 +266,23 @@ public class FastByteBuffer {
return array;
}
/**
* 返回快速缓冲中的数据如果缓冲区中的数据长度固定则直接返回原始数组<br>
* 注意此方法共享数组不能修改数组内容
*
* @return 快速缓冲中的数据
*/
public byte[] toArrayZeroCopyIfPossible() {
if(1 == currentBufferIndex){
final int len = buffers[0].length;
if(len == size){
return buffers[0];
}
}
return toArray();
}
/**
* 返回快速缓冲中的数据
*

View File

@ -120,6 +120,16 @@ public class FastByteArrayOutputStream extends OutputStream {
return buffer.toArray();
}
/**
* 转为Byte数组如果缓冲区中的数据长度固定则直接返回原始数组<br>
* 注意此方法共享数组不能修改数组内容
*
* @return Byte数组
*/
public byte[] toByteArrayZeroCopyIfPossible() {
return buffer.toArrayZeroCopyIfPossible();
}
@Override
public String toString() {
return toString(CharsetUtil.defaultCharset());

View File

@ -76,7 +76,7 @@ public class StreamReader {
}
//noinspection resource
return read(length).toByteArray();
return read(length).toByteArrayZeroCopyIfPossible();
}
/**