Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #39775 > unrolled thread

Handle delimiter in read message

Started bymike <mikaelpetterson@hotmail.com>
First post2022-05-22 23:09 -0700
Last post2022-06-07 23:19 +0300
Articles 8 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Handle delimiter in read message mike <mikaelpetterson@hotmail.com> - 2022-05-22 23:09 -0700
    Re: Handle delimiter in read message mike <mikaelpetterson@hotmail.com> - 2022-05-23 02:31 -0700
      Re: Handle delimiter in read message mike <mikaelpetterson@hotmail.com> - 2022-05-23 02:59 -0700
        Re: Handle delimiter in read message mike <mikaelpetterson@hotmail.com> - 2022-05-23 04:34 -0700
          Re: Handle delimiter in read message "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2022-05-23 06:31 -0700
            Re: Handle delimiter in read message Eric Sosman <esosman@comcast-dot-net.invalid> - 2022-05-23 10:33 -0400
          Re: Handle delimiter in read message Arne Vajhøj <arne@vajhoej.dk> - 2022-05-23 11:21 -0400
      Re: Handle delimiter in read message Stanimir Stamenkov <s7an10@netscape.net> - 2022-06-07 23:19 +0300

#39775 — Handle delimiter in read message

Frommike <mikaelpetterson@hotmail.com>
Date2022-05-22 23:09 -0700
SubjectHandle delimiter in read message
Message-ID<43d5ea21-6264-4b24-ab0c-32c27cdc09c2n@googlegroups.com>
Hi,

We receive a message called "chunked message" in NETCONF.
Example:

 final String MSG_REPLY = "#877\r\n"
                +
                "<rpc-reply message-id=\"1\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><data xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><netconf-server xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-server\"><listen><endpoint><name>default-ssh</name><ssh><tcp-server-parameters><local-address>0.0.0.0</local-address><keepalives><idle-time>1</idle-time><max-probes>10</max-probes><probe-interval>5</probe-interval></keepalives></tcp-server-parameters><ssh-server-parameters><server-identity><host-key><name>default-key</name><public-key><keystore-reference>genkey</keystore-reference></public-key></host-key></server-identity><client-authentication><supported-authentication-methods><publickey/><passsword/><other>interactive</other></supported-authentication-methods><users/></client-authentication></ssh-server-parameters></ssh></endpoint></listen></netconf-server></data></rpc-reply>\r\n"
                +
                "##\r\n";

I tried:

String newline = System.getProperty("line.separator");//make sure I get newline for platform.
 return sb.toString().contains(newline + "##" + newline);

But it fails. Is it possible to check if the string ends with a regex pattern?

//mike

[toc] | [next] | [standalone]


#39776

Frommike <mikaelpetterson@hotmail.com>
Date2022-05-23 02:31 -0700
Message-ID<08d72ee6-dd98-46ab-a0b8-ae683996b3d8n@googlegroups.com>
In reply to#39775
måndag 23 maj 2022 kl. 08:09:48 UTC+2 skrev mike:
> Hi, 
> 
> We receive a message called "chunked message" in NETCONF. 
> Example: 
> 
> final String MSG_REPLY = "#877\r\n" 
> + 
> "<rpc-reply message-id=\"1\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><data xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><netconf-server xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-server\"><listen><endpoint><name>default-ssh</name><ssh><tcp-server-parameters><local-address>0.0.0.0</local-address><keepalives><idle-time>1</idle-time><max-probes>10</max-probes><probe-interval>5</probe-interval></keepalives></tcp-server-parameters><ssh-server-parameters><server-identity><host-key><name>default-key</name><public-key><keystore-reference>genkey</keystore-reference></public-key></host-key></server-identity><client-authentication><supported-authentication-methods><publickey/><passsword/><other>interactive</other></supported-authentication-methods><users/></client-authentication></ssh-server-parameters></ssh></endpoint></listen></netconf-server></data></rpc-reply>\r\n" 
> + 
> "##\r\n"; 
> 
> I tried: 
> 
> String newline = System.getProperty("line.separator");//make sure I get newline for platform. 
> return sb.toString().contains(newline + "##" + newline); 
> 
> But it fails. Is it possible to check if the string ends with a regex pattern? 
> 
> //mike

I even checked the ASCII in my buffer.

