diff --git a/agents-flex-core/src/main/java/com/agentsflex/core/document/parser/DefaultTextDocumentParser.java b/agents-flex-core/src/main/java/com/agentsflex/core/document/parser/DefaultTextDocumentParser.java index 08d527f..0641bdb 100644 --- a/agents-flex-core/src/main/java/com/agentsflex/core/document/parser/DefaultTextDocumentParser.java +++ b/agents-flex-core/src/main/java/com/agentsflex/core/document/parser/DefaultTextDocumentParser.java @@ -17,26 +17,18 @@ package com.agentsflex.core.document.parser; import com.agentsflex.core.document.Document; import com.agentsflex.core.document.DocumentParser; +import com.agentsflex.core.util.IOUtil; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.nio.charset.StandardCharsets; public class DefaultTextDocumentParser implements DocumentParser { @Override public Document parse(InputStream stream) { try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - int len; - while ((len = stream.read(buffer)) != -1) { - baos.write(buffer, 0, len); - } - return new Document(new String(baos.toByteArray(), StandardCharsets.UTF_8)); + return Document.of(IOUtil.readUtf8(stream)); } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException(e); } - return null; } } diff --git a/agents-flex-core/src/main/java/com/agentsflex/core/image/Image.java b/agents-flex-core/src/main/java/com/agentsflex/core/image/Image.java index 22ee6a4..775c4ff 100644 --- a/agents-flex-core/src/main/java/com/agentsflex/core/image/Image.java +++ b/agents-flex-core/src/main/java/com/agentsflex/core/image/Image.java @@ -15,9 +15,9 @@ */ package com.agentsflex.core.image; +import com.agentsflex.core.util.IOUtil; + import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; import java.util.Arrays; public class Image { @@ -78,11 +78,7 @@ public class Image { } public void writeBytesToFile(File file) { - try (FileOutputStream stream = new FileOutputStream(file)) { - stream.write(bytes); - } catch (IOException e) { - throw new RuntimeException(e); - } + IOUtil.writeBytes(this.bytes, file); } @Override diff --git a/agents-flex-core/src/main/java/com/agentsflex/core/llm/client/HttpClient.java b/agents-flex-core/src/main/java/com/agentsflex/core/llm/client/HttpClient.java index 932b60c..6ee5b84 100644 --- a/agents-flex-core/src/main/java/com/agentsflex/core/llm/client/HttpClient.java +++ b/agents-flex-core/src/main/java/com/agentsflex/core/llm/client/HttpClient.java @@ -15,8 +15,10 @@ */ package com.agentsflex.core.llm.client; +import com.agentsflex.core.util.IOUtil; import okhttp3.*; import okio.BufferedSink; +import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -191,11 +193,8 @@ public class HttpClient { } @Override - public void writeTo(BufferedSink sink) throws IOException { - byte[] buffer = new byte[1024]; - for (int len; (len = inputStream.read(buffer)) != -1; ) { - sink.write(buffer, 0, len); - } + public void writeTo(@NotNull BufferedSink sink) throws IOException { + IOUtil.copy(inputStream, sink); } } } diff --git a/agents-flex-core/src/main/java/com/agentsflex/core/util/IOUtil.java b/agents-flex-core/src/main/java/com/agentsflex/core/util/IOUtil.java new file mode 100644 index 0000000..e213a0a --- /dev/null +++ b/agents-flex-core/src/main/java/com/agentsflex/core/util/IOUtil.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023-2025, Agents-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.agentsflex.core.util; + +import okio.BufferedSink; + +import java.io.*; +import java.nio.charset.StandardCharsets; + +public class IOUtil { + + public static void writeBytes(byte[] bytes, File toFile) { + try (FileOutputStream stream = new FileOutputStream(toFile)) { + stream.write(bytes); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static void copy(InputStream inputStream, BufferedSink sink) throws IOException { + byte[] buffer = new byte[1024]; + for (int len; (len = inputStream.read(buffer)) != -1; ) { + sink.write(buffer, 0, len); + } + } + + public static void copy(InputStream inputStream, OutputStream outStream) throws IOException { + byte[] buffer = new byte[1024]; + for (int len; (len = inputStream.read(buffer)) != -1; ) { + outStream.write(buffer, 0, len); + } + } + + public static String readUtf8(InputStream inputStream) throws IOException { + ByteArrayOutputStream outStream = new ByteArrayOutputStream(); + copy(inputStream, outStream); + return new String(outStream.toByteArray(), StandardCharsets.UTF_8); + } +}