NIO: Detection end-of-stream

Hi group,
I'm writing a simple HTTP client based on NIO. Setting up a connection and sending a request all goes well. Reading the response also works, but not as expected. I would expect to receive a -1 value on the SocketChannel.read(buffer) to indicate end of stream. However, if the last chunk of data is read this method returns the size of this chunk (and not -1) and the SocketChannel.read(buffer) is not called anymore until the server closes the connection. If the timeout is not set or is set with a large value my application waits for a long time to continue.
How do I detect the end of the (HTTP Response) stream without waiting for the connection to be closed by the server?
Regards,
Erwin.

ejfdebruijn wrote:
I'm writing a simple HTTP client based on NIO. Setting up a connection and sending a request all goes well. Reading the response also works, but not as expected. I would expect to receive a -1 value on the SocketChannel.read(buffer) to indicate end of stream. However, if the last chunk of data is read this method returns the size of this chunk (and not -1) and the SocketChannel.read(buffer) is not called anymore until the server closes the connection. If the timeout is not set or is set with a large value my application waits for a long time to continue.
How do I detect the end of the (HTTP Response) stream without waiting for the connection to be closed by the server?By interpreting the data.
Read the HTTP header, get the Content-Size header field's value, and limit() your buffers accordingly.
@see the HTTP specs.
A client-server connection might be used for more than one exchange of messages, hence it is left open. You could try passing Connection: close in the request header, but it's not guaranteed to work across the field.
You'd have the same problem with the old IO, btw.

