1111 ******************************************************************************/
1212package org .eclipse .lsp4j .jsonrpc .json ;
1313
14+ import java .io .ByteArrayOutputStream ;
1415import java .io .IOException ;
1516import java .io .OutputStream ;
17+ import java .nio .charset .Charset ;
1618import java .nio .charset .StandardCharsets ;
1719
1820import org .eclipse .lsp4j .jsonrpc .JsonRpcException ;
2426 */
2527public class StreamMessageConsumer implements MessageConsumer , MessageConstants {
2628
27- private final String encoding ;
29+ private final Charset encoding ;
2830 private final MessageJsonHandler jsonHandler ;
2931
3032 private final Object outputLock = new Object ();
3133
3234 private OutputStream output ;
3335
3436 public StreamMessageConsumer (MessageJsonHandler jsonHandler ) {
35- this (null , StandardCharsets .UTF_8 . name () , jsonHandler );
37+ this (null , StandardCharsets .UTF_8 , jsonHandler );
3638 }
3739
3840 public StreamMessageConsumer (OutputStream output , MessageJsonHandler jsonHandler ) {
39- this (output , StandardCharsets .UTF_8 . name () , jsonHandler );
41+ this (output , StandardCharsets .UTF_8 , jsonHandler );
4042 }
4143
4244 public StreamMessageConsumer (OutputStream output , String encoding , MessageJsonHandler jsonHandler ) {
45+ this (output , Charset .forName (encoding ), jsonHandler );
46+ }
47+
48+ public StreamMessageConsumer (OutputStream output , Charset encoding , MessageJsonHandler jsonHandler ) {
4349 this .output = output ;
4450 this .encoding = encoding ;
4551 this .jsonHandler = jsonHandler ;
@@ -56,16 +62,16 @@ public void setOutput(OutputStream output) {
5662 @ Override
5763 public void consume (Message message ) {
5864 try {
59- String content = jsonHandler . serialize ( message );
60- byte [] contentBytes = content . getBytes ( encoding );
61- int contentLength = contentBytes . length ;
65+ final var content = new ByteArrayOutputStream ( 256 );
66+ jsonHandler . serialize ( message , content , encoding );
67+ int contentLength = content . size () ;
6268
6369 String header = getHeader (contentLength );
6470 byte [] headerBytes = header .getBytes (StandardCharsets .US_ASCII );
6571
6672 synchronized (outputLock ) {
6773 output .write (headerBytes );
68- output . write ( contentBytes );
74+ content . writeTo ( output );
6975 output .flush ();
7076 }
7177 } catch (IOException exception ) {
@@ -80,9 +86,9 @@ public void consume(Message message) {
8086 protected String getHeader (int contentLength ) {
8187 final var headerBuilder = new StringBuilder ();
8288 appendHeader (headerBuilder , CONTENT_LENGTH_HEADER , contentLength ).append (CRLF );
83- if (!StandardCharsets .UTF_8 .name (). equals (encoding )) {
89+ if (!StandardCharsets .UTF_8 .equals (encoding )) {
8490 appendHeader (headerBuilder , CONTENT_TYPE_HEADER , JSON_MIME_TYPE );
85- headerBuilder .append ("; charset=" ).append (encoding ).append (CRLF );
91+ headerBuilder .append ("; charset=" ).append (encoding . name () ).append (CRLF );
8692 }
8793 headerBuilder .append (CRLF );
8894 return headerBuilder .toString ();
0 commit comments