• Post Reply Bookmark Topic Watch Topic
  • New Topic

reading byte array  RSS feed

 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am getting an inputstream of bytes how to convert them to String ?
??? = new ByteArrayInputStream( theProcess.getInputStream() ));
thanks in advance
 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BufferedInputStream in = new BufferedInputStream(theProcess.getInputStream());
while ( len != -1 ){
len = in.read();
if ( len != -1 )

{
System.out.print(len);

}
}

i am getting out put as int how to convert them into char and to string
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't post the same question to multiple forums; I deleted the copy you posted to the I/O forum, since this is really a fundamental question.

One way to do this would be to use a Reader to collect the bytes and turn them into characters, and then append the characters to a StringBuffer as they come in.

 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for the duplicate posting .after posting here i observed the other forum may be appropriate so i placed it there ..i dont know how to forward the posting from here to there.
when i tried to execute the code u have adviced its returning count is returning -1

but the len is returning
21631291471471931511511621091311501491341371357516316716337227196150
13116275240248752402427524024275240240109240240242109242240240245240
24724224575133129153372271531291492011521091982272402442402417513312
91533722715312914920115210919822724024424024110922719722622775133129
15337132133147150168148133149163109162131153137151163162109240248240
24124024510924024124224737132133151147150168751671481473713213315114
71501681481331491631091621311531371511631623713213316537136133147147
15075131147129162162371371491621631971291532131967514512913114737162
133163149150132133751451291311473716213316316415119514813221113714913337
thanks for the prompt responces
[ August 07, 2005: Message edited by: kamesh aru ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"count" in my program is the number of bytes read at each iteration of the loop. It will always be -1 after the last iteration; -1 means "end of file".
In my program, "theString" is a String made of all the characters read from the input stream; you can print it or do whatever you want with it.

In your original, "len" (oddly named variable!) holds the individual character read from the stream, until the end, when it holds -1. If you print "len" it prints as an int; if you want it as a char, you cast it -- i.e.

System.out.print((char) len);
 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[conde]
import java.io.*;

public class CallHelloPgm
{
public static void main(String args[])
{
Process theProcess = null;
ObjectInputStream inStream = null;
System.out.println("CallHelloPgm.main() invoked");

File filepath = new File("/home/chgmgmt/");

// call the ls command
try
{

theProcess = Runtime.getRuntime().exec("ls" );


}
catch(IOException e)
{
System.err.println("Error on exec() method");
e.printStackTrace();
}



// read from the called program's standard output stream
try
{

int len = 0;
BufferedInputStream in = new BufferedInputStream(theProcess.getInputStream());
while ( len != -1 ){
len = in.read();
if ( len != -1 )

{ // do something with buf }}

// StringBuffer buf = new StringBuffer();
// buf.append((char)len);
// System.out.print(buf.toString());

System.out.print(len);

}
}


////////////////////////////////////////////Ernest Friedman-Hill/////////////
BufferedReader reader = new BufferedReader(new InputStreamReader(theProcess.getInputStream()));
char[] buffer = new char[1024];
StringBuffer stringBuffer = new StringBuffer();
int count;
while ((count = reader.read(buffer, 0, 1024)) > -1) {
stringBuffer.append(buffer, 0, count);
System.err.println("the count"+count);

}


String theString = stringBuffer.toString();
System.out.println("the string is "+theString);
////////////////////////////////////////////////////////////////////////////////


}

catch(IOException e)
{
System.err.println("Error on inStream.readLine()");
e.printStackTrace();
}


} // end method

} // end class

[/code]
-------------------------------------------------

java CallHelloPgm
------------------------------------------
out put in as400
------------------------------------------
CallHelloPgm.main() invoked
71982271952402422402413719822723024024124024137201148151230133130109242240240
24524024424224710922724224024024175133129153372151531501511331531631681091371
49162163129147147193151151162751671481473721515315015113315316316810913714916
21631291471471931511511621091311501491341371357516316716337227196150131162752
the string i
s
$
---------------------------------------------------
count is -1 means i am not getting in to the while loop in the code u suggested
thanks for the responces
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My goodness.

You're trying to read the data from the process twice. First, your old code reads all the data, and prints each character as an int. Now, all the dats is read, and any further attempts to read the output of that process will return -1, because there's no more data to read.

Then, you've pasted in my code, which has no data to read, and so no characters get added to the buffer, and you end up with an empty String.

Programming is a tricky business, isn't it!
 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for prompt responces

i commented the first part of reading the process i am executing the code u adviced the out put is
------------------------------------------------
CallHelloPgm.main() invoked
the count688
the string is � ��� m������K

%� ��� m������m����K
%��� �
�K �%��� ��K
%���
m��������m������m����K
%� �
K ��%� �
� K ��%� ��
m������K
%� �
m������K
%� �
m������m����K
%�
�� �
%�
�� �
m �� m�� %�
�� �
m
�%� m������K
%������%������%������%������%������%������%������%������%������%������%����
��%������%������%������%�������%�������%�������%� �
m��������m�����K
%�
��m �� � �K� %�
��m �� � �m K���%�� �K��K��K��K��m���m��������K
%� � m������K
�m� ��m������m����%
�K� %

�m� ��%
�%
K ��% ��� ��K %�

K %�
�� � �
%� �K� %��m �%�� %
$
--------------------------
i am running the code in as400 environment in qshell
thanks in advance
[ August 08, 2005: Message edited by: kamesh aru ]
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the AS/400 (iSeries) you have the whole EBCDIC/ASCII (different character sets/encodings) issue.

If you are just trying to get a list of files in a directory, why not try an alternate approach:



compile with "javac FileLister.java"
run with "java FileLister /home" (for example)

Have a read up on the API for java.io.File for more interesting things you can do:
http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!