66package io .jooby .internal .output ;
77
88import java .nio .ByteBuffer ;
9- import java .nio .charset .Charset ;
109import java .util .ArrayList ;
1110import java .util .List ;
1211
1312import edu .umd .cs .findbugs .annotations .NonNull ;
1413import io .jooby .Context ;
1514import io .jooby .SneakyThrows ;
16- import io .jooby .output .Output ;
15+ import io .jooby .output .BufferedOutput ;
1716
18- public class CompsiteByteBufferOutput implements Output {
17+ /**
18+ * Merge buffers into one.
19+ *
20+ * @author edgar
21+ * @since 4.0.0
22+ */
23+ public class CompositeOutput implements BufferedOutput {
1924 private final List <ByteBuffer > chunks = new ArrayList <>();
2025 private int size = 0 ;
2126
@@ -25,25 +30,25 @@ public int size() {
2530 }
2631
2732 @ Override
28- public Output write (byte b ) {
33+ public BufferedOutput write (byte b ) {
2934 addChunk (ByteBuffer .wrap (new byte [] {b }));
3035 return this ;
3136 }
3237
3338 @ Override
34- public Output write (byte [] source ) {
39+ public BufferedOutput write (byte [] source ) {
3540 addChunk (ByteBuffer .wrap (source ));
3641 return this ;
3742 }
3843
3944 @ Override
40- public Output write (byte [] source , int offset , int length ) {
45+ public BufferedOutput write (byte [] source , int offset , int length ) {
4146 addChunk (ByteBuffer .wrap (source , offset , length ));
4247 return this ;
4348 }
4449
4550 @ Override
46- public Output clear () {
51+ public BufferedOutput clear () {
4752 chunks .forEach (ByteBuffer ::clear );
4853 chunks .clear ();
4954 return this ;
@@ -57,18 +62,13 @@ public Output clear() {
5762 @ Override
5863 public ByteBuffer asByteBuffer () {
5964 var buf = ByteBuffer .allocate (size );
60- chunks .forEach (buf ::put );
65+ for (ByteBuffer chunk : chunks ) {
66+ buf .put (chunk .duplicate ());
67+ }
6168 buf .flip ();
6269 return buf ;
6370 }
6471
65- @ Override
66- public String asString (@ NonNull Charset charset ) {
67- var sb = new StringBuilder ();
68- chunks .forEach (bytes -> sb .append (charset .decode (bytes )));
69- return sb .toString ();
70- }
71-
7272 @ Override
7373 public void transferTo (@ NonNull SneakyThrows .Consumer <ByteBuffer > consumer ) {
7474 chunks .forEach (consumer );
0 commit comments