ASCII numbers is:

10 LF 
35 #
35 #
10 LF

I changed my regexp:

sb.toString().matches("(?s).*\\r\\n|\\n##\\r\\n|\\n$")

but it still evaluates to false. I suspect it is DOT ALL that gives me the headache.

//mike

[toc] | [prev] | [next] | [standalone]


#39777

Frommike <mikaelpetterson@hotmail.com>
Date2022-05-23 02:59 -0700
Message-ID<71e3ac9a-032a-42d5-b509-884e36908b81n@googlegroups.com>
In reply to#39776
måndag 23 maj 2022 kl. 11:32:07 UTC+2 skrev mike:
> måndag 23 maj 2022 kl. 08:09:48 UTC+2 skrev mike: 
> > Hi, 
> > 
> > We receive a message called "chunked message" in NETCONF. 
> > Example: 
> > 
> > final String MSG_REPLY = "#877\r\n" 
> > + 
> > "<rpc-reply message-id=\"1\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><data xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><netconf-server xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-server\"><listen><endpoint><name>default-ssh</name><ssh><tcp-server-parameters><local-address>0.0.0.0</local-address><keepalives><idle-time>1</idle-time><max-probes>10</max-probes><probe-interval>5</probe-interval></keepalives></tcp-server-parameters><ssh-server-parameters><server-identity><host-key><name>default-key</name><public-key><keystore-reference>genkey</keystore-reference></public-key></host-key></server-identity><client-authentication><supported-authentication-methods><publickey/><passsword/><other>interactive</other></supported-authentication-methods><users/></client-authentication></ssh-server-parameters></ssh></endpoint></listen></netconf-server></data></rpc-reply>\r\n" 
> > + 
> > "##\r\n"; 
> > 
> > I tried: 
> > 
> > String newline = System.getProperty("line.separator");//make sure I get newline for platform. 
> > return sb.toString().contains(newline + "##" + newline); 
> > 
> > But it fails. Is it possible to check if the string ends with a regex pattern? 
> > 
> > //mike
> I even checked the ASCII in my buffer. 
> 
> ASCII numbers is: 
> 
> 10 LF 
> 35 # 
> 35 # 
> 10 LF 
> 
> I changed my regexp: 
> 
> sb.toString().matches("(?s).*\\r\\n|\\n##\\r\\n|\\n$") 
> 
> but it still evaluates to false. I suspect it is DOT ALL that gives me the headache. 
> 
> //mike

Even tried this to make sure I have  only four last characters of my string.

String endofMsg = sb.toString().substring(sb.toString().length() - 4);
        	
 return endofMsg.matches("^\\r\\n|[\\n]##\\r\\n|[\\n]$");

[toc] | [prev] | [next] | [standalone]


#39778

Frommike <mikaelpetterson@hotmail.com>
Date2022-05-23 04:34 -0700
Message-ID<2353f4c2-b2a5-4036-9a7f-8be371b9c446n@googlegroups.com>
In reply to#39777
måndag 23 maj 2022 kl. 12:00:06 UTC+2 skrev mike:
> måndag 23 maj 2022 kl. 11:32:07 UTC+2 skrev mike: 
> > måndag 23 maj 2022 kl. 08:09:48 UTC+2 skrev mike: 
> > > Hi, 
> > > 
> > > We receive a message called "chunked message" in NETCONF. 
> > > Example: 
> > > 
> > > final String MSG_REPLY = "#877\r\n" 
> > > + 
> > > "<rpc-reply message-id=\"1\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><data xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><netconf-server xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-server\"><listen><endpoint><name>default-ssh</name><ssh><tcp-server-parameters><local-address>0.0.0.0</local-address><keepalives><idle-time>1</idle-time><max-probes>10</max-probes><probe-interval>5</probe-interval></keepalives></tcp-server-parameters><ssh-server-parameters><server-identity><host-key><name>default-key</name><public-key><keystore-reference>genkey</keystore-reference></public-key></host-key></server-identity><client-authentication><supported-authentication-methods><publickey/><passsword/><other>interactive</other></supported-authentication-methods><users/></client-authentication></ssh-server-parameters></ssh></endpoint></listen></netconf-server></data></rpc-reply>\r\n" 
> > > + 
> > > "##\r\n"; 
> > > 
> > > I tried: 
> > > 
> > > String newline = System.getProperty("line.separator");//make sure I get newline for platform. 
> > > return sb.toString().contains(newline + "##" + newline); 
> > > 
> > > But it fails. Is it possible to check if the string ends with a regex pattern? 
> > > 
> > > //mike 
> > I even checked the ASCII in my buffer. 
> > 
> > ASCII numbers is: 
> > 
> > 10 LF 
> > 35 # 
> > 35 # 
> > 10 LF 
> > 
> > I changed my regexp: 
> > 
> > sb.toString().matches("(?s).*\\r\\n|\\n##\\r\\n|\\n$") 
> > 
> > but it still evaluates to false. I suspect it is DOT ALL that gives me the headache. 
> > 
> > //mike
> Even tried this to make sure I have only four last characters of my string. 
> 
> String endofMsg = sb.toString().substring(sb.toString().length() - 4); 
> 
> return endofMsg.matches("^\\r\\n|[\\n]##\\r\\n|[\\n]$");

