From d0d4e0e633b810696a16b61d5bb51b5552018912 Mon Sep 17 00:00:00 2001 From: Timi Date: Mon, 5 Jan 2026 01:51:58 +0800 Subject: [PATCH] add toOutputStream(InputStream is, OutputStream os, long start, long end) --- src/main/java/com/imyeyu/io/IO.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/java/com/imyeyu/io/IO.java b/src/main/java/com/imyeyu/io/IO.java index 3dc13d2..f366b89 100644 --- a/src/main/java/com/imyeyu/io/IO.java +++ b/src/main/java/com/imyeyu/io/IO.java @@ -643,6 +643,24 @@ public class IO implements OS.FileSystem { return !interrupt; } + public static void toOutputStream(InputStream is, OutputStream os, long start, long end) throws IOException { + long skip = is.skip(start); + if (skip != start) { + throw new TimiException(TimiCode.ERROR, "skip fail"); + } + byte[] buffer = new byte[8192]; + long remaining = end - start + 1; + while (0 < remaining) { + int toRead = (int) Math.min(buffer.length, remaining); + int read = is.read(buffer, 0, toRead); + if (read == -1) { + break; + } + os.write(buffer, 0, read); + remaining -= read; + } + } + /** * 写入字节到文件随机访问对象,此操作不会关闭流 *