|
| 1 | +package datadog.trace.bootstrap.instrumentation.buffer; |
| 2 | + |
| 3 | +import java.io.FilterOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.OutputStream; |
| 6 | + |
| 7 | +/** |
| 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 | + */ |
| 11 | +public class InjectingPipeOutputStream extends FilterOutputStream { |
| 12 | + private final byte[] lookbehind; |
| 13 | + private int pos; |
| 14 | + private boolean bufferFilled; |
| 15 | + private final byte[] marker; |
| 16 | + private final byte[] contentToInject; |
| 17 | + private boolean found = false; |
| 18 | + private int matchingPos = 0; |
| 19 | + private final Runnable onContentInjected; |
| 20 | + private final int bulkWriteThreshold; |
| 21 | + |
| 22 | + /** |
| 23 | + * @param downstream the delegate output stream |
| 24 | + * @param marker the marker to find in the stream |
| 25 | + * @param contentToInject the content to inject once before the marker if found. |
| 26 | + * @param onContentInjected callback called when and if the content is injected. |
| 27 | + */ |
| 28 | + public InjectingPipeOutputStream( |
| 29 | + final OutputStream downstream, |
| 30 | + final byte[] marker, |
| 31 | + final byte[] contentToInject, |
| 32 | + final Runnable onContentInjected) { |
| 33 | + super(downstream); |
| 34 | + this.marker = marker; |
| 35 | + this.lookbehind = new byte[marker.length]; |
| 36 | + this.pos = 0; |
| 37 | + this.contentToInject = contentToInject; |
| 38 | + this.onContentInjected = onContentInjected; |
| 39 | + this.bulkWriteThreshold = marker.length * 2 - 2; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void write(int b) throws IOException { |
| 44 | + if (found) { |
| 45 | + out.write(b); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (bufferFilled) { |
| 50 | + out.write(lookbehind[pos]); |
| 51 | + } |
| 52 | + |
| 53 | + lookbehind[pos] = (byte) b; |
| 54 | + pos = (pos + 1) % lookbehind.length; |
| 55 | + |
| 56 | + if (!bufferFilled) { |
| 57 | + bufferFilled = pos == 0; |
| 58 | + } |
| 59 | + |
| 60 | + if (marker[matchingPos++] == b) { |
| 61 | + if (matchingPos == marker.length) { |
| 62 | + found = true; |
| 63 | + out.write(contentToInject); |
| 64 | + if (onContentInjected != null) { |
| 65 | + onContentInjected.run(); |
| 66 | + } |
| 67 | + drain(); |
| 68 | + } |
| 69 | + } else { |
| 70 | + matchingPos = 0; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 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; |
| 79 | + } |
| 80 | + if (len > bulkWriteThreshold) { |
| 81 | + // if the content is large enough, we can bulk write everything but the N trail and tail. |
| 82 | + // This because the buffer can already contain some byte from a previous single write. |
| 83 | + // Also we need to fill the buffer with the tail since we don't know about the next write. |
| 84 | + int idx = arrayContains(b, marker); |
| 85 | + if (idx >= 0) { |
| 86 | + // we have a full match. just write everything |
| 87 | + found = true; |
| 88 | + drain(); |
| 89 | + out.write(b, off, idx); |
| 90 | + out.write(contentToInject); |
| 91 | + if (onContentInjected != null) { |
| 92 | + onContentInjected.run(); |
| 93 | + } |
| 94 | + out.write(b, off + idx, len - idx); |
| 95 | + } else { |
| 96 | + // we don't have a full match. write everything in a bulk except the lookbehind buffer |
| 97 | + // sequentially |
| 98 | + for (int i = off; i < off + marker.length - 1; i++) { |
| 99 | + write(b[i]); |
| 100 | + } |
| 101 | + drain(); |
| 102 | + out.write(b, off + marker.length - 1, len - bulkWriteThreshold); |
| 103 | + for (int i = len - marker.length + 1; i < len; i++) { |
| 104 | + write(b[i]); |
| 105 | + } |
| 106 | + } |
| 107 | + } else { |
| 108 | + // use slow path because the length to write is small and within the lookbehind buffer size |
| 109 | + super.write(b, off, len); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private int arrayContains(byte[] array, byte[] search) { |
| 114 | + for (int i = 0; i < array.length - search.length; i++) { |
| 115 | + if (array[i] == search[0]) { |
| 116 | + boolean found = true; |
| 117 | + int k = i; |
| 118 | + for (int j = 1; j < search.length; j++) { |
| 119 | + k++; |
| 120 | + if (array[k] != search[j]) { |
| 121 | + found = false; |
| 122 | + break; |
| 123 | + } |
| 124 | + } |
| 125 | + if (found) { |
| 126 | + return i; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return -1; |
| 131 | + } |
| 132 | + |
| 133 | + private void drain() throws IOException { |
| 134 | + if (bufferFilled) { |
| 135 | + for (int i = 0; i < lookbehind.length; i++) { |
| 136 | + out.write(lookbehind[(pos + i) % lookbehind.length]); |
| 137 | + } |
| 138 | + } else { |
| 139 | + out.write(this.lookbehind, 0, pos); |
| 140 | + } |
| 141 | + pos = 0; |
| 142 | + matchingPos = 0; |
| 143 | + bufferFilled = false; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void close() throws IOException { |
| 148 | + if (!found) { |
| 149 | + drain(); |
| 150 | + } |
| 151 | + super.close(); |
| 152 | + } |
| 153 | +} |
0 commit comments