I found out I can do:

String endofMsg = sb.toString().substring(sb.toString().length() - 6);
            return endofMsg.matches("\\r\\n##\\r\\n|.*\\n##\\n$");

I had to use the last 6 characters since newline is different for each platform.

//mike

[toc] | [prev] | [next] | [standalone]


#39779

From"e.d.pro...@gmail.com" <e.d.programmer@gmail.com>
Date2022-05-23 06:31 -0700
Message-ID<4ceef2dc-8924-4ab2-b400-73066308a83cn@googlegroups.com>
In reply to#39778
> I found out I can do: 
> 
> String endofMsg = sb.toString().substring(sb.toString().length() - 6); 
> return endofMsg.matches("\\r\\n##\\r\\n|.*\\n##\\n$"); 
> 
> I had to use the last 6 characters since newline is different for each platform. 
> 
> //mike

I'm not clear what is the problem you're trying to solve, but System.getProperty("line.separator") is system dependent, so if you're running that on one system to get a value to use on a different system, your value will be wrong if the systems are different. *nix uses \n for linefeed, Windows uses \r\n , so you're checking for the presence of \n if you want to check for a linefeed.

[toc] | [prev] | [next] | [standalone]


#39780

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2022-05-23 10:33 -0400
Message-ID<t6g608$hha$1@dont-email.me>
In reply to#39779
On 5/23/2022 9:31 AM, e.d.pro...@gmail.com wrote:
>> I found out I can do:
>>
>> String endofMsg = sb.toString().substring(sb.toString().length() - 6);
>> return endofMsg.matches("\\r\\n##\\r\\n|.*\\n##\\n$");
>>
>> I had to use the last 6 characters since newline is different for each platform.
>>
>> //mike
> 
> I'm not clear what is the problem you're trying to solve, but System.getProperty("line.separator") is system dependent, so if you're running that on one system to get a value to use on a different system, your value will be wrong if the systems are different. *nix uses \n for linefeed, Windows uses \r\n , so you're checking for the presence of \n if you want to check for a linefeed.

     Also (if I recall correctly), various Internet RFC's require CR-LF
line terminators, regardless of the system sending the lines.  I don't
know whether mike's messages follow such a protocol, but many do.

     His best bet may be to recognize all of CR, LF, and CR-LF as line
terminators, and maybe even LF-CR as well.

-- 
esosman@comcast-dot-net.invalid
Look on my code, ye Hackers, and guffaw!

-- 
This email has been checked for viruses by AVG.
https://www.avg.com

[toc] | [prev] | [next] | [standalone]


#39781

