|
| 1 | +package json.ext; |
| 2 | + |
| 3 | +import org.jcodings.Encoding; |
| 4 | +import org.jruby.util.ByteList; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | + |
| 8 | +public class SegmentedByteListDirectOutputStream extends AbstractByteListDirectOutputStream { |
| 9 | + private static final int DEFAULT_CAPACITY = 1024; |
| 10 | + |
| 11 | + private int totalLength; |
| 12 | + // Why 21? The minimum segment size is 1024 bytes. If we double the segment size each time |
| 13 | + // we need a new segment, we only need 21 segments to reach the maximum array size in Java. |
| 14 | + private byte[][] segments = new byte[21][]; |
| 15 | + private int currentSegmentIndex; |
| 16 | + private int currentSegmentLength; |
| 17 | + private byte[] currentSegment; |
| 18 | + |
| 19 | + SegmentedByteListDirectOutputStream(int size) { |
| 20 | + currentSegment = new byte[Math.max(size, DEFAULT_CAPACITY)]; |
| 21 | + segments[0] = currentSegment; |
| 22 | + } |
| 23 | + |
| 24 | + public ByteList toByteListDirect(Encoding encoding) { |
| 25 | + byte[] buffer = new byte[totalLength]; |
| 26 | + int pos = 0; |
| 27 | + // We handle the current segment separately. |
| 28 | + for (int i = 0; i < currentSegmentIndex; i++) { |
| 29 | + byte[] segment = segments[i]; |
| 30 | + System.arraycopy(segment, 0, buffer, pos, segment.length); |
| 31 | + pos += segment.length; |
| 32 | + } |
| 33 | + System.arraycopy(currentSegment, 0, buffer, pos, currentSegmentLength); |
| 34 | + return new ByteList(buffer, 0, totalLength, encoding, false); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void write(int b) throws IOException { |
| 39 | + if (currentSegmentLength == currentSegment.length) { |
| 40 | + if (totalLength + 1 < 0) { |
| 41 | + throw new IOException("Total length exceeds maximum length of an array."); |
| 42 | + } |
| 43 | + currentSegmentIndex++; |
| 44 | + int capacity = currentSegment.length * 2; |
| 45 | + capacity = (capacity < 0) ? DEFAULT_CAPACITY : capacity; |
| 46 | + currentSegment = new byte[capacity]; |
| 47 | + currentSegmentLength = 0; |
| 48 | + segments[currentSegmentIndex] = currentSegment; |
| 49 | + } |
| 50 | + currentSegment[currentSegmentLength++] = (byte) b; |
| 51 | + totalLength++; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void write(byte[] bytes, int start, int length) throws IOException { |
| 56 | + int remaining = length; |
| 57 | + |
| 58 | + while (remaining > 0) { |
| 59 | + if (currentSegmentLength == currentSegment.length) { |
| 60 | + if (totalLength + remaining < 0) { |
| 61 | + throw new IOException("Total length exceeds maximum length of an array."); |
| 62 | + } |
| 63 | + currentSegmentIndex++; |
| 64 | + int capacity = currentSegment.length << 1; |
| 65 | + capacity = (capacity < 0) ? DEFAULT_CAPACITY : capacity; |
| 66 | + capacity = (capacity < remaining) ? remaining : capacity; |
| 67 | + currentSegment = new byte[capacity]; |
| 68 | + currentSegmentLength = 0; |
| 69 | + segments[currentSegmentIndex] = currentSegment; |
| 70 | + } |
| 71 | + int toWrite = Math.min(remaining, currentSegment.length - currentSegmentLength); |
| 72 | + System.arraycopy(bytes, start, currentSegment, currentSegmentLength, toWrite); |
| 73 | + currentSegmentLength += toWrite; |
| 74 | + start += toWrite; |
| 75 | + remaining -= toWrite; |
| 76 | + } |
| 77 | + totalLength += length; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void write(byte[] bytes) throws IOException { |
| 82 | + write(bytes, 0, bytes.length); |
| 83 | + } |
| 84 | +} |
0 commit comments