*
The moose likes Sockets and Internet Protocols and the fly likes Getting garbage characters when reading the java.io.inputstream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Getting garbage characters when reading the java.io.inputstream" Watch "Getting garbage characters when reading the java.io.inputstream" New topic
Author

Getting garbage characters when reading the java.io.inputstream

Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
Hi,

I am trying to read the data using inputstream, that has been written out by a product (solaris machine) onto its output stream.

I am getting all the data, however I get a whole bunch of garbage characters in between the data.

I am collecting the data in a byte array directly from the inputstream.

Can anyone tell me why I might be facing this problem ?

I have tried using Reader and providing different charset values, but it is not working.

Here's the output with garbage (unhighlighted part)
Last login: Wed Dec 23 22:16:34 2009 from 148.147.152.191

Sun Microsystems Inc. SunOS 5.9 Generic May 2002
Sun Microsystems Inc. SunOS 5.9 Generic May 2002
TERM is: vt100


 H H H H H H H H H
7tsdexpert:/export/home/pshindikar $
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Which server are you connecting to? Your own Java server, telnet, SSH, ...? Can you figure out which charset is used on that server? Because if you just try some randomly and you don't find the right one then you won't convert the bytes to chars properly.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

That looks like escape sequences for a VT100 terminal emulator. \x1b[3g then all the \x1bH sequences reset the tab stops, \x1b7 saves the cursor position,x1b[r resets scrolling, \x1b999;999H scrolls to the bottom of the screen, then \x1b6 restores the cursor position.

Is there any other way to get to the server that doesn't expect a terminal emulator to be connected?
Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
Rob Prime,

Yes, I am connecting to a SSH server on the remote machine.

If I try to connect to the same machine manually using PUTTY, I do not see these sequences. But if I try to read them on the stream, I see these sequences ?

I have also tried to create a reader using charset - ISO-8859-1 (as is used by PUTTY) but was not able to accomplish the same result.

Carey Evans,

That is a very good observation. Thanks! Are you aware if there is some way in which i can ignore these sequences while reading ? I am also looking at connecting to the same server, bypassing the VT100 terminal emulator. Do you think there can be a charset mismatch issue ??
Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
I also figured out that PUTTY uses XTERM as its default Terminal.

Can we set the TERM environ variable to point to say "xterm". If yes, can you tell me how to do it ?

I am taking PUTTY as example, simply because manually I connect using PUTTY and it does not show any escape sequences!
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

PuTTY reads the escape sequences, and uses them to position the cursor, clear the screen, etc. You could try passing the TERM environment variable as xterm, but that would just give you slightly different escape sequences.

You should be able to configure your SSH connection not to allocate a pseudo-terminal, or to run your remote program directly instead of the shell, or both, and not get any escape sequences. It's hard to say more without knowing which SSH library you're using, or what program you're running on Solaris.
Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
I am using the JSCH SSH library for connections.

What is bugging me is the fact that I connected to at least 10 different machines (Linux / Solaris) where the terminal is set to VT100 by default and the code worked fine (ie, i have not received any escape sequences).

So I doubt if I should really get into the configuration of a SSH connection through JSch.
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

It may be that there is something in /etc/profile or $HOME/.profile on that machine that is sending the escape sequences, like reset or tput, a different PS1 setting, or a different shell like bash or ksh instead of sh.

The rsync program has similar problems running over SSH, and they have some more about it in their FAQ: http://www.samba.org/rsync/FAQ.html#3.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting garbage characters when reading the java.io.inputstream
 
Similar Threads
JMX + RMIConnector
Ant telnet
java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
Couldn't run JDK1.5 generics in JBOSS4.2.1
com.sun.mail.smtp.SMTPSendFailedException: 451 4.7.1 Please try again later