Re: ?s about Process Class

From: publius36 (publius36_at_yahoo.com)
Date: 07/08/04


Date: Thu, 08 Jul 2004 19:21:26 GMT

ipconfig is work well for test purposes..

however the code frag you gave...

> InputStream in = process.getInputStream();
> BufferedInputStream buf = new BufferedInputStream(in);
> InputStreamReader r = new InputStreamReader(buf);

won't work : syntax ---> public InputStreamReader(InputStream in)
you are supplying a BufferedInputStream object and not the required
InputStream object

the correct code frag should be :

   InputStream in = nhProcess.getInputStream();
   InputStreamReader r = new InputStreamReader(in);
   BufferedReader buf = new BufferedReader(r);

to achieve what you intended.
even though the syntax for BufferedReader is
public BufferedReader(Reader obj)
I think it works because the Reader object is the superclass
of all readers, and why it can handle the InputStreamReader object.

"Chris Smith" <cdsmith@twu.net> wrote in message
news:MPG.1b57298c8b8f26929896e2@news.altopia.net...
> publius36 wrote:
> > Well I can't get it to work and being the presistant bugger that i am I
have
> > switch tactics so I can more up the learning curve.
> > I have resorted to just trying to display the output of mem.exe.
>
> Okay. I have a number of comments. Many of them are not necessarily
> related to the specific problem you've got, but here they are anyway.
>
> First,
>
> > while((ch = buf.read()) != -1)
> > {
> > System.out.print((char)ch);
> > }
>
> BufferedInputStream reads bytes. System.out writes characters (it's a
> rather confused case, actually, but if you call its "print" method that
> it writes characters.) Bytes and characters are *not* the same thing.
> The right way to do this is to use an InputStreamReader to convert from
> bytes to characters.
>
> InputStream in = process.getInputStream();
> BufferedInputStream buf = new BufferedInputStream(in);
> InputStreamReader r = new InputStreamReader(buf);
>
> then
>
> while ((ch = r.read()) != -1)
> {
> System.out.print((char) ch);
> }
>
> then
>
> r.close();
>
> The way you've written your code only happens to work because your
> system default character encoding matches the byte-truncation of unicode
> for all characters that you read. As the system's default encoding
> changes, or as you start working with programs that output different
> characters such as accented or non-latin characters, the code will
> suddenly break. InputStreamReader solves that problem.
>
> > It works the first time I run the class file with java test.
> > However, the next attempt hangs and requires a system restart inorder to
get
> > the code to display the results.
>
> Actually, it probably requires killing the runaway NTVDM process on
> Windows. There is a bug in modern 32-bit Windows operating systems
> running a 16-bit process in a way that doesn't attach it to a physical
> console, and this causes NTVDM to become confused. Since mem.exe is
> just a test program for you, the answer is to choose a different program
> to test with. Try "ipconfig.exe" instead.
>
> --
> www.designacourse.com
> The Easiest Way to Train Anyone... Anywhere.
>
> Chris Smith - Lead Software Developer/Technical Trainer
> MindIQ Corporation



Relevant Pages

  • Re: File corruption when copying over sockets
    ... > InputStreamReader wrapped in a BufferedReader. ... Similarly a wave file was horribly corrupt and sounded ... The InputStreamReader will convert the input bytes into ... characters using the default platform encoding. ...
    (comp.lang.java.help)
  • Browser versus Java URLConnection
    ... InputStreamReader and a BufferedReader: ... saved out of a browser from the same URL. ... characters, whereas the lines read in Java are missing ... if I change the character encoding to "UTF-8" then the ...
    (comp.lang.java.programmer)
  • What is the purpose of all these Streams?
    ... And why is there besides a BufferedInputStream also a Buffered Reader? ... InputStreamReader r = new InputStreamReader); ... Any enlightment upon these subjects would be very welcome ... ...
    (comp.lang.java)
  • Re: What is the purpose of all these Streams?
    ... One is buffered a buffered input stream, ... BufferedInputStream is a subclass of InputStream... ... >And why is there besides a BufferedInputStream also a Buffered Reader? ... >InputStreamReader r = new InputStreamReader(new BufferedInputStream( ...
    (comp.lang.java)
  • Re: File corruption when copying over sockets
    ... In message, Matt Humphrey writes ... The InputStreamReader will convert the input bytes into characters using the default platform encoding. ...
    (comp.lang.java.help)