我正在将大量数据从InputStreams流式传输到OutputStreams。我使用的方法如下所示:
public static void forwardStream(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
现在我需要在写入流内容之前对其执行搜索/替换操作。将整个流加载到 String 中,然后执行 String.replace() 不是一个选项,因为流数据很大。
我的想法是将InputStream包装在某种继承InputStream的帮助器类中,并在读取其内容时执行替换操作。有没有经过验证的解决方案?
最佳答案
我很少知道您需要什么样的过滤。但可能的解决方案之一是扩展 java.io.FilterInputStream 并重写 read() 和 read(byte[] b)
关于java - 输入流和输出流之间的搜索/替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38739460/