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


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

parse output and check if a change

Started bymike <mikaelpetterson@hotmail.com>
First post2013-03-07 11:10 -0800
Last post2013-03-08 08:54 +0000
Articles 9 — 6 participants

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


Contents

  parse output and check if a change mike <mikaelpetterson@hotmail.com> - 2013-03-07 11:10 -0800
    Re: parse output and check if a change Arne Vajhøj <arne@vajhoej.dk> - 2013-03-07 14:14 -0500
      Re: parse output and check if a change Arne Vajhøj <arne@vajhoej.dk> - 2013-03-07 14:16 -0500
        Re: parse output and check if a change Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2013-03-08 19:16 -0800
          Re: parse output and check if a change Arne Vajhøj <arne@vajhoej.dk> - 2013-03-08 22:29 -0500
      Re: parse output and check if a change Joerg Meier <joergmmeier@arcor.de> - 2013-03-07 23:50 +0100
        Re: parse output and check if a change Arne Vajhøj <arne@vajhoej.dk> - 2013-03-07 19:13 -0500
    Re: parse output and check if a change Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-03-07 16:59 -0800
    Re: parse output and check if a change lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-03-08 08:54 +0000

#22796 — parse output and check if a change

Frommike <mikaelpetterson@hotmail.com>
Date2013-03-07 11:10 -0800
Subjectparse output and check if a change
Message-ID<d8a65c18-bb59-42e8-85b9-8d47d4b21500@googlegroups.com>
Hi,

I am sending a command to unix and then I parse the output. The output is stored in a Info object ( Info.java). This will be used later in application. 

The problem is that the output from the unix command might change. So I want to make sure that if there is a change then we parse and update the Info object. But if there is no change then we do not need to parse and extract the information. We can use the Info object directly.

The idea I have is to use the output from the command ( a text string) and calculate some kind of hash/md5sum.

So the next time I run the unix command I take the output (a string) and calculate hash/md5 and see if it is an exact match as was generated the first time.

Anyone will to give a hint on how to implememt this using java?

br,

//mike

[toc] | [next] | [standalone]


#22798

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-03-07 14:14 -0500
Message-ID<5138e70e$0$32107$14726298@news.sunsite.dk>
In reply to#22796
On 3/7/2013 2:10 PM, mike wrote:
> I am sending a command to unix and then I parse the output. The
> output is stored in a Info object ( Info.java). This will be used
> later in application.
>
> The problem is that the output from the unix command might change. So
> I want to make sure that if there is a change then we parse and
> update the Info object. But if there is no change then we do not need
> to parse and extract the information. We can use the Info object
> directly.
>
> The idea I have is to use the output from the command ( a text
> string) and calculate some kind of hash/md5sum.
>
> So the next time I run the unix command I take the output (a string)
> and calculate hash/md5 and see if it is an exact match as was
> generated the first time.
>
> Anyone will to give a hint on how to implememt this using java?

You will need to code your logic, but hashing a String is easy:

MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hash = md.digest(data.getBytes());

or maybe:

MessageDigest md = MessageDigest.getInstance("SHA-256");
String hash = toHex(md.digest(data.getBytes()));

...

     private static String toHex(byte[] ba) {
         char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', 
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
         StringBuffer sb = new StringBuffer("");
         for (int i = 0; i < ba.length; i++) {
            sb.append(hexdigit[(ba[i] >> 4) & 0x0F]);
            sb.append(hexdigit[ba[i] & 0x0F]);
         }
         return sb.toString();
      }

Arne



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


#22799

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-03-07 14:16 -0500
Message-ID<5138e77a$0$32107$14726298@news.sunsite.dk>
In reply to#22798
On 3/7/2013 2:14 PM, Arne Vajhøj wrote:
> On 3/7/2013 2:10 PM, mike wrote:
>> I am sending a command to unix and then I parse the output. The
>> output is stored in a Info object ( Info.java). This will be used
>> later in application.
>>
>> The problem is that the output from the unix command might change. So
>> I want to make sure that if there is a change then we parse and
>> update the Info object. But if there is no change then we do not need
>> to parse and extract the information. We can use the Info object
>> directly.
>>
>> The idea I have is to use the output from the command ( a text
>> string) and calculate some kind of hash/md5sum.
>>
>> So the next time I run the unix command I take the output (a string)
>> and calculate hash/md5 and see if it is an exact match as was
>> generated the first time.
>>
>> Anyone will to give a hint on how to implememt this using java?
>
> You will need to code your logic, but hashing a String is easy:
>
> MessageDigest md = MessageDigest.getInstance("SHA-256");
> byte[] hash = md.digest(data.getBytes());
>
> or maybe:
>
> MessageDigest md = MessageDigest.getInstance("SHA-256");
> String hash = toHex(md.digest(data.getBytes()));
>
> ...
>
>      private static String toHex(byte[] ba) {
>          char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
> '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
>          StringBuffer sb = new StringBuffer("");
>          for (int i = 0; i < ba.length; i++) {
>             sb.append(hexdigit[(ba[i] >> 4) & 0x0F]);
>             sb.append(hexdigit[ba[i] & 0x0F]);
>          }
>          return sb.toString();
>       }

