Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > parse output and check if a change

Reply
Thread Tools

parse output and check if a change

 
 
mike
Guest
Posts: n/a
 
      03-07-2013
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 firsttime.

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

br,

//mike
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      03-07-2013
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




 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      03-07-2013
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


 
Reply With Quote
 
Joerg Meier
Guest
Posts: n/a
 
      03-07-2013
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.
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      03-08-2013
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


 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      03-08-2013
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?
 
Reply With Quote
 
Kevin McMurtrie
Guest
Posts: n/a
 
      03-09-2013
In article <5138e77a$0$32107$(E-Mail Removed)>,
Arne Vajh°j <(E-Mail Removed)> 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
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      03-09-2013
On 3/8/2013 10:16 PM, Kevin McMurtrie wrote:
> In article <5138e77a$0$32107$(E-Mail Removed)>,
> Arne Vajh°j <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
optparse: parse v. parse! ?? 7stud -- Ruby 3 02-20-2008 05:20 AM
A Paradise DNS address change? What change? There was no change. Tony Neville NZ Computing 7 09-22-2006 01:02 PM
How to parse a string like C program parse the command line string? linzhenhua1205@163.com C Programming 19 03-15-2005 07:41 PM
parse output screen ok but cant get desired output new file! chuck amadi Python 1 06-23-2004 02:16 PM
Parse Text File and Output to File John M. Lembo Perl 0 08-01-2003 04:34 PM



Advertisments