|
| 1 | +/** |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.rocker; |
| 7 | + |
| 8 | +import java.nio.ByteBuffer; |
| 9 | +import java.nio.charset.Charset; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.util.Arrays; |
| 12 | + |
| 13 | +import com.fizzed.rocker.ContentType; |
| 14 | +import com.fizzed.rocker.RockerOutput; |
| 15 | +import com.fizzed.rocker.RockerOutputFactory; |
| 16 | + |
| 17 | +/** |
| 18 | + * Rocker output that uses a byte array to render the output. |
| 19 | + * |
| 20 | + * @author edgar |
| 21 | + */ |
| 22 | +public class ByteBufferOutput implements RockerOutput<ByteBufferOutput> { |
| 23 | + |
| 24 | + /** Default buffer size: <code>4k</code>. */ |
| 25 | + public static final int BUFFER_SIZE = 4096; |
| 26 | + |
| 27 | + /** |
| 28 | + * The maximum size of array to allocate. |
| 29 | + * Some VMs reserve some header words in an array. |
| 30 | + * Attempts to allocate larger arrays may result in |
| 31 | + * OutOfMemoryError: Requested array size exceeds VM limit |
| 32 | + */ |
| 33 | + private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
| 34 | + |
| 35 | + private final ContentType contentType; |
| 36 | + |
| 37 | + /** |
| 38 | + * The buffer where data is stored. |
| 39 | + */ |
| 40 | + protected byte[] buf; |
| 41 | + |
| 42 | + /** |
| 43 | + * The number of valid bytes in the buffer. |
| 44 | + */ |
| 45 | + protected int count; |
| 46 | + |
| 47 | + ByteBufferOutput(ContentType contentType, int bufferSize) { |
| 48 | + this.buf = new byte[bufferSize]; |
| 49 | + this.contentType = contentType; |
| 50 | + } |
| 51 | + |
| 52 | + void reset() { |
| 53 | + count = 0; |
| 54 | + } |
| 55 | + |
| 56 | + @Override public ContentType getContentType() { |
| 57 | + return contentType; |
| 58 | + } |
| 59 | + |
| 60 | + @Override public Charset getCharset() { |
| 61 | + return StandardCharsets.UTF_8; |
| 62 | + } |
| 63 | + |
| 64 | + @Override public ByteBufferOutput w(String string) { |
| 65 | + return w(string.getBytes(StandardCharsets.UTF_8)); |
| 66 | + } |
| 67 | + |
| 68 | + @Override public ByteBufferOutput w(byte[] bytes) { |
| 69 | + int len = bytes.length; |
| 70 | + ensureCapacity(count + len); |
| 71 | + System.arraycopy(bytes, 0, buf, count, len); |
| 72 | + count += len; |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + @Override public int getByteLength() { |
| 77 | + return count; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get a view of the byte buffer. |
| 82 | + * |
| 83 | + * @return Byte buffer. |
| 84 | + */ |
| 85 | + public ByteBuffer toBuffer() { |
| 86 | + return ByteBuffer.wrap(buf, 0, count); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Copy internal byte array into a new array. |
| 91 | + * |
| 92 | + * @return Byte array. |
| 93 | + */ |
| 94 | + public byte[] toByteArray() { |
| 95 | + byte[] array = new byte[count]; |
| 96 | + System.arraycopy(buf, 0, array, 0, count); |
| 97 | + return array; |
| 98 | + } |
| 99 | + |
| 100 | + private void ensureCapacity(int minCapacity) { |
| 101 | + // overflow-conscious code |
| 102 | + if (minCapacity - buf.length > 0) { |
| 103 | + grow(minCapacity); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Increases the capacity to ensure that it can hold at least the |
| 109 | + * number of elements specified by the minimum capacity argument. |
| 110 | + * |
| 111 | + * @param minCapacity the desired minimum capacity |
| 112 | + */ |
| 113 | + private void grow(int minCapacity) { |
| 114 | + // overflow-conscious code |
| 115 | + int oldCapacity = buf.length; |
| 116 | + int newCapacity = oldCapacity << 1; |
| 117 | + if (newCapacity - minCapacity < 0) { |
| 118 | + newCapacity = minCapacity; |
| 119 | + } |
| 120 | + if (newCapacity - MAX_ARRAY_SIZE > 0) { |
| 121 | + newCapacity = hugeCapacity(minCapacity); |
| 122 | + } |
| 123 | + buf = Arrays.copyOf(buf, newCapacity); |
| 124 | + } |
| 125 | + |
| 126 | + private static int hugeCapacity(int minCapacity) { |
| 127 | + if (minCapacity < 0) { |
| 128 | + throw new OutOfMemoryError(); |
| 129 | + } |
| 130 | + return (minCapacity > MAX_ARRAY_SIZE) |
| 131 | + ? Integer.MAX_VALUE |
| 132 | + : MAX_ARRAY_SIZE; |
| 133 | + } |
| 134 | + |
| 135 | + static RockerOutputFactory<ByteBufferOutput> factory(int bufferSize) { |
| 136 | + return (contentType, charsetName) -> new ByteBufferOutput(contentType, bufferSize); |
| 137 | + } |
| 138 | + |
| 139 | + static RockerOutputFactory<ByteBufferOutput> reuse( |
| 140 | + RockerOutputFactory<ByteBufferOutput> factory) { |
| 141 | + return new RockerOutputFactory<ByteBufferOutput>() { |
| 142 | + private final ThreadLocal<ByteBufferOutput> thread = new ThreadLocal<>(); |
| 143 | + |
| 144 | + @Override public ByteBufferOutput create(ContentType contentType, String charsetName) { |
| 145 | + ByteBufferOutput output = thread.get(); |
| 146 | + if (output == null) { |
| 147 | + output = factory.create(contentType, charsetName); |
| 148 | + thread.set(output); |
| 149 | + } |
| 150 | + output.reset(); |
| 151 | + return output; |
| 152 | + } |
| 153 | + }; |
| 154 | + } |
| 155 | +} |
0 commit comments