Java流内容替换

业务上有需求,需要将日语外字替换成指定字符,利用FilterInputStream实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class App {
public static void main(String[] args) throws Exception {
String str = "abc";
InputStream stream = new ByteArrayInputStream(str.getBytes());
// abc -> acc
replaceString(stream).stream().forEach( elt -> System.out.println(elt));
}

private static List<String> replaceString(InputStream inputStream) throws UnsupportedEncodingException {
List<String> result = new ArrayList<>();
FilterInputStream filterInputStream = new FilterInputStream(inputStream) {
@Override
public int read(byte[] b, int off, int len) throws IOException {
int bytesRead = super.read(b, off, len);
if (bytesRead != -1) {
for (int i = off; i < off + bytesRead - 1; i++) {
// ab -> ac
if (b[i] == (byte) 0x61 && b[i + 1] == (byte) 0x62) {
b[i] = (byte) 0x61;
b[i + 1] = (byte) 0x63;
}
}
}
return bytesRead;
}
};

BufferedReader br = new BufferedReader(new InputStreamReader(filterInputStream, "UTF8"));
br.lines().forEach( elt -> {
result.add(elt);
});
return result;
}
}
作者

Etsu

发布于

2023-06-07

更新于

2023-06-07

许可协议

评论