Skip to content

Commit 47718a7

Browse files
sebthommickaelistria
authored andcommitted
fix: broken FilterInputStream in StreamConnectionProvider#forwardCopyTo
1 parent 6cc07bd commit 47718a7

2 files changed

Lines changed: 98 additions & 13 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Vegard IT GmbH and others.
3+
* This program and the accompanying materials are made
4+
* available under the terms of the Eclipse Public License 2.0
5+
* which is available at https://www.eclipse.org/legal/epl-2.0/
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* Sebastian Thomschke (Vegard IT GmbH) - initial implementation
11+
*******************************************************************************/
12+
package org.eclipse.lsp4e.test.server;
13+
14+
import static java.nio.charset.StandardCharsets.UTF_8;
15+
import static org.junit.Assert.*;
16+
17+
import java.io.ByteArrayInputStream;
18+
import java.io.ByteArrayOutputStream;
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.io.OutputStream;
22+
import java.net.URI;
23+
24+
import org.eclipse.lsp4e.server.StreamConnectionProvider;
25+
import org.eclipse.lsp4j.jsonrpc.messages.Message;
26+
import org.eclipse.lsp4j.services.LanguageServer;
27+
import org.junit.Test;
28+
29+
public class StreamConnectionProviderTest {
30+
31+
private static StreamConnectionProvider newProvider() {
32+
return new StreamConnectionProvider() {
33+
@Override
34+
public void start() throws IOException {
35+
}
36+
37+
@Override
38+
public InputStream getInputStream() {
39+
return null;
40+
}
41+
42+
@Override
43+
public OutputStream getOutputStream() {
44+
return null;
45+
}
46+
47+
@Override
48+
public InputStream getErrorStream() {
49+
return null;
50+
}
51+
52+
@Override
53+
public void stop() {
54+
}
55+
56+
@Override
57+
public void handleMessage(Message message, LanguageServer languageServer, URI rootURI) {
58+
}
59+
};
60+
}
61+
62+
@Test
63+
public void test_forwardCopy_singleByteRead_writesToProvidedOutput() throws Exception {
64+
final var input = new ByteArrayInputStream("ABC".getBytes(UTF_8));
65+
final var sink = new ByteArrayOutputStream();
66+
67+
try (InputStream forwarding = newProvider().forwardCopyTo(input, sink)) {
68+
while ((forwarding.read()) != -1) {
69+
// read one byte at a time to exercise single-byte read path
70+
}
71+
}
72+
assertEquals("expected input to be forwarded to provided OutputStream", "ABC", sink.toString(UTF_8));
73+
}
74+
75+
@Test
76+
public void test_forwardCopy_readArray_onEOF_returnsMinusOne_noException() throws Exception {
77+
final var emptyInput = new ByteArrayInputStream(new byte[0]);
78+
final var sink = new ByteArrayOutputStream();
79+
80+
try (final var forwarding = newProvider().forwardCopyTo(emptyInput, sink)) {
81+
final var buf = new byte[8];
82+
int n = forwarding.read(buf); // should be -1 and not throw
83+
assertEquals("expected EOF (-1) on empty stream", -1, n);
84+
}
85+
assertArrayEquals(new byte[0], sink.toByteArray());
86+
}
87+
}

org.eclipse.lsp4e/src/org/eclipse/lsp4e/server/StreamConnectionProvider.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,23 @@ public interface StreamConnectionProvider {
7575
@Override
7676
public int read() throws IOException {
7777
int res = super.read();
78-
System.err.print((char) res);
78+
if (res == -1) {
79+
return -1;
80+
}
81+
output.write(res);
7982
return res;
8083
}
8184

8285
@Override
8386
public int read(byte[] b, int off, int len) throws IOException {
8487
int bytes = super.read(b, off, len);
85-
final var payload = new byte[bytes];
86-
System.arraycopy(b, off, payload, 0, bytes);
87-
output.write(payload, 0, payload.length);
88-
return bytes;
89-
}
90-
91-
@Override
92-
public int read(byte[] b) throws IOException {
93-
int bytes = super.read(b);
94-
final var payload = new byte[bytes];
95-
System.arraycopy(b, 0, payload, 0, bytes);
96-
output.write(payload, 0, payload.length);
88+
if (bytes == -1) {
89+
return -1;
90+
}
91+
if (bytes == 0) {
92+
return 0;
93+
}
94+
output.write(b, off, bytes);
9795
return bytes;
9896
}
9997
};

0 commit comments

Comments
 (0)