mirror of
https://gitee.com/agents-flex/agents-flex.git
synced 2024-11-29 18:38:17 +08:00
feat: add IOUtil.java
This commit is contained in:
parent
cf1b0d0a50
commit
6ca6b01007
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2025, Agents-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user