import java util regex Matcher import java util regex Pattern import s

 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
45
46
47
48
49
50
51
52
53
54
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.System.arraycopy;
public final class StringMem {
private static final int BYTES_LENGTH = 4 * 1024 * 1024;
public static void main(String[] args) {
String sample = "HelloWorld";
byte[] bytes = new byte[BYTES_LENGTH];
arraycopy(sample.getBytes(), 0, bytes, 32, sample.getBytes().length);
System.out.println();
Pattern pattern = Pattern.compile("(World)");
Matcher matcher = pattern.matcher(new Seq(bytes, 0, bytes.length));
while (matcher.find()) {
System.out.println(matcher.group(1));
}
}
private static final class Seq implements CharSequence {
private final byte[] bytes;
private final int start;
private final int end;
private Seq(byte[] bytes, int start, int end) {
this.bytes = bytes;
this.start = start;
this.end = end;
}
@Override
public int length() {
return bytes.length;
}
@Override
public char charAt(int index) {
return (char) bytes[index];
}
@Override
public CharSequence subSequence(int start, int end) {
return new Seq(bytes, start, end);
}
@Override
public String toString() {
return new String(bytes, start, end - start);
}
}
}