Debian Codegraph @

morfologik-tools/src/main/java/morfologik/tools/BinaryInput.java

raw
1 package morfologik.tools;
2
3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.List;
11
12 import com.beust.jcommander.Parameter;
13
14 final class BinaryInput {
15 private static final String ARG_ACCEPT_BOM = "--accept-bom";
16 private static final String ARG_ACCEPT_CR = "--accept-cr";
17 private static final String ARG_IGNORE_EMPTY = "--ignore-empty";
18
19 private static interface LineConsumer {
20 byte[] process(byte[] buffer, int length);
21 }
22
23 @Parameter(
24 names = BinaryInput.ARG_ACCEPT_BOM,
25 arity = 0,
26 description = "Accept leading BOM bytes (UTF-8).")
27 private boolean acceptBom;
28
29 @Parameter(
30 names = BinaryInput.ARG_ACCEPT_CR,
31 arity = 0,
32 description = "Accept CR bytes in input sequences (\\r).")
33 private boolean acceptCr;
34
35 @Parameter(
36 names = BinaryInput.ARG_IGNORE_EMPTY,
37 arity = 0,
38 description = "Ignore empty lines in the input.")
39 private boolean ignoreEmpty;
40
41 BinaryInput() {}
42
43 public BinaryInput(boolean acceptBom,
44 boolean acceptCr,
45 boolean ignoreEmpty) {
46 this.acceptBom = acceptBom;
47 this.acceptCr = acceptCr;
48 this.ignoreEmpty = ignoreEmpty;
49 }
50
51 List<byte[]> readBinarySequences(Path input, byte separator) throws IOException {
52 final List<byte[]> sequences = new ArrayList<>();
53 try (InputStream is = new BufferedInputStream(Files.newInputStream(input))) {
54 if (!acceptBom) {
55 is.mark(4);
56 if (is.read() == 0xef &&
57 is.read() == 0xbb &&
58 is.read() == 0xbf) {
59 throw new ExitStatusException(ExitStatus.ERROR_OTHER,
60 "The input starts with UTF-8 BOM bytes which is" +
61 " most likely not what you want. Use header-less UTF-8 file or override with %s.", ARG_ACCEPT_BOM);
62 }
63 is.reset();
64 }
65
66 forAllLines(is, separator, new LineConsumer() {
67 @Override
68 public byte[] process(byte[] buffer, int length) {
69 if (!acceptCr && hasCr(buffer, length)) {
70 throw new ExitStatusException(ExitStatus.ERROR_OTHER,
71 "The input contains \\r byte (CR) which would be encoded as part of the"
72 + " automaton. If this is desired, use %s.", ARG_ACCEPT_CR);
73 }
74
75 if (length == 0) {
76 if (!ignoreEmpty) {
77 throw new ExitStatusException(ExitStatus.ERROR_OTHER,
78 "The input contains empty sequences."
79 + " If these can be ignored, use --ignore-empty.");
80 }
81 } else {
82 sequences.add(Arrays.copyOf(buffer, length));
83 }
84
85 return buffer;
86 }
87 });
88 }
89
90 return sequences;
91 }
92
93 private static boolean hasCr(byte[] seq, int length) {
94 for (int o = length; --o >= 0;) {
95 if (seq[o] == '\r') {
96 return true;
97 }
98 }
99 return false;
100 }
101
102 /**
103 * Read all byte-separated sequences.
104 */
105 private static int forAllLines(InputStream is, byte separator, LineConsumer lineConsumer) throws IOException {
106 int lines = 0;
107 byte[] buffer = new byte[0];
108 int b, pos = 0;
109 while ((b = is.read()) != -1) {
110 if (b == separator) {
111 buffer = lineConsumer.process(buffer, pos);
112 pos = 0;
113 lines++;
114 } else {
115 if (pos >= buffer.length) {
116 buffer = java.util.Arrays.copyOf(buffer, buffer.length + Math.max(10, buffer.length / 10));
117 }
118 buffer[pos++] = (byte) b;
119 }
120 }
121
122 if (pos > 0) {
123 lineConsumer.process(buffer, pos);
124 lines++;
125 }
126 return lines;
127 }
128 }