1313
1414import edu .umd .cs .findbugs .annotations .NonNull ;
1515
16+ /**
17+ * Factory class for buffered {@link Output}.
18+ *
19+ * @author edgar
20+ * @since 4.0.0
21+ */
1622public interface OutputFactory {
1723
1824 /**
@@ -22,10 +28,14 @@ public interface OutputFactory {
2228 * @param factory Factory.
2329 * @return Thread local factory.
2430 */
25- static OutputFactory threadLocal (OutputFactory factory , int bufferSize ) {
31+ static OutputFactory threadLocal (OutputFactory factory ) {
2632 return new ForwardingOutputFactory (factory ) {
27- private final ThreadLocal <Output > threadLocal =
28- withInitial (() -> factory .newBufferedOutput (bufferSize ));
33+ private final ThreadLocal <Output > threadLocal = withInitial (factory ::newBufferedOutput );
34+
35+ @ Override
36+ public Output newBufferedOutput (boolean direct , int size ) {
37+ return threadLocal .get ().clear ();
38+ }
2939
3040 @ Override
3141 public Output newBufferedOutput (int size ) {
@@ -44,25 +54,62 @@ public Output newBufferedOutput() {
4454 *
4555 * @return Default output factory.
4656 */
47- static OutputFactory create () {
48- return new DefaultOutputFactory ();
57+ static OutputFactory create (boolean direct , int bufferSize ) {
58+ return new ByteBufferOutputFactory (direct , bufferSize );
59+ }
60+
61+ /**
62+ * Default output factory, backed by {@link ByteBuffer}.
63+ *
64+ * @return Default output factory.
65+ */
66+ static OutputFactory create (boolean direct ) {
67+ return new ByteBufferOutputFactory (direct , Output .BUFFER_SIZE );
4968 }
5069
70+ /**
71+ * Indicates whether this factory allocates direct buffers (i.e. non-heap, native memory).
72+ *
73+ * @return {@code true} if this factory allocates direct buffers; {@code false} otherwise
74+ */
75+ boolean isDirect ();
76+
77+ /**
78+ * Buffer of a default initial capacity. Default capacity is <code>1024</code> bytes.
79+ *
80+ * @return buffer of a default initial capacity.
81+ */
82+ int getInitialBufferSize ();
83+
84+ /**
85+ * Set default buffer initial capacity.
86+ *
87+ * @param initialBufferSize Default initial buffer capacity.
88+ * @return This buffer factory.
89+ */
90+ OutputFactory setInitialBufferSize (int initialBufferSize );
91+
5192 /**
5293 * Creates a new byte buffered output.
5394 *
95+ * @param direct True for direct buffers.
5496 * @param size Output size.
5597 * @return A byte buffered output.
5698 */
57- Output newBufferedOutput (int size );
99+ Output newBufferedOutput (boolean direct , int size );
58100
59101 /**
60102 * Creates a new byte buffered output with an initial size of {@link Output#BUFFER_SIZE}.
61103 *
104+ * @param size Output size.
62105 * @return A byte buffered output.
63106 */
107+ default Output newBufferedOutput (int size ) {
108+ return newBufferedOutput (isDirect (), size );
109+ }
110+
64111 default Output newBufferedOutput () {
65- return newBufferedOutput (Output .BUFFER_SIZE );
112+ return newBufferedOutput (isDirect (), Output .BUFFER_SIZE );
66113 }
67114
68115 /**
0 commit comments