Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

23
LINES

< > BotCompany Repo | #1032345 // subBot_serveFileRange [dev.]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3186L/18K).

static virtual NanoHTTPD.Response subBot_serveFileRange(File file, S mimeType, S rangeHeader) ctex {
  S rangeValue = dropPrefix("bytes=", trim(rangeHeader));
  long fileLength = file.length(), start, end;
  
  if (rangeValue.startsWith("-")) {
    end = fileLength-1;
    start = fileLength-1-parseLong(dropPrefix("-", rangeValue));
  } else {
    S[] range = rangeValue.split("-");
    start = parseLong(range[0]);
    end = range.length > 1 ? min(fileLength-1, parseLong(range[1])) : fileLength-1;
  }
  
  if (start <= end) {
    long contentLength = end-start+1;
    FileInputStream fileInputStream = new(file);
    fileInputStream.skip(start);
    virtual Response response = subBot_servePartialContent(mimeType, fileInputStream, contentLength);
    call(response, "addHeader", "Content-Range", "bytes " + start + "-" + end + "/" + fileLength);
    ret response;
  } else
    ret subBot_serveRangeNotSatisfiable(rangeHeader);
}

Author comment

Began life as a copy of #1014005

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1032345
Snippet name: subBot_serveFileRange [dev.]
Eternal ID of this version: #1032345/9
Text MD5: 468ceac486a1ce258a2e394ab703530c
Transpilation MD5: 31a95e903ad5e8148ae522d34b150ddb
Author: stefan
Category: javax / html
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-29 01:53:04
Source code size: 955 bytes / 23 lines
Pitched / IR pitched: No / No
Views / Downloads: 150 / 229
Version history: 8 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)