|
5 | 5 | import java.io.OutputStream; |
6 | 6 |
|
7 | 7 | /** |
8 | | - * A circular buffer that holds n+1 bytes and with a lookbehind buffer of n bytes. The first time |
9 | | - * that the latest n bytes matches the marker, a content is injected before. |
| 8 | + * A circular buffer with a lookbehind buffer of n bytes. The first time that the latest n bytes |
| 9 | + * matches the marker, a content is injected before. |
10 | 10 | */ |
11 | 11 | public class InjectingPipeOutputStream extends FilterOutputStream { |
12 | 12 | private final byte[] lookbehind; |
@@ -43,47 +43,108 @@ public void write(int b) throws IOException { |
43 | 43 | out.write(b); |
44 | 44 | return; |
45 | 45 | } |
| 46 | + |
| 47 | + if (bufferFilled) { |
| 48 | + out.write(lookbehind[pos]); |
| 49 | + } |
| 50 | + |
46 | 51 | lookbehind[pos] = (byte) b; |
47 | 52 | pos = (pos + 1) % lookbehind.length; |
48 | 53 |
|
| 54 | + if (!bufferFilled) { |
| 55 | + bufferFilled = pos == 0; |
| 56 | + } |
| 57 | + |
49 | 58 | if (marker[matchingPos++] == b) { |
50 | 59 | if (matchingPos == marker.length) { |
51 | 60 | found = true; |
| 61 | + out.write(lookbehind[pos]); |
| 62 | + pos = (pos + 1) % lookbehind.length; |
52 | 63 | out.write(contentToInject); |
53 | 64 | if (onContentInjected != null) { |
54 | 65 | onContentInjected.run(); |
55 | 66 | } |
56 | | - drain((pos + 1) % lookbehind.length, marker.length); |
57 | | - return; |
| 67 | + drain(lookbehind.length - 1); |
58 | 68 | } |
59 | 69 | } else { |
60 | 70 | matchingPos = 0; |
61 | 71 | } |
| 72 | + } |
62 | 73 |
|
63 | | - if (!bufferFilled) { |
64 | | - bufferFilled = pos == lookbehind.length - 1; |
| 74 | + @Override |
| 75 | + public void write(byte[] b, int off, int len) throws IOException { |
| 76 | + if (found) { |
| 77 | + out.write(b, off, len); |
| 78 | + return; |
65 | 79 | } |
| 80 | + if (len > marker.length * 2) { |
| 81 | + int idx = arrayContains(b, marker); |
| 82 | + if (idx >= 0) { |
| 83 | + // we have a full match. just write everything |
| 84 | + found = true; |
| 85 | + drain(lookbehind.length); |
| 86 | + out.write(b, off, idx); |
| 87 | + out.write(contentToInject); |
| 88 | + if (onContentInjected != null) { |
| 89 | + onContentInjected.run(); |
| 90 | + } |
| 91 | + out.write(b, off + idx, len - idx); |
| 92 | + } else { |
| 93 | + // we don't have a full match. write everything in a bulk except the lookbehind buffer |
| 94 | + // sequentially |
| 95 | + for (int i = off; i < off + marker.length; i++) { |
| 96 | + write(b[i]); |
| 97 | + } |
| 98 | + drain(lookbehind.length); |
| 99 | + out.write(b, off + marker.length, len - marker.length * 2); |
| 100 | + for (int i = len - marker.length; i < len; i++) { |
| 101 | + write(b[i]); |
| 102 | + } |
| 103 | + drain(lookbehind.length); |
| 104 | + } |
| 105 | + } else { |
| 106 | + // use slow path because the length to write is small and within the lookbehind buffer size |
| 107 | + super.write(b, off, len); |
| 108 | + } |
| 109 | + } |
66 | 110 |
|
67 | | - if (bufferFilled) { |
68 | | - super.write(lookbehind[(pos + 1) % lookbehind.length]); |
| 111 | + private int arrayContains(byte[] array, byte[] search) { |
| 112 | + for (int i = 0; i < array.length - search.length; i++) { |
| 113 | + if (array[i] == search[0]) { |
| 114 | + boolean found = true; |
| 115 | + int k = i; |
| 116 | + for (int j = 1; j < search.length; j++) { |
| 117 | + k++; |
| 118 | + if (array[k] != search[j]) { |
| 119 | + found = false; |
| 120 | + break; |
| 121 | + } |
| 122 | + } |
| 123 | + if (found) { |
| 124 | + return i; |
| 125 | + } |
| 126 | + } |
69 | 127 | } |
| 128 | + return -1; |
70 | 129 | } |
71 | 130 |
|
72 | | - private void drain(int from, int size) throws IOException { |
73 | | - while (size-- > 0) { |
74 | | - super.write(Character.valueOf((char) lookbehind[from])); |
75 | | - from = (from + 1) % lookbehind.length; |
| 131 | + private void drain(int len) throws IOException { |
| 132 | + if (bufferFilled) { |
| 133 | + for (int i = 0; i < len; i++) { |
| 134 | + out.write(lookbehind[(pos + i) % lookbehind.length]); |
| 135 | + } |
| 136 | + } else { |
| 137 | + out.write(this.lookbehind, 0, pos); |
76 | 138 | } |
| 139 | + pos = 0; |
| 140 | + matchingPos = 0; |
| 141 | + bufferFilled = false; |
77 | 142 | } |
78 | 143 |
|
79 | 144 | @Override |
80 | 145 | public void close() throws IOException { |
81 | 146 | if (!found) { |
82 | | - if (bufferFilled) { |
83 | | - drain((pos + 2) % lookbehind.length, marker.length - 1); |
84 | | - } else { |
85 | | - drain(0, pos); |
86 | | - } |
| 147 | + drain(lookbehind.length); |
87 | 148 | } |
88 | 149 | super.close(); |
89 | 150 | } |
|
0 commit comments