MD5 is obsolete from a cryptographic point of view, so I
switched to SHA-256.

Arne

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


#22839

FromKevin McMurtrie <mcmurtrie@pixelmemory.us>
Date2013-03-08 19:16 -0800
Message-ID<513aa989$0$52745$742ec2ed@news.sonic.net>
In reply to#22799
In article <5138e77a$0$32107$14726298@news.sunsite.dk>,
 Arne Vajhøj <arne@vajhoej.dk> wrote:

> On 3/7/2013 2:14 PM, Arne Vajhøj wrote:
> > On 3/7/2013 2:10 PM, mike wrote:
> >> I am sending a command to unix and then I parse the output. The
> >> output is stored in a Info object ( Info.java). This will be used
> >> later in application.
> >>
> >> The problem is that the output from the unix command might change. So
> >> I want to make sure that if there is a change then we parse and
> >> update the Info object. But if there is no change then we do not need
> >> to parse and extract the information. We can use the Info object
> >> directly.
> >>
> >> The idea I have is to use the output from the command ( a text
> >> string) and calculate some kind of hash/md5sum.
> >>
> >> So the next time I run the unix command I take the output (a string)
> >> and calculate hash/md5 and see if it is an exact match as was
> >> generated the first time.
> >>
> >> Anyone will to give a hint on how to implememt this using java?
> >
> > You will need to code your logic, but hashing a String is easy:
> >
> > MessageDigest md = MessageDigest.getInstance("SHA-256");
> > byte[] hash = md.digest(data.getBytes());
> >
> > or maybe:
> >
> > MessageDigest md = MessageDigest.getInstance("SHA-256");
> > String hash = toHex(md.digest(data.getBytes()));
> >
> > ...
> >
> >      private static String toHex(byte[] ba) {
> >          char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
> > '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
> >          StringBuffer sb = new StringBuffer("");
> >          for (int i = 0; i < ba.length; i++) {
> >             sb.append(hexdigit[(ba[i] >> 4) & 0x0F]);
> >             sb.append(hexdigit[ba[i] & 0x0F]);
> >          }
> >          return sb.toString();
> >       }
> 
> MD5 is obsolete from a cryptographic point of view, so I
> switched to SHA-256.
> 
> Arne

I would have just saved the original String if it's not too big.
-- 
I will not see posts from Google because I must filter them as spam

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


#22840

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-03-08 22:29 -0500
Message-ID<513aac82$0$32110$14726298@news.sunsite.dk>
In reply to#22839
On 3/8/2013 10:16 PM, Kevin McMurtrie wrote:
> In article <5138e77a$0$32107$14726298@news.sunsite.dk>,
>   Arne Vajhøj <arne@vajhoej.dk> wrote:
>
>> On 3/7/2013 2:14 PM, Arne Vajhøj wrote:
>>> On 3/7/2013 2:10 PM, mike wrote:
>>>> I am sending a command to unix and then I parse the output. The
>>>> output is stored in a Info object ( Info.java). This will be used
>>>> later in application.
>>>>
>>>> The problem is that the output from the unix command might change. So
>>>> I want to make sure that if there is a change then we parse and
>>>> update the Info object. But if there is no change then we do not need
>>>> to parse and extract the information. We can use the Info object
>>>> directly.
>>>>
>>>> The idea I have is to use the output from the command ( a text
>>>> string) and calculate some kind of hash/md5sum.
>>>>
>>>> So the next time I run the unix command I take the output (a string)
>>>> and calculate hash/md5 and see if it is an exact match as was
>>>> generated the first time.
>>>>
>>>> Anyone will to give a hint on how to implememt this using java?
>>>
>>> You will need to code your logic, but hashing a String is easy:
>>>
>>> MessageDigest md = MessageDigest.getInstance("SHA-256");
>>> byte[] hash = md.digest(data.getBytes());
>>>
>>> or maybe:
>>>
>>> MessageDigest md = MessageDigest.getInstance("SHA-256");
>>> String hash = toHex(md.digest(data.getBytes()));
>>>
>>> ...
>>>
>>>       private static String toHex(byte[] ba) {
>>>           char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
>>> '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
>>>           StringBuffer sb = new StringBuffer("");
>>>           for (int i = 0; i < ba.length; i++) {
>>>              sb.append(hexdigit[(ba[i] >> 4) & 0x0F]);
>>>              sb.append(hexdigit[ba[i] & 0x0F]);
>>>           }
>>>           return sb.toString();
>>>        }
>>
>> MD5 is obsolete from a cryptographic point of view, so I
>> switched to SHA-256.
>
> I would have just saved the original String if it's not too big.

