据我了解,InputStream
是字节流。我对将InputStream对象转换为字节流感兴趣。基本上,是以下方法的实现。
public Stream<byte[]> toStream(final InputStream is,final int bufferSize);
完成此任务的最佳方法是什么?缓冲区大小是一次从InputStream读取的字节数。
据我了解,InputStream
是字节流。我对将InputStream对象转换为字节流感兴趣。基本上,是以下方法的实现。
public Stream<byte[]> toStream(final InputStream is,final int bufferSize);
完成此任务的最佳方法是什么?缓冲区大小是一次从InputStream读取的字节数。
您需要编写自己的Spliterator
,如下所示:
public final class ChunkingInputStreamSpliterator implements Spliterator<byte[]> {
private final InputStream is;
private final int bufferSize;
public ChunkingInputStreamSpliterator(InputStream is,int bufferSize) {
this.is = is;
this.bufferSize = bufferSize;
}
@Override
public boolean tryAdvance(Consumer<? super byte[]> action) {
byte[] bytes;
try {
bytes = this.is.readNBytes(this.bufferSize);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
if (bytes.length == 0)
return false;
action.accept(bytes);
return true;
}
@Override
public Spliterator<byte[]> trySplit() {
return null; // cannot split an InputStream
}
@Override
public long estimateSize() {
return Long.MAX_VALUE; // unknown
}
@Override
public int characteristics() {
return Spliterator.ORDERED | Spliterator.NONNULL;
}
}
然后像这样实现您的方法:
public static Stream<byte[]> toStream(InputStream is,int bufferSize) {
return StreamSupport.stream(new ChunkingInputStreamSpliterator(is,bufferSize),false);
}
如果您没有Java 11,那么就没有非常方便的readNBytes
方法,那么您就可以自己这样做:
public boolean tryAdvance(Consumer<? super byte[]> action) {
byte[] bytes = new byte[this.bufferSize];
int len = 0;
try {
for (int read; len < bytes.length; len += read)
if ((read = this.is.read(bytes,len,bytes.length - len)) <= 0)
break;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
if (len == 0)
return false;
if (len < bytes.length)
bytes = Arrays.copyOfRange(bytes,len);
action.accept(bytes);
return true;
}