77
88import java .nio .ByteBuffer ;
99import java .nio .charset .Charset ;
10+ import java .util .function .Supplier ;
1011
1112import edu .umd .cs .findbugs .annotations .NonNull ;
1213import io .jooby .Context ;
1314import io .jooby .SneakyThrows ;
1415import io .jooby .output .Output ;
1516
16- public class ByteBufferWrappedOutput implements Output {
17+ public class ByteBufferOutputStatic implements Output {
1718
18- private final ByteBuffer buffer ;
19+ private final int size ;
20+ private final Supplier <ByteBuffer > provider ;
1921
20- public ByteBufferWrappedOutput (ByteBuffer buffer ) {
21- this .buffer = buffer ;
22+ public ByteBufferOutputStatic (int size , Supplier <ByteBuffer > provider ) {
23+ this .size = size ;
24+ this .provider = provider ;
25+ }
26+
27+ public ByteBufferOutputStatic (ByteBuffer byteBuffer ) {
28+ this (byteBuffer .remaining (), () -> byteBuffer );
2229 }
2330
2431 @ Override
@@ -38,23 +45,22 @@ public Output write(byte[] source, int offset, int length) {
3845
3946 @ Override
4047 public Output clear () {
41- buffer .clear ();
4248 return this ;
4349 }
4450
4551 @ Override
4652 public int size () {
47- return buffer . remaining () ;
53+ return size ;
4854 }
4955
5056 @ Override
5157 public void transferTo (@ NonNull SneakyThrows .Consumer <ByteBuffer > consumer ) {
52- consumer .accept (buffer );
58+ consumer .accept (asByteBuffer () );
5359 }
5460
5561 @ Override
5662 public ByteBuffer asByteBuffer () {
57- return buffer . duplicate ();
63+ return provider . get ();
5864 }
5965
6066 @ Override
@@ -69,6 +75,6 @@ public String toString() {
6975
7076 @ Override
7177 public void send (Context ctx ) {
72- ctx .send (buffer );
78+ ctx .send (asByteBuffer () );
7379 }
7480}
0 commit comments