FromArne Vajhøj <arne@vajhoej.dk>
Date2022-05-23 11:21 -0400
Message-ID<628ba668$0$693$14726298@news.sunsite.dk>
In reply to#39778
On 5/23/2022 7:34 AM, mike wrote:
> måndag 23 maj 2022 kl. 12:00:06 UTC+2 skrev mike:
>> måndag 23 maj 2022 kl. 11:32:07 UTC+2 skrev mike:
>>> måndag 23 maj 2022 kl. 08:09:48 UTC+2 skrev mike:
>>>> We receive a message called "chunked message" in NETCONF.
>>>> Example:
>>>>
>>>> final String MSG_REPLY = "#877\r\n"
>>>> +
>>>> "<rpc-reply message-id=\"1\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><data xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><netconf-server xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-server\"><listen><endpoint><name>default-ssh</name><ssh><tcp-server-parameters><local-address>0.0.0.0</local-address><keepalives><idle-time>1</idle-time><max-probes>10</max-probes><probe-interval>5</probe-interval></keepalives></tcp-server-parameters><ssh-server-parameters><server-identity><host-key><name>default-key</name><public-key><keystore-reference>genkey</keystore-reference></public-key></host-key></server-identity><client-authentication><supported-authentication-methods><publickey/><passsword/><other>interactive</other></supported-authentication-methods><users/></client-authentication></ssh-server-parameters></ssh></endpoint></listen></netconf-server></data></rpc-reply>\r\n"
>>>> +
>>>> "##\r\n";
>>>>
>>>> I tried:
>>>>
>>>> String newline = System.getProperty("line.separator");//make sure I get newline for platform.
>>>> return sb.toString().contains(newline + "##" + newline);
>>>>
>>>> But it fails. Is it possible to check if the string ends with a regex pattern?
>>>>
>>>> //mike
>>> I even checked the ASCII in my buffer.
>>>
>>> ASCII numbers is:
>>>
>>> 10 LF
>>> 35 #
>>> 35 #
>>> 10 LF
>>>
>>> I changed my regexp:
>>>
>>> sb.toString().matches("(?s).*\\r\\n|\\n##\\r\\n|\\n$")
>>>
>>> but it still evaluates to false. I suspect it is DOT ALL that gives me the headache.
>>>
>> Even tried this to make sure I have only four last characters of my string.
>>
>> String endofMsg = sb.toString().substring(sb.toString().length() - 4);
>>
>> return endofMsg.matches("^\\r\\n|[\\n]##\\r\\n|[\\n]$");
> 
> I found out I can do:
> 
> String endofMsg = sb.toString().substring(sb.toString().length() - 6);
>              return endofMsg.matches("\\r\\n##\\r\\n|.*\\n##\\n$");
> 
> I had to use the last 6 characters since newline is different for each platform.

This seems to work:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EndTest {
	private static final Pattern re = 
Pattern.compile("^#\\d+\\v+(.*)##\\v+$", Pattern.MULTILINE + 
Pattern.DOTALL);
	public static void test(String s) {
		System.out.println(s);
		Matcher m = re.matcher(s);
		if(m.find()) {
			System.out.println("Match: " + m.group(1));
		} else {
			System.out.println("No match");
		}
	}
	public static void main(String[] args) {
		test("ABC");
		String msg = "#877\r\n"
				+
				"<rpc-reply message-id=\"1\" 
xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><data 
xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><netconf-server 
xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-server\"><listen><endpoint><name>default-ssh</name><ssh><tcp-server-parameters><local-address>0.0.0.0</local-address><keepalives><idle-time>1</idle-time><max-probes>10</max-probes><probe-interval>5</probe-interval></keepalives></tcp-server-parameters><ssh-server-parameters><server-identity><host-key><name>default-key</name><public-key><keystore-reference>genkey</keystore-reference></public-key></host-key></server-identity><client-authentication><supported-authentication-methods><publickey/><passsword/><other>interactive</other></supported-authentication-methods><users/></client-authentication></ssh-server-parameters></ssh></endpoint></listen></netconf-server></data></rpc-reply>\r\n" 

				+
				"##\r\n";
		test(msg);
	}
}

Arne

[toc] | [prev] | [next] | [standalone]


#39782

FromStanimir Stamenkov <s7an10@netscape.net>
Date2022-06-07 23:19 +0300
Message-ID<t7obs5$vmi$1@dont-email.me>
In reply to#39776
Mon, 23 May 2022 02:31:59 -0700 (PDT), /mike/:

> I changed my regexp:
> 
> sb.toString().matches("(?s).*\\r\\n|\\n##\\r\\n|\\n$")
> 
> but it still evaluates to false. I suspect it is DOT ALL that gives me the headache.

Maybe you really mean (note the grouping):

     sb.toString().matches("(?s).*(\\r\\n|\\n##\\r\\n|\\n)$")

or even:

     sb.toString().matches("(?s).*\\r?\\n(##\\r?\\n?)?$")

-- 
Stanimir

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web