|
1 | | -package api.json; |
2 | | - |
3 | | -import org.apache.logging.log4j.LogManager; |
4 | | -import org.apache.logging.log4j.Logger; |
5 | | -import org.eclipse.lsp4j.jsonrpc.JsonRpcException; |
6 | | -import org.eclipse.lsp4j.jsonrpc.MessageConsumer; |
7 | | -import org.eclipse.lsp4j.jsonrpc.json.MessageConstants; |
8 | | -import org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler; |
9 | | -import org.eclipse.lsp4j.jsonrpc.messages.Message; |
10 | | - |
11 | | -import java.io.IOException; |
12 | | -import java.io.OutputStream; |
13 | | -import java.nio.charset.StandardCharsets; |
14 | | - |
15 | | - |
16 | | -/** |
17 | | - * A message consumer that serializes messages to JSON and sends them to an output stream. |
18 | | - * |
19 | | - * @since 0.0.3 |
20 | | - */ |
21 | | - |
22 | | -public class GoblintSocketMessageConsumer implements MessageConsumer, MessageConstants { |
23 | | - |
24 | | - private final String encoding; |
25 | | - private final MessageJsonHandler jsonHandler; |
26 | | - private final Object outputLock = new Object(); |
27 | | - private final OutputStream output; |
28 | | - |
29 | | - private final Logger log = LogManager.getLogger(GoblintSocketMessageConsumer.class); |
30 | | - |
31 | | - public GoblintSocketMessageConsumer(OutputStream output, MessageJsonHandler jsonHandler) { |
32 | | - this(output, StandardCharsets.UTF_8.name(), jsonHandler); |
33 | | - } |
34 | | - |
35 | | - public GoblintSocketMessageConsumer(OutputStream output, String encoding, MessageJsonHandler jsonHandler) { |
36 | | - this.output = output; |
37 | | - this.encoding = encoding; |
38 | | - this.jsonHandler = jsonHandler; |
39 | | - } |
40 | | - |
41 | | - @Override |
42 | | - public void consume(Message message) { |
43 | | - try { |
44 | | - String content = jsonHandler.serialize(message) + "\n"; |
45 | | - byte[] contentBytes = content.getBytes(encoding); |
46 | | - synchronized (outputLock) { |
47 | | - output.write(contentBytes); |
48 | | - output.flush(); |
49 | | - } |
50 | | - log.debug("WRITTEN: {}", content); |
51 | | - } catch (IOException exception) { |
52 | | - throw new JsonRpcException(exception); |
53 | | - } |
54 | | - } |
55 | | - |
56 | | -} |
| 1 | +package api.jsonrpc; |
| 2 | + |
| 3 | +import org.apache.logging.log4j.LogManager; |
| 4 | +import org.apache.logging.log4j.Logger; |
| 5 | +import org.eclipse.lsp4j.jsonrpc.JsonRpcException; |
| 6 | +import org.eclipse.lsp4j.jsonrpc.MessageConsumer; |
| 7 | +import org.eclipse.lsp4j.jsonrpc.json.MessageConstants; |
| 8 | +import org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler; |
| 9 | +import org.eclipse.lsp4j.jsonrpc.messages.Message; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.OutputStream; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | + |
| 15 | + |
| 16 | +/** |
| 17 | + * A message consumer that serializes messages to JSON and sends them to an output stream. |
| 18 | + * |
| 19 | + * @since 0.0.3 |
| 20 | + */ |
| 21 | + |
| 22 | +public class GoblintSocketMessageConsumer implements MessageConsumer, MessageConstants { |
| 23 | + |
| 24 | + private final String encoding; |
| 25 | + private final MessageJsonHandler jsonHandler; |
| 26 | + private final Object outputLock = new Object(); |
| 27 | + private final OutputStream output; |
| 28 | + |
| 29 | + private final Logger log = LogManager.getLogger(GoblintSocketMessageConsumer.class); |
| 30 | + |
| 31 | + public GoblintSocketMessageConsumer(OutputStream output, MessageJsonHandler jsonHandler) { |
| 32 | + this(output, StandardCharsets.UTF_8.name(), jsonHandler); |
| 33 | + } |
| 34 | + |
| 35 | + public GoblintSocketMessageConsumer(OutputStream output, String encoding, MessageJsonHandler jsonHandler) { |
| 36 | + this.output = output; |
| 37 | + this.encoding = encoding; |
| 38 | + this.jsonHandler = jsonHandler; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void consume(Message message) { |
| 43 | + try { |
| 44 | + String content = jsonHandler.serialize(message) + "\n"; |
| 45 | + byte[] contentBytes = content.getBytes(encoding); |
| 46 | + synchronized (outputLock) { |
| 47 | + output.write(contentBytes); |
| 48 | + output.flush(); |
| 49 | + } |
| 50 | + log.debug("WRITTEN: {}", content); |
| 51 | + } catch (IOException exception) { |
| 52 | + throw new JsonRpcException(exception); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | +} |
0 commit comments