简体   繁体   中英

Get InputStream From a Socket

I want to stream and audio with SIP Connection in java application(SE).I connected with the server and got 200 OK messages.I want to receive data sent by the server. I created a SOCKET and got an InputStream. Here is how I do it. 123.456.789.1 is the my ip address and 1234 is which my application listening port.

            Socket socket=new Socket("123.456.789.1",1234);                
            InputStream in=socket.getInputStream();
            System.out.println("inputSream available :"+in.available());  

But in.available() is always 0 .

But if I get the Object content=response.getContent() ;

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);   
out.writeObject(content);
byte[] contentBytes = bos.toByteArray();   

the lenght of contenBytes equals to the response content length.But when I try to get inputStream and Play ,like following

InputStream pp=new ByteArrayInputStream(b);

          AudioStream as = new AudioStream(pp);
          AudioData data = as.getData();
          ContinuousAudioDataStream cas = new ContinuousAudioDataStream (data);

An Exception throws; java.io.IOException: could not create audio stream from input stream Then I tried to read the inputstream in.read() then when read some bytes,and IOException was thrown. Q1. How can I solve and get InputStream from the socket?
Q2. how to get an inputStream to play the audio?

or let me know where the problem is and how to solve it.

UPDATED: Thank you all who showed a fault in.availabe();
Then I changed the code.

ByteArrayOutputStream ou=new ByteArrayOutputStream();
               int i=0;
                System.out.println("Before while");
               while((i=in.read())!=-1){

               ou.write(i);
                   System.out.println("Wrote :"+i);

               }  

Unfortunately the application doesn't go further.That means only Before while is printed.Application just shows running(I use netbeans IDE).I don't why.Any clarification?

When you use getContent you get some kind of object wrapping the content. Then using an ObjectOutputStream you write the Java representation of that object, not the actual bytes of the original data.

You should be able to do

AudioStream as = new AudioStream(in);
AudioData data = as.getData();
ContinuousAudioDataStream cas = new ContinuousAudioDataStream (data);

or if you do want to buffer the data

int chunkSize;
byte[] chunk = new byte[2048];
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
while ( ( chunkSize = in.read(chunk) ) != -1) {
  outBuffer.write(chunk, 0, chunkSize);
}

ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
AudioStream as = new AudioStream(inBuffer);
AudioData data = as.getData();
ContinuousAudioDataStream cas = new ContinuousAudioDataStream (data);  

available() show how many bytes can be guaranteed read before blocking. It might always return 0.

available() is the number of bytes which can be read with out performing a blocking call to the OS. If you want to know how much data is available you should try to read it and see how much you get.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM