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;
}
}

web.xml中load-on-startup的作用

  1. load-on-startup 元素标记容器是否应该在web应用程序启动的时候就加载这个servlet,(实例化并调用其init()方法)。
  2. 它的值必须是一个整数,表示servlet被加载的先后顺序。
  3. 如果该元素的值为负数或者没有设置,则容器会当Servlet被请求时再加载。
  4. 如果值为正整数或者0时,表示容器在应用启动时就加载并初始化这个servlet,值越小,servlet的优先级越高,就越先被加载。值相同时,容器就会自己选择顺序来加载。

TortoiseGit中known changes与unknown changes区别

示意图

官方介绍

known changes - This allows remote repository to accept a safer non-fast-forward push. This can cause the remote repository to lose commits; use it with care. This can prevent from losing unknown changes from other people on the remote. It checks if the server branch points to the same commit as the remote-tracking branch (known changes). If yes, a force push will be performed. Otherwise it will be rejected. Since git does not have remote-tracking tags, tags cannot be overwritten using this option. This passes –force-with-lease option of git push command.

unknown changes - This allows remote repository to accept an unsafe non-fast-forward push. This can cause the remote repository to lose commits; use it with care. This does not check any server commits, so it is possible to lose unknown changes on the remote. Use this option with Include Tags to overwrite tags. This passes the traditional –force option of git push command.

简单理解

unknown changes 等价于--force,强行提交时,可能覆盖团队成员在此期间推送的所有更改。

known changes 等价于--force-with-lease,使用此参数推送,如果远端有其他人推送了新的提交,那么推送将被拒绝。该命令解决的是本地仓库不够新时,依然覆盖了远端新仓库的问题,如果执意想要覆盖远端提交,只需要先 fetch 再push,它也不会拒绝的。

Python3-enum枚举类使用示例

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
from enum import Enum

# 声明枚举类
class Branch(Enum):
MAIN = "main branch"
DEV = "develop branch"


print(Branch.MAIN.name) # MAIN
print(Branch.MAIN.value) # main branch
print(Branch["MAIN"]) # Branch.MAIN
print(Branch("main branch")) # Branch.MAIN
print(Branch.MAIN) # Branch.MAIN

print(Branch.MAIN == Branch.MAIN) # True
print(Branch.MAIN == Branch.DEV) # False

print(Branch.MAIN is Branch.MAIN) # True

for branch in Branch:
print(branch)
# Branch.MAIN
# Branch.DEV

for branch in Branch.__members__:
print(branch)
# MAIN
# DEV