javahtmlvideopartial-response

partial range request with last byte failure


In trying to fulfill a partial range request using chrome as the video playback tool, the video playback start playing back but on reaching halfway, it freezes as if the client is still waiting on more data. At this point the server has already sent the entire request. Please observe the following requests and response and the code used to send the range requested:

Range:bytes=0-

Accept-Ranges:bytes
Content-Length:546827
Content-Range:bytes 0-546827/546828
Content-Type:video/webm

Accept-Ranges:bytes
Content-Length:6155
Content-Range:bytes 540672-546827/546828
Content-Type:video/webm


Accept-Ranges:bytes
Content-Length:1
Content-Range:bytes 546827-546827/546828
Content-Type:video/webm

Is the second response handled correctly? Cause it freezes on this request and starts making multiple requests again until the request times out.

Code handling the request:

private static void copyStream (PullBufferStream inputStream, OutputStream outputStream, BytesStreamObject bytesWrittenObj, int from, int to) throws IOException, CodecNotAvailableException {
    //export media stream
    BufferedOutputStream bout = new BufferedOutputStream (outputStream);
    Buffer b = new Buffer();
    long skipped =0;
    int byteLength = 0;

    do {
        inputStream.read(b);

        int len    = b.getLength();
        byte[] data   = (byte[]) b.getData();
        int  offset = b.getOffset();
        if (len > 0) {
            if(skipped < from) {             
              // skip bytes until reach from position in inputstream
              if(skipped + len <= from) {     
                  // skip all bytes just read
                  skipped += len;
              } else {                       
                  // skip only some of bytes read 
                  offset += (from - skipped);
                  len -= (from - skipped);
                  skipped = from;

              }
            } 
            if(to >= 0) {
                if(from + byteLength + len <= to) {
                    // use the full buffer
                } else {
                    // trim len to needed bytes to be read to prevent exceeding the "to" input parameter
                    len = (to + 1) - (from + byteLength);
                }
            } 

            byteLength+= len;
            bytesWrittenObj.bytesWritten = byteLength;
            bout.write(data, offset, len);
            //bout.write(data, from, end);
        } 
    } while (!b.isEOM());
    //fileProperties.setBytesLength(byteLength);

    //bout.close(); 

}

Solution

  • I needed to ensure I flush the outputstream but also made some changes on the actual file size rather than using stream.available().