Similar Messages

  • Detecting END or Stream turned off in OSMF  -- Live Stream

    Hi all
    i am using the OSMF player to stream live events.  When the stream ends or if the feed is turned off the player throws a BUFFER state change which my code catches and shows a loading spinner, but how do i detect if the stream has ended or turned off while the user is watching?  Also will be helpfull if I want to post roll some ad or something.   I could mickey mouse it i guess if it throws BUFFER EMPTY and set a timer but there has to be an easier way to do this.
    anybody?
    thanks
    Corey

    it appears Akamais test player counts the bytes loaded when the buffer empties and sets a timer (what I thought! hey, im smart!) and then checks again and if the bytes are different it dispatches an event which backs the feed back to the start (or stops completely if FORCE LIVE is set)
    now, how to know if the feed is live or memorex (ie- archived)
    anybody? beuhler? beuhler?

  • Detecting end of stream in Inputstream

    I'm new to java, and there are several things I am not familiar with.
    I want to download a web page. I have looked at the sample code here:
    http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.html
    and other code like it (using URLConnection, for example). I don't understand how to detect that the entire file has been downloaded. the code
    while ((inputLine = in.readLine()) != null)doesn't seem to differentiate between a true end and just a not-ready condition. What actually happens if the buffer is still filling? What if the server puts a gap in transmission, leaving the buffer empty for a moment?
    Would someone clarify this for me, please?
    Thanks,
    Jim

    lobo93065 wrote:
    What actually happens if the buffer is still filling? What if the server puts a gap in transmission, leaving the buffer empty for a moment?Then the call will wait until it has something to return.

  • AS3 - 1) Countdown Timer to the designated Date for Live-Broadcast-Event yet to take place; 2) Detect End of Live Stream on FLVPlayback with FLVPlayback.isLive = true - ActionScript 3 - flash cs3 cs4

    Hi folks,
    Ronny's here again on forums, having particularly 2 (two) questions/problems to resolve:
    1) Countdown Timer to the designated Date for Live-Broadcast-Event yet to take place
    2) Detect End of Live Stream on FLVPlayback with FLVPlayback.isLive = true
    attached is the .zip file (as3_Countdown Timer_ver 1.0.1_by Ronny Depp.zip) with all flash source files containing:
    a) The FLash Source (file: timer_module.fla) - (FLA flash source file - Flash CS3 Professional, Flash Player 9, actionscript 3.0)
    b) com.othenticmedia.utils.dateAndTimeManagement package including 2 .as actionscript 3.0 Class files.
           i) com.othenticmedia.utils.dateAndTimeManagement.DateAndTimeManager Class in the said package. (file: DateAndTimeManager.as)
           ii) com.othenticmedia.utils.dateAndTimeManagement.CountdownTimer Class in the package. (file: CountdownTimer.as)
    c) The compiled SWF file version of this Application's blueprint. (file: timer_module.swf).
    What i need to confirm is: ........................................................ see the next post of mine. (for Problems  need to be Resolved)

    Problems to Resolve:
    Problem#1) - Countdown Timer to the designated Date for Live-Broadcast-Event yet to take place.
    Problem#2) - Detect End of Live Stream on FLVPlayback with FLVPlayback.isLive = true;
    Problem#1 Description:
    I need to pinpoint the Logical TimeSync Exception, i am still unable to figure out. That is I'm using a webservice in my Application to Synchronize the Time with the actual ET (eastern time) with accomodation of auto-adjustment for EDT GMT-4 (eastern daylight time) & EST GMT-5 (eastern standard time), times. I am using the zipcode: "10012" to pass it to the Web Service in urlRequest object, to retrieve the Current ET eastern time according to EDT & EST time settings for Manhattan/Brooklyn areas or others within  New York, NY 10012.
    Currently the Web Service is returning accurate date/time based on local EDT GMT-4 daylight time.
    Is there some defined set of dates for EDT & EST times for New York region that I can check for to ensure the correct Dates/Times for Eastern Time in New York area ??? I am using NY zipcodes because i am sure to get correct ET values.
    The Major Problem Part: is I need to correct the time by 2 seconds or approx. 2 secs, some millisecs.
    When I retrieve the Time Value from WebService, it lags behind for 2 seconds as compared to DateObj i create using computer's local time, on my Windows XP Service Pack 2 with Automatic Updates turned-on. And I'm sure about my Windows will be having latest updates for Time Management already installed. I also added the 2 secs. to the TimeSync(ed) Date to make correction to this Date obj.
    I call my custom fucntion addSeconds(dateObj:Date, secs:int) to add 2 seconds to the Date by Converting Seconds to Milliseconds.
    Please comb through the as code in files attached and Help Me Out !!!
    Problem#2 Description:
    Secondly I need to Detect the End of Stream state while using FLVPlayback component, an rtmp:// live Stream from FLASH MEDIA SERVER.
    I need to Play a YuMe Post-Roll Ad when Steam Finishes/Ends.
    Live Broadcast Stream Event starts every night on Wednesdays & Saturdays exactly  at 10:59 PM EDT GMT-4.
    Live Events only Streams/Broadcasts the stream for 50secs. exactly. When [playback stopped] it plays a PostRoll Ad and after the CountdownTimer again comes back to life. The Next upcoming Event is calculated & the Countdown begins until Next Event's time/date is reached.
    Here is the  code on the frame 1 on the MainTimeline: (rest of the params like source, volume, skinAutoHide are Set using Property Inspector for FLVPlayback instance on Stage)
    //myStream instance of FLVPlayback is on the Stage
    myStream.isLive = true;// Frame 1 Actions in the FLA
    myStream.addEventListener(VideoEvent.COMPLETE, onEndOfStream);
    myStream.addEventListener(VideoEvent.STATE_CHANGE, onState);
    myStream.addEventListener(String(VideoError.NO_CONNECTION), onStreamError);
    myStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
    /*if(myStream.stopped){
         trace("tracy: "+myStream.state);
    } else if(myStream.state == VideoState.STOPPED){
         trace("tracy: "+myStream.state);
    function onStreamError(event:VideoError) {
         trace(event.code + "\n\t" + event);
    function onState(event:VideoEvent) {
         trace(event.state + "\n\t" + event.toString());
    function onEndOfStream(event:VideoEvent) {
         trace(event.state + "\n\t" + event.toString());
    function netStatusHandler(event:NetStatusEvent):void {
         switch (event.info.code) {
              case "NetConnection.Connect.Success":
                   //connectStream();
                   break;
              case "NetStream.Play.StreamNotFound":
                   trace("Stream not found: "/* + myStream.source*/);
                   break;

  • End of Stream

    Hi,
    How can I send an "End of Stream" down a stream without closing it?
    Cheers
    Rob

    No doesn't seem to work in this case. I'm reading the stream with an app I've written to use the nio package's selectable channels. Below is the read method I'm using. The problem is that clientSocket.read(buff) is never returning -1 even though I can see that all the bytes have been read.
    Basically it does the first pass, reads all the bytes into the buffer and then just hangs around waiting for the channel's interest ops to come back to OP_READ, but they never do. I'm sure I must be reading the socket or checking the buffer badly but I can't figure out where.
         private boolean read(SelectionKey key) throws IOException {
              int read =clientSocket.read(buff);
              if( read==-1) {
                   buff.flip();
                   in = charset.decode(buff).toString();
                   System.out.println("Switch to write");
                   return true;
              } else {
                   System.out.println("More read ["+read+":"+buff.remaining()+"]");
                   key.interestOps(SelectionKey.OP_READ);
                   return false;
         }

  • What is ASCII character for end of stream?

    I googled but failed to find the answer. Is it 004/EOT, 027/ETB, or something else?
    In socket, read() != -1 never be reached, which incurred my above question. I want to see what ascii is used to indicate end of stream.
    Thanks!

    You need to rethnk the way that you reason about behavior and how you ask questions.
    There must be an ASCII to indicate end of stream ... otherwise how [does it] detect ... end of stream?It looks to me like the question you are trying to ask is:
    "How does one socket tell another that it has reached an end of stream?"
    There's a mechanism there - youre just assuming its ASCII or that some sentinel value is sent.
    ASCII is a text encoding. Since youre talking about a communications protocol were talking
    binary. Theres no reason to call it ASCII.
    It could be somthing as simple as: I know how many bytes long the message is and ive read
    that many bytes.

  • DataDirect DB2 drivers (jdbc) exception: end of stream on read

    Using the DataDirect version 3.4 drivers against V8.1 of DB2...
    Having trouble establishing a connection to DB2 with the com.ddtek.jdbc.db2.DB2Driver class:
    SQLException: [DataDirect][DB2 JDBC Driver]End of stream was detected on a read.
    The url is as follows:
    jdbc:datadirect:db2://sadbarkprodtest2:6789;databaseName=INFRA
    Used the default JDBC port number (which has been verified) and the user/password are set as arguments in the Connection initialization. Not sure why the exception is being thrown. Has anybody use had the same problem?
    /Matthew Young

    Running DB2 V8.1.5 on a Solaris machine (sparc)...
    On the client side I am using jdk 141_03 from Bea and the DataDirect JDBC drivers (version 3.4). Sample code looks as follows:
    public class Test {
      private String url = "jdbc:datadirect:db2://sadbarkprodtest2:6789;databaseName=INFRA";
      private String userid = "xxx";
      private String password = "xxx";
      public void connect() {
        try
          Class.forName("com.ddtek.jdbc.db2.DB2Driver");
          connection = DriverManager.getConnection(url,userid,password);
        catch (SQLException sqle) {
           sqle.printStackTrace();
        catch (Exception e) {
           e.printStackTrace();
      public static void main(String[] args) {
        Test test = new Test ();
        test.connect ();
    }I get the same "End of stream was detected on read" error with the Bea DB2 7.*,8.* drivers. Assuming the problem has something to do with the version of DB2.
    / thanks - Matthew

  • Detect "end of file" while send n numbers files over a socket?

    Hi!
    I�m trying to find a way to detect "end of file" while send n numbers files over a socket.
    What i'm looking for is how to detect on the client side when the file i�m sending is downloaded.
    Here is the example i�m working on.
    Client side.
    import java.io.*;
    import java.net.*;
    public class fileTransfer {
        private InputStream fromServer;
        public fileTransfer(String fileName) throws FileNotFoundException, IOException {
            Socket socket = new Socket("localhost", 2006);
            fromServer = socket.getInputStream();
            for(int i=0; i<10; i++)
                receive(new File(i+fileName));
        private void receive(File uploadedFile) throws FileNotFoundException, IOException {
            uploadedFile.createNewFile();
            FileOutputStream toFile = new FileOutputStream(uploadedFile);
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = fromServer.read(buffer)) != -1) {
                toFile.write(buffer, 0, bytesRead);
        public static void main(String[] args) {
            try {
                new fileTransfer("testa.jpg");
            } catch (Exception ex) {ex.printStackTrace();}
    }Server side.
    import java.io.*;
    import java.net.*;
    public class fileTransferSend {
        Socket serv = null;
        OutputStream toClient;
        public fileTransferSend(String fileName) throws FileNotFoundException, IOException {
            StartServer();       
            for(int i =0; i<10; i++)
                send(new File(fileName));
        public void StartServer() throws IOException {
            ServerSocket ssocket = new ServerSocket(2006);
            System.out.println("Waiting for incomming");
            serv = ssocket.accept();
            System.out.println("incomming");
            toClient = serv.getOutputStream();
        private void send(File f) throws FileNotFoundException, IOException {
            if(f.exists() && f.canRead()) {
                FileInputStream fromFile = new FileInputStream(f);
                try {
                    byte[] buffer = new byte[4096]; // 4K
                    int bytesRead = 0;
                    System.out.println("sending: "+f.getName());
                    while ((bytesRead = fromFile.read(buffer)) != -1) {
                        toClient.flush();
                        toClient.write(buffer, 0, bytesRead);
                finally {
                    //toClient.close();
                    fromFile.close();
            } else {
                System.out.println("no files");
        public static void main(String[] args) {
            try {
                new fileTransferSend("test.jpg");
            }catch(Exception e) {e.printStackTrace();}
    I know that the client never reads -1 becuase i doesn�t close the stream.
    Is there anyway to tell the client that the file is downloaded?

    A common (and easy) TCP/IP protocol is to send length, followed by data.
    Because TCP/IP is a stream-oriented protocol, a receiver can never absolutely determine where the first packet ends and the second packet begins. So it is common to send the length of the packet, followed by the packet itself.
    In your case, you could send length of file, followed by file data. It should be fairly easy to obtain file length and send that as a 32-bit (or 64-bit value). Here is an idea (not code) for the receiver:
    receive (4) // where 4 = number bytes to receive
    unsigned length = convert 4 bytes to unsigned integer
    while (length != 0)
    n = receive ( length ) // where n = number bytes actually received, and length = number bytes desired
    Or, you can use the concept of an "Escape" character in the stream. Arbitrarily choose an ESCAPE character of 0x1B (although it could be any 8-bit value). When the receiver detects an ESCAPE char, the next character can be either control or data. So for end of file you might send 0x1B 0x00. If the byte to be sent is 0x1B, then send 0x1B 0x1B. The receiver would look something like this:
    b = read one byte from stream
    if (b == 0x1B)
    b = read one byte from stream
    if (b == 0x00) then end of file
    else place b in buffer
    else
    place b in buffer
    Later.

  • SocketChannel read() -1 end of stream

    Hello,
    I have read in the javadoc of the NIO API that the read() method of
    SockeChannel returns a -1 when the end of stream is reached.
    I can confirm this. After a connection has been idle for a while and
    no data has been sent successive calls to the read() method of
    SocketChannel yield a -1.
    My question now is what the -1 excatly means. I know that it for sure
    means that I can't read any data from the SocketChannel anymore. So
    far, so good.
    However, I'm not certain what it means for the output channel. Can I
    still write data using the put() method to the SocketChannel?
    Does the the -1 signal a closed connection in both directions? Or does
    it only mean that you can't read anymore but can still write data?
    The later would also mean that a -1 does not signal a closed
    connection in the sense that there is not network connection at all
    anymore.
    It would be great if a -1 would signal a close of the connection in
    both directions, but I'm not sure about it and it makes a big
    difference.
    What's you opinion on this?
    Fritz

    The length of -1 means that the input side of the Socket associated with SocketChannel has been shut down. If the output side is still open you can write to it. close shuts down both sides of the socket. the usual response to receiving the -1 length is to close the channel, this completes the close regardless of what your partner did. For examples of this see my NIO Client Example and NIO Server Example.

  • Encore DVD Error Message - "End of Stream has been reached"

    I have created a project In Encore 2. it has motion menus, subtitles and sound. When we go to preview the project it gives us this error
    "End of Stream has been reached"
    We have checked our assets and the audio and video are DVD standard.
    We did a check project and it found no errors with the project.
    We are running Windows XP SP2 on a network, 2GB memory, 200GB hard drive Dell OptiPlex Desktop
    Dose anyone know what we should do???

    The End of Stream Message can be solved by removing one or moor unwanted elements from the time line. in my case that was a jpg image that encore didnt liked.
    It is very difficult to find out witch element you need to remove, but that is the only solution.

  • End of Stream has been reached error CS4

    After transcoding the main video file during the build, Encore then prepares "Planning audio for movie" at which case the error "end of stream has been reached" pop up window appears and the burn subsequently fails. There are no errors within the project and I have burned the project successfully onto a DVD with no error in the past. The only changes this time around is a re encoding of the video file (Uncompressed AVI 1920 x 1280 max depth bit), a change in the video thumbnails for the chapter selection pages, and a tweeking of the chapter points. Because the video file is so large, I delete the previous AVI file and export again using the same name to create the approriate links. I have read that the sound compression is the main cause for this error, however, I have not done anything sound related between versions of the DVD. How can I fix this error from occurring?  Thanks in advance.

    Especially with muxed (Audio & Video), there is a slightly different Block Size for each stream, so what appears to match 100% in PrPro can be every so slightly longer (Audio), when Transcoded. That is why I always leave a touch of "silence" in my Audio.
    For my edits, I almost always start with a Dip-to-Black, from an 02 sec. bit of Black Video, and then end with another 02 sec. of Black Video. My Audio always ends, just before the end of that Black Video. This also makes things seem to flow better, between Timelines, as the user sees things Dip-to-Black, and unless they have a stopwatch going, do not know if a moment has elapsed, when the laser moves, and the next Timeline comes up from black. There is never a jolting "black flash."
    Good luck,
    Hunt

  • Detect end of the .swf file from captivate

    Hi,
    I've a .swf file sent by our clients. They want to put that .swf file on the webpage. The .swf file is published from captivate.
    Now they want to display a submit button on end of the .swf (video). I don't know how to detect end of the video.
    Need help...!!!
    Thank you,
    Himadri

    Welcome to our community
    Within Captivate you are able to choose different actions to occur when the Captivate movie completes play. Of course if you don't have Captivate and the project file you are pretty much stuck with what you have. If you *DO* have access to the source, you would then configure the end action to be what you like. Perhaps execute some JavaScript, load a different page, etc.
    If you don't have access to the source file, you may be better off asking whomever provided the SWF to you to make a small change to the end action. They would then compile a new SWF and provide that to you to use.
    You just said a "Submit" button should appear after the video plays. What exactly is this button supposed to do?
    Cheers... Rick
    Helpful and Handy Links
    Begin learning Captivate 5 moments from now! $29.95
    Captivate Wish Form/Bug Reporting Form
    Adobe Certified Captivate Training
    SorcererStone Blog
    Captivate eBooks

  • How can I set stream to end of stream?

    How can I set stream to end of stream?
    I don't want to close the socket.
    What I have to send that inputstream realizes
    that end of stream has been reached?

    Hi Diego Victor, thanks for the very quick response. Sorry I am delayed getting back to you.
    This made setting home page was easy.
    I can see from cor-els reply how to make a new tab have the sites I want so am trying that for now but still cannot work out how to make a new tab actually go go www.google.co.uk.
    I could not get there from Victors reply. I went to about:config but it said this could invalidate my warranty so nervous of that.
    Sorry if it is just me being a bit stupid but find technical stuff hard - just ask my boss as I work in IT!!

  • End of stream or off-line message?

    When you stop live streaming, is there a way to have the client player present an end of stream or off-line message?
    Right now when I stop streaming, the client plays the last part of the stream and then just buffers endlessly.

    something like this?
    conn = new NetConnection();
    conn.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
    private function onStatus(e:NetStatusEvent):void {
       switch(e.info.code) {
       case "NetStream.Play.StreamNotFound":
       //some code
       break;
       case "NetStream.Play.Stop":
       //some code
       break;

  • Visual admin login (end of stream error)

    If I logon to visual administrator I am getting the following error during connection time(12%)
      Unable to logion to Hostname:
    End of stream reached unexpectedly during input from socket( 168.15.5.2 ,port =50004,localport =2825)
    beacuse of this problem I am not able to log onto visual adminstrator.
    I would be grateful for all your suggestion

    Hi,
    Check your server0.log file I think there is some issue with j2ee_admin password.Any ways check that log file it gives you enough info.
    Regards,
    Vamshi.

Maybe you are looking for

  • IMovie '11 won't recognize Sony HDR-XR100

    I know there are a hundred other threads on this subject, but I can't find the answer I am looking for. I am using fresh installed iMovie '11 (9.0.6) on an Intel Core 2 iMac with OS 10.7.4 the camera is a Sony DCR-XR100. iMovie only seems to recogniz

  • When i insert string text in hebrew i see ?????????

    hi when i insert string text in hebrew i see ????????? how to fix it ? i work on: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production Oracle Developer ver. 2.1.1.64 thanks

  • IMac crashes when previewing some jpg files

    When I preview some jpg files using quicklook my Mac crashes, I get the same result when using get info on the same files. Other jpg files can be previewed without this problem. All jpg files open fine in Photoshop and can be saved with a different f

  • E_PACK_DUPLICATE_SRC error

    During update resource on ACS I got the error responce. <error xmlns="http://ns.adobe.com/adept" data="E_PACK_DUPLICATE_SRC http://myACSurl/packaging/Package urn:uuid:bca93e2f-1eb0-4d6d-883a-d2d91c7e16dd http://myACSurl/media/bca93e2f-1eb0-4d6d-883a-

  • Bank withdrawal

    bank sent email for 3.17 withdrawal I don't need ink and have not received cartridges for coupl of months???