Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38952
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2019-05-09 23:16 -0700 |
| Message-ID | <8beb1189-8953-4072-9b4b-0d54cf64eaa5@googlegroups.com> (permalink) |
| Subject | readLine() and newline problem |
| From | mike <mikaelpetterson@hotmail.com> |
Hi,
We have the following code for reading a NETCONF "hello" message from a NETCONF Device.
public class MessageHandler {
public StringBuilder readLines(InputStream is) {
final StringBuilder buffer = new StringBuilder();
final String delimiter = "]]>]]>";
String tmp;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
do {
tmp = reader.readLine();
if (tmp != null) {
buffer.append(tmp);
}
} while (tmp != null && !tmp.endsWith(delimiter));
} catch (IOException e) {
LOGGER.info("Failed to read <rpc-reply> message ",
e);
}
return buffer;
}
}
We get the following string from the device ( just an example):
<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities>
<capability>
urn:ietf:params:netconf:base:1.1
</capability>
<capability>
urn:ietf:params:ns:netconf:capability:startup:1.0
</capability>
</capabilities>
<session-id>4</session-id>
</hello>
]]>]]>
The problem here is that some devices add a newline after "]]>]]>" which is the message separator. And of course some devices don't since it is not required in the RFC6242.
So when there is no newline from device then the above method will hang. Then I thought it would be easy to create a unit test that recreates the problem. One with a hello message with message separator and newline and one with only message separator. But problem is that I get no exception in the test without the newline. Any ideas what I am missing or how I can re-create the issue?
br,
//mike
public class MessageHandlerTest {
private MessageHandler messageHandler;
@BeforeClass
public void setup() {
messageHandler = new MessageHandler();
}
@Test
public void testHelloServerWithOutNewLine() throws Exception {
InputStream is = getHelloWithoutNewline();
StringBuilder actual = messageHandler.readLines(is);
Assert.assertTrue(actual.length() > 0);
}
@Test
public void testHelloServerWithNewline() throws Exception {
InputStream is = getHelloWithNewline();
StringBuilder actual = messageHandler.readLines(is);
Assert.assertTrue(actual.length() > 0);
}
private InputStream getHelloWithoutNewline() {
final String HELLO_REPLY = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
" <hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" +
" <capabilities>" +
" <capability>" +
" urn:ietf:params:netconf:base:1.1" +
" </capability>" +
" <capability>" +
" urn:ietf:params:ns:netconf:capability:startup:1.0" +
" </capability>" +
" </capabilities>" +
" <session-id>4</session-id>" +
" </hello>" +
" ]]>]]>";
return new ByteArrayInputStream(HELLO_REPLY.getBytes());
}
private InputStream getHelloWithNewline() {
final String HELLO_REPLY = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
" <hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
" <capabilities>\n" +
" <capability>\n" +
" urn:ietf:params:netconf:base:1.1\n" +
" </capability>\n" +
" <capability>\n" +
" urn:ietf:params:ns:netconf:capability:startup:1.0\n" +
" </capability>\n" +
" </capabilities>\n" +
" <session-id>4</session-id>\n" +
" </hello>\n" +
" ]]>]]>\n";
return new ByteArrayInputStream(HELLO_REPLY.getBytes());
}
}
Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread
readLine() and newline problem mike <mikaelpetterson@hotmail.com> - 2019-05-09 23:16 -0700
Re: readLine() and newline problem Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-05-10 09:02 +0200
Re: readLine() and newline problem mike <mikaelpetterson@hotmail.com> - 2019-05-10 01:29 -0700
Re: readLine() and newline problem Martin Gregorie <martin@mydomain.invalid> - 2019-05-10 10:22 +0000
Re: readLine() and newline problem Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-05-10 18:50 +0200
csiph-web