001/* 002 * Copyright (c) 2023-2026, Agents-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.agentsflex.core.file2text; 017 018 019import com.agentsflex.core.file2text.source.ByteArrayDocumentSource; 020import com.agentsflex.core.file2text.source.ByteStreamDocumentSource; 021import com.agentsflex.core.file2text.source.FileDocumentSource; 022import com.agentsflex.core.file2text.source.HttpDocumentSource; 023 024import java.io.File; 025import java.io.InputStream; 026 027public class File2TextUtil { 028 private static File2TextService file2TextService = new File2TextService(); 029 030 public static void setFile2TextService(File2TextService file2TextService) { 031 if (file2TextService == null) { 032 throw new IllegalArgumentException("File2TextService cannot be null"); 033 } 034 File2TextUtil.file2TextService = file2TextService; 035 } 036 037 public static String readFromHttpUrl(String httpUrl) { 038 return file2TextService.extractTextFromSource(new HttpDocumentSource(httpUrl)); 039 } 040 041 public static String readFromHttpUrl(String httpUrl, String fileName) { 042 return file2TextService.extractTextFromSource(new HttpDocumentSource(httpUrl, fileName)); 043 } 044 045 public static String readFromHttpUrl(String httpUrl, String fileName, String mimeType) { 046 return file2TextService.extractTextFromSource(new HttpDocumentSource(httpUrl, fileName, mimeType)); 047 } 048 049 public static String readFromFile(File file) { 050 return file2TextService.extractTextFromSource(new FileDocumentSource(file)); 051 } 052 053 public static String readFromStream(InputStream is, String fileName, String mimeType) { 054 return file2TextService.extractTextFromSource(new ByteStreamDocumentSource(is, fileName, mimeType)); 055 } 056 057 public static String readFromBytes(byte[] bytes, String fileName, String mimeType) { 058 return file2TextService.extractTextFromSource(new ByteArrayDocumentSource(bytes, fileName, mimeType)); 059 } 060 061 062}