|
3 | 3 | import org.jcodings.Encoding;
|
4 | 4 | import org.jruby.util.ByteList;
|
5 | 5 |
|
6 |
| -import java.io.ByteArrayOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.OutputStream; |
| 8 | +import java.util.Arrays; |
| 9 | + |
| 10 | +public class ByteListDirectOutputStream extends OutputStream { |
| 11 | + private byte[] buffer; |
| 12 | + private int length; |
7 | 13 |
|
8 |
| -public class ByteListDirectOutputStream extends ByteArrayOutputStream { |
9 | 14 | ByteListDirectOutputStream(int size) {
|
10 |
| - super(size); |
| 15 | + buffer = new byte[size]; |
11 | 16 | }
|
12 | 17 |
|
13 | 18 | public ByteList toByteListDirect(Encoding encoding) {
|
14 |
| - return new ByteList(buf, 0, count, encoding, false); |
| 19 | + return new ByteList(buffer, 0, length, encoding, false); |
| 20 | + } |
| 21 | + |
| 22 | + @Override |
| 23 | + public void write(int b) throws IOException { |
| 24 | + int currentLength = this.length; |
| 25 | + int newLength = currentLength + 1; |
| 26 | + byte[] buffer = ensureBuffer(this, newLength); |
| 27 | + buffer[currentLength] = (byte) b; |
| 28 | + this.length = newLength; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public void write(byte[] bytes, int start, int length) throws IOException { |
| 33 | + int currentLength = this.length; |
| 34 | + int newLength = currentLength + length; |
| 35 | + byte[] buffer = ensureBuffer(this, newLength); |
| 36 | + System.arraycopy(bytes, start, buffer, currentLength, length); |
| 37 | + this.length = newLength; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void write(byte[] bytes) throws IOException { |
| 42 | + int myLength = this.length; |
| 43 | + int moreLength = bytes.length; |
| 44 | + int newLength = myLength + moreLength; |
| 45 | + byte[] buffer = ensureBuffer(this, newLength); |
| 46 | + System.arraycopy(bytes, 0, buffer, myLength, moreLength); |
| 47 | + this.length = newLength; |
| 48 | + } |
| 49 | + |
| 50 | + private static byte[] ensureBuffer(ByteListDirectOutputStream self, int minimumLength) { |
| 51 | + byte[] buffer = self.buffer; |
| 52 | + int myCapacity = buffer.length; |
| 53 | + int diff = minimumLength - myCapacity; |
| 54 | + if (diff > 0) { |
| 55 | + buffer = self.buffer = grow(buffer, myCapacity, diff); |
| 56 | + } |
| 57 | + |
| 58 | + return buffer; |
| 59 | + } |
| 60 | + |
| 61 | + private static byte[] grow(byte[] oldBuffer, int myCapacity, int diff) { |
| 62 | + // grow to double current buffer length or capacity + diff, whichever is greater |
| 63 | + int newLength = myCapacity + Math.max(myCapacity, diff); |
| 64 | + // check overflow |
| 65 | + if (newLength < 0) { |
| 66 | + // try just diff length in case it can fit |
| 67 | + newLength = myCapacity + diff; |
| 68 | + if (newLength < 0) { |
| 69 | + throw new ArrayIndexOutOfBoundsException("cannot allocate array of size " + myCapacity + "+" + diff); |
| 70 | + } |
| 71 | + } |
| 72 | + return Arrays.copyOf(oldBuffer, newLength); |
15 | 73 | }
|
16 | 74 | }
|
0 commit comments