That is the simple solution.

:-)

Arne

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


#22809

FromJoerg Meier <joergmmeier@arcor.de>
Date2013-03-07 23:50 +0100
Message-ID<ocfl63quwss3.1eklrnb9rswic$.dlg@40tude.net>
In reply to#22798
On Thu, 07 Mar 2013 14:14:20 -0500, Arne Vajhøj wrote:

>      private static String toHex(byte[] ba) {
>          char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', 
> '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
>          StringBuffer sb = new StringBuffer("");
>          for (int i = 0; i < ba.length; i++) {
>             sb.append(hexdigit[(ba[i] >> 4) & 0x0F]);
>             sb.append(hexdigit[ba[i] & 0x0F]);
>          }
>          return sb.toString();
>       }

For future reference:

DatatypeConverter.printHexBinary(ba).toLowerCase();

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

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


#22811

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-03-07 19:13 -0500
Message-ID<51392d32$0$32110$14726298@news.sunsite.dk>
In reply to#22809
On 3/7/2013 5:50 PM, Joerg Meier wrote:
> On Thu, 07 Mar 2013 14:14:20 -0500, Arne Vajhøj wrote:
>
>>       private static String toHex(byte[] ba) {
>>           char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
>> '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
>>           StringBuffer sb = new StringBuffer("");
>>           for (int i = 0; i < ba.length; i++) {
>>              sb.append(hexdigit[(ba[i] >> 4) & 0x0F]);
>>              sb.append(hexdigit[ba[i] & 0x0F]);
>>           }
>>           return sb.toString();
>>        }
>
> For future reference:
>
> DatatypeConverter.printHexBinary(ba).toLowerCase();

Ah - my little toHex is no longer needed.

Thanks.

Arne

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


#22813

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2013-03-07 16:59 -0800
Message-ID<gKa_s.227841$411.112277@newsfe02.iad>
In reply to#22796
On 3/7/13 11:10 AM, mike wrote:
> Hi,
>
> I am sending a command to unix and then I parse the output. The output is stored in a Info object ( Info.java). This will be used later in application.
>
> The problem is that the output from the unix command might change. So I want to make sure that if there is a change then we parse and update the Info object. But if there is no change then we do not need to parse and extract the information. We can use the Info object directly.
>
> The idea I have is to use the output from the command ( a text string) and calculate some kind of hash/md5sum.
>
> So the next time I run the unix command I take the output (a string) and calculate hash/md5 and see if it is an exact match as was generated the first time.
>
> Anyone will to give a hint on how to implememt this using java?
>
> br,
>
> //mike
>
So, you're going to run a process every time (which is expensive), and 
then avoid parsing the result (which is likely far less expensive) if 
the output doesn't change?

Is there perhaps a better way to get this Info that doesn't involve 
running an external program?

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


#22816

Fromlipska the kat <"nospam at neversurrender dot co dot uk">
Date2013-03-08 08:54 +0000
Message-ID<FZWdnYNBoOepOqTMnZ2dnUVZ8rydnZ2d@bt.com>
In reply to#22796
On 07/03/13 19:10, mike wrote:
> Hi,
>
> I am sending a command to unix and then I parse the output. The output is stored in a Info object ( Info.java). This will be used later in application.
>
> The problem is that the output from the unix command might change. So I want to make sure that if there is a change then we parse and update the Info object. But if there is no change then we do not need to parse and extract the information. We can use the Info object directly.
>
> The idea I have is to use the output from the command ( a text string) and calculate some kind of hash/md5sum.
>
> So the next time I run the unix command I take the output (a string) and calculate hash/md5 and see if it is an exact match as was generated the first time.
>
> Anyone will to give a hint on how to implememt this using java?
>
> br,
>
> //mike

Just FYI

the MessageDigest class has a method to compare two digests
Not sure what all this 'converting to hex' is all about, just compare 
digests.

So something like (not tested for syntatic correctness)

MessageDigest md = MessageDigest.getInstance("SHA"); //or whatever

...

Info info = new Info();
String data = getData();

byte[] dgst = md.digest(data.getBytes());

info.parse(data);
info.setDigest(dgst);

//later

data = getData();
dgst = md.digest(data.getBytes());

if(!MessageDigest.isEqual(dgst, info.getDigest()){
    info.parse(data);
    info.setDigest(dgst);
}
... etc

What do you think?

lipska

-- 
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun

[toc] | [prev] | [standalone]


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


csiph-web