Not able to get whole requested streaming data
<p>I want to read requested URL data and wants to save it on my local file <code>FileOutputStream outFile</code> 
In my code i am not getting how to read whole requested URL data.
I am getting only 1KB data, but my requested file size is 924KB .
How i can get whole requested file data?</p>

<p>This is my code</p>

<pre><code>import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
 import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;

import javax.microedition.io.Datagram;
import javax.microedition.io.Connector;
import javax.microedition.media.Control;
import javax.microedition.media.MediaException;
import javax.microedition.io.SocketConnection;
import javax.microedition.io.DatagramConnection;
import javax.microedition.media.protocol.SourceStream;
import javax.microedition.media.protocol.ContentDescriptor;

public class RTPSourceStreamMY implements SourceStream {

private RTSPProtocolHandler handler;

private InputStream is;
private OutputStream os;
int flag =0;
 private FileOutputStream outFile;

private DatagramSocket socket;

public RTPSourceStreamMY(String address) throws IOException, MediaException {

    // create the protocol handler and set it up so that the
    // application is ready to read data

    // create a socketconnection to the remote host
    // (in this case I have set it up so that its localhost, you can
    // change it to wherever your server resides)
    //SocketConnection sc =
    //  (SocketConnection)Connector.open("socket://localhost:554");
    Socket sc=new Socket("localhost",554);

    // open the input and output streams
    //is = sc.openInputStream();
    //os = sc.openOutputStream();
    is = sc.getInputStream();
    os = sc.getOutputStream();
    System.out.println("InputStream:"+is.available());

    // and initialize the handler
    handler = new RTSPProtocolHandler(address, is, os);


    //Open file for writting buffer data
     outFile = 
  new FileOutputStream("E:\\Tejas\\NewData_FromNov\\MovieFile.mp4");
    // send the basic signals to get it ready'

    handler.doDescribe();
    handler.doSetup();      
      // and send the PLAY command
     handler.doPlay();
      socket=new DatagramSocket(8080);
      byte[] input = new byte[1024*16];
      int length=input.length;
      read(input,0,length);   

      handler.doTeardown();


}

public void start() throws IOException, MediaException {

  // open a local socket on port 8080 to read data to
 // socket = (DatagramConnection)Connector.open("datagram://:8080");
     socket = new DatagramSocket(8080);
  // and send the PLAY command
  handler.doPlay();
}

public void close() throws IOException {

    if(handler != null) handler.doTeardown();

    is.close();
    os.close();
}

public int read(byte[] buffer, int offset, int length)
  throws IOException {

     // create a byte array which will be used to read the datagram
     byte[] fullPkt = new byte[length];

     // the new Datagram
    ////// Datagram packet = socket.newDatagram(fullPkt, length);
     DatagramPacket packet = new DatagramPacket(fullPkt, length);

     // receive it
     socket.receive(packet);

     // extract the actual RTP Packet's media data in the requested buffer
     ///////
   RTPPacket rtpPacket = getRTPPacket(packet, packet.getData());
   buffer = rtpPacket.getData();
   ///////
  outFile.write(buffer);
  outFile.flush();
   outFile.close();
   System.out.println("File data:"+buffer);
   // debug
   System.err.println(rtpPacket + " with media length: " + buffer.length);

     // and return its length
     return buffer.length;
}

// extracts the RTP packet from each datagram packet received
private RTPPacket getRTPPacket(DatagramPacket packet, byte[] buf) {

  // SSRC
  long SSRC = 0;

    // the payload type
    byte PT = 0;

  // the time stamp
    int timeStamp = 0;

    // the sequence number of this packet
    short seqNo = 0;


    // see http://www.networksorcery.com/enp/protocol/rtp.htm
    // for detailed description of the packet and its data
    PT =
      (byte)((buf[1] &amp; 0xff) &amp; 0x7f);

    seqNo =
      (short)((buf[2] &lt;&lt; 8) | ( buf[3] &amp; 0xff));

    timeStamp =
      (((buf[4] &amp; 0xff) &lt;&lt; 24) | ((buf[5] &amp; 0xff) &lt;&lt; 16) |
        ((buf[6] &amp; 0xff) &lt;&lt; 8) | (buf[7] &amp; 0xff)) ;

    SSRC =
      (((buf[8] &amp; 0xff) &lt;&lt; 24) | ((buf[9] &amp; 0xff) &lt;&lt; 16) |
        ((buf[10] &amp; 0xff) &lt;&lt; 8) | (buf[11] &amp; 0xff));


    // create an RTPPacket based on these values
    RTPPacket rtpPkt = new RTPPacket();

    // the sequence number
    rtpPkt.setSequenceNumber(seqNo);

    // the timestamp
    rtpPkt.setTimeStamp(timeStamp);

    // the SSRC
    rtpPkt.setSSRC(SSRC);

    // the payload type
    rtpPkt.setPayloadType(PT);

    // the actual payload (the media data) is after the 12 byte header
    // which is constant
    byte payload[] = new byte [packet.getLength() - 12];

    for(int i=0; i &lt; payload.length; i++) payload [i] = buf[i+12];

    // set the payload on the RTP Packet
    rtpPkt.setData(payload);

    // and return the payload
    return rtpPkt;

}

public long seek(long where) throws IOException {
 throw new IOException("cannot seek");
}

public long tell() { return -1; }

public int getSeekType() { return NOT_SEEKABLE; }

public Control[] getControls() { return null; }

public Control getControl(String controlType) { return null; }

public long getContentLength() { return -1; }

public int getTransferSize() { return -1;   }

public ContentDescriptor getContentDescriptor() {
    return new ContentDescriptor("audio/rtp");
}
  }
</code></pre>
