- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 InputStream,它将 html 文件作为输入参数。我必须从输入流中获取字节。
我有一个字符串:"XYZ"
。我想将这个字符串转换为字节格式,并检查我从 InputStream 获得的字节序列中是否有匹配的字符串。如果有的话,我必须将匹配项替换为其他字符串的 bye 序列。
有人可以帮我解决这个问题吗?我使用正则表达式查找和替换。但是,我不知道如何查找和替换字节流。
以前,我使用 jsoup 来解析 html 并替换字符串,但是由于一些 utf 编码问题,当我这样做时文件似乎已损坏。
TL;DR:我的问题是:
是否有一种方法可以在 Java 中的原始 InputStream 中查找和替换字节格式的字符串?
最佳答案
不确定您是否选择了解决问题的最佳方法。
就是说,我不喜欢(并且有政策不)用“不要”回答问题,所以这里是...
来自文档:
A FilterInputStream contains some other input stream, which it uses as its basic source of data, possibly transforming the data along the way or providing additional functionality.
把它写下来是一个有趣的练习。这是一个完整的例子:
import java.io.*;
import java.util.*;
class ReplacingInputStream extends FilterInputStream {
LinkedList<Integer> inQueue = new LinkedList<Integer>();
LinkedList<Integer> outQueue = new LinkedList<Integer>();
final byte[] search, replacement;
protected ReplacingInputStream(InputStream in,
byte[] search,
byte[] replacement) {
super(in);
this.search = search;
this.replacement = replacement;
}
private boolean isMatchFound() {
Iterator<Integer> inIter = inQueue.iterator();
for (int i = 0; i < search.length; i++)
if (!inIter.hasNext() || search[i] != inIter.next())
return false;
return true;
}
private void readAhead() throws IOException {
// Work up some look-ahead.
while (inQueue.size() < search.length) {
int next = super.read();
inQueue.offer(next);
if (next == -1)
break;
}
}
@Override
public int read() throws IOException {
// Next byte already determined.
if (outQueue.isEmpty()) {
readAhead();
if (isMatchFound()) {
for (int i = 0; i < search.length; i++)
inQueue.remove();
for (byte b : replacement)
outQueue.offer((int) b);
} else
outQueue.add(inQueue.remove());
}
return outQueue.remove();
}
// TODO: Override the other read methods.
}
class Test {
public static void main(String[] args) throws Exception {
byte[] bytes = "hello xyz world.".getBytes("UTF-8");
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
byte[] search = "xyz".getBytes("UTF-8");
byte[] replacement = "abc".getBytes("UTF-8");
InputStream ris = new ReplacingInputStream(bis, search, replacement);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int b;
while (-1 != (b = ris.read()))
bos.write(b);
System.out.println(new String(bos.toByteArray()));
}
}
给定字符串 "Hello xyz world"
的字节,它打印:
Hello abc world
关于java - 在 InputStream 中过滤(搜索和替换)字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7743534/
我在一个类中有两个方法: private static InputStream getSongStream(String ip, String id){ try { U
我创建了一个扩展 InputStream 的新类并且必须 @Override read()。我正在尝试使用方法 read(int b),但是当我使用它时,它会转到方法read() 和我不能使用参数,我
我正在尝试编写一个函数,该函数将接受带有压缩文件数据的 InputStream 并返回另一个带有解压缩数据的 InputStream。 压缩后的文件将只包含一个文件,因此不需要创建目录等... 我尝试
我想知道是否有任何理想的方式可以将多个 InputStream 链接到 Java(或 Scala)中的一个连续 InputStream 中。 我需要它来解析我从 FTP 服务器通过网络加载的平面文件。
我想做的是:打开大文件的 InputStream,按 10MB block 读取它,上传一个 block ,读取下一个 block 。 val chunkCount = Math.ceil(total
我不知道怎么理解: { if (inputStream **!= null**) { inputStream.close(); 来自那个例子: public c
我想知道 InputStream 是否为空,但不使用 read() 方法。有没有办法不读取就知道它是否为空? 最佳答案 不,你不能。 InputStream 设计用于处理远程资源,因此在实际读取它之前
我制作了一个蓝牙输入流监听器,只需询问 if(InputStream.isAvailable > 0) 即可检查是否有传入数据,然后创建一个循环将传入数据存储到 int[] 直到没有更多并且它工作完美
这是我的代码流程,文件内容丢失,我认为可能是 IOUtils.toByteArray() 行有问题,请指导这里实际出了什么问题。 文件内容丢失: InputStream stream = someCl
我从 HTTP 请求的响应开始: InputStream responseInputStream = response.getEntityInputStream() 我需要对该响应进行 gzip 压缩
用户将一个大文件上传到我的网站,我想对该文件进行 gzip 压缩并将其存储在 blob 中。所以我有一个未压缩的 InputStream,而 blob 需要一个 InputStream。我知道如何使用
我调用了一个返回压缩文件的服务。我从响应中将数据作为 InputStream(由 javax.activation.DataHandler.getInputStream(); 提供)提供。 我想做的是
我正在尝试压缩一个 InputStream 并返回一个 InputStream: public InputStream compress (InputStream in){ // Read "in
我最近在 Kotlin 中看到了将 InputStream 的全部内容读入 String 的代码,例如: // input is of type InputStream val baos = Byte
我正在尝试使用以下代码从 IHTTPSession.getInputStream() 读取 InputStream,但它每次都给出 Socket TimeOut Exception。 private
如 How to use Jersey interceptors to get request body 中所述,我正在修改 ContainerRequestFilter 中的 EntityInput
我正在编写一个需要与蓝牙 2.1 设备交换数据的应用程序。我已经做过好几次了,但这次发生了一些奇怪的事情。 Log.d("TAG", "connectToDevice"); if(ma
我只是在犹豫这是好主意还是坏主意: InputStreamReader in = new InputStreamReader(socket.getInputStream()); BufferedRea
我正在开发一个 Android 应用程序,它的 View 包含多个图库。图库的内容(位图)是来自 Internet 的红色。 对于第一个画廊,一切正常,但是当尝试下载第二个画廊的第一张图片时,Bitm
在Dart中,我想读取BMP,所以可能是BIG文件。 我这样做是这样的: var inputStream = imageFile.openInputStream(); inputStream.onDa
我是一名优秀的程序员,十分优秀!