|
| 1 | +/* |
| 2 | + * Copyright The Hypertrace Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.hypertrace.agent.core.instrumentation.buffer; |
| 18 | + |
| 19 | +import java.io.CharArrayWriter; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.Writer; |
| 22 | + |
| 23 | +public class BoundedCharArrayWriter extends CharArrayWriter { |
| 24 | + |
| 25 | + private final int maxCapacity; |
| 26 | + |
| 27 | + BoundedCharArrayWriter(int maxCapacity) { |
| 28 | + this.maxCapacity = maxCapacity; |
| 29 | + } |
| 30 | + |
| 31 | + BoundedCharArrayWriter(int maxCapacity, int initialSize) { |
| 32 | + super(initialSize); |
| 33 | + this.maxCapacity = maxCapacity; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void write(int c) { |
| 38 | + if (size() == maxCapacity) { |
| 39 | + return; |
| 40 | + } |
| 41 | + super.write(c); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void write(char[] c, int off, int len) { |
| 46 | + int size = size(); |
| 47 | + if (size + len > maxCapacity) { |
| 48 | + super.write(c, off, maxCapacity - size); |
| 49 | + return; |
| 50 | + } |
| 51 | + super.write(c, off, len); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void write(String str, int off, int len) { |
| 56 | + int size = size(); |
| 57 | + if (size + len > maxCapacity) { |
| 58 | + super.write(str, off, maxCapacity - size); |
| 59 | + return; |
| 60 | + } |
| 61 | + super.write(str, off, len); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void writeTo(Writer out) throws IOException { |
| 66 | + // don't need to override, it will copy what is buffered |
| 67 | + super.writeTo(out); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public CharArrayWriter append(CharSequence csq) { |
| 72 | + // the same impl as the super class |
| 73 | + String s = (csq == null ? "null" : csq.toString()); |
| 74 | + this.write(s, 0, s.length()); |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public CharArrayWriter append(CharSequence csq, int start, int end) { |
| 80 | + String s = (csq == null ? "null" : csq).subSequence(start, end).toString(); |
| 81 | + write(s, 0, s.length()); |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public CharArrayWriter append(char c) { |
| 87 | + write(c); |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void write(char[] cbuf) throws IOException { |
| 93 | + this.write(cbuf, 0, cbuf.length); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void write(String str) throws IOException { |
| 98 | + this.write(str, 0, str.length()); |
| 99 | + } |
| 100 | +} |
0 commit comments