log.info("didn't understand request.. treat as normal request"); if (log.isDebugEnabled()) { logHeaders(request);
int n; byte[] b = new byte[BUFFER_SIZE]; while ((n = in.read(b)) != -1) { out.write(b, 0, n);
    public static void process (HttpServletRequest request, HttpServletResponse response, InputStream in, long len)    throws IOException {	OutputStream out = response.getOutputStream();	// can only do partial content when length is unknown	if (len != -1) {	    String rangeHeader = request.getHeader(RANGE_HEADER);	    // was partial content requested?	    if (rangeHeader != null && rangeHeader.startsWith(BYTE_RANGE)) {		String byteSpec = rangeHeader.substring(BYTE_RANGE.length());		int sepPos = byteSpec.indexOf('-');		// does the byte spec describe a range?		if (sepPos != -1) {		    long offset = 0;		    long endpoint = -1;		    // determine offset		    if (sepPos > 0) {			String s = byteSpec.substring(0, sepPos).trim();			offset = Integer.parseInt(s);		    }		    // determine endpoint		    if (sepPos != byteSpec.length() - 1) {			String s = byteSpec.substring(sepPos + 1).trim();			endpoint = Integer.parseInt(s);		    } else {			endpoint = len - 1;		    }		    // notify receiver this is partial content		    response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);		    String contentRange = offset+"-"+endpoint+"/"+len;		    response.setHeader(CONTENT_RANGE_HEADER, contentRange);		    response.setContentLength((int)(endpoint-offset+1));		    // skip till offset		    if (offset > 0) {			in.skip(offset);		    }		    // write partial data		    int n;		    byte[] b = new byte[BUFFER_SIZE];		    while ((n = in.read(b)) != -1) {			if (endpoint != -1) {			    if (offset + n > endpoint) {				n = (int) (endpoint - offset) + 1;				if (n > 0) {				    out.write(b, 0, n);				}				break;			    }			    offset += n;			}			out.write(b, 0, n);		    }		    // done		    return;		}		log.info("didn't understand request.. treat as normal request");		if (log.isDebugEnabled()) {		    logHeaders(request);		}	    }	    // inform client of data size	    response.setContentLength((int)len);	}	// write all content to client	int n;	byte[] b = new byte[BUFFER_SIZE];	while ((n = in.read(b)) != -1) {	    out.write(b, 0, n);	}    }