W3C home > Mailing lists > Public > ietf-http-wg@w3.org > October to December 2015

Range requests (information only)

From: Dave Wain <dave.wain@ntlworld.com>
Date: Tue, 22 Dec 2015 10:10:59 -0000
To: <ietf-http-wg@w3.org>
Message-ID: <001a01d13ca1$0e457810$2ad06830$@ntlworld.com>
A typical Java get with a range request looks like this:

	/**
	 * Retrieves the part of the file required via HTTP.
	 */
	private byte[] getHttpBytes(HeaderAndData hnd, long index, int
nbytes)
	{
		// Allocate a new buffer
		final byte[] bytes = new byte[nbytes];

		try {
			// Create a header to read the correct data
			final URLConnection uc = hnd.data.openConnection();
			final long last = index + nbytes - 1;
	
uc.setRequestProperty("Range","bytes="+index+"-"+last);
			uc.setRequestProperty("Accept-Encoding","gzip,
deflate");
			uc.setUseCaches(false); //BUG applets

			// Do the request
			final InputStream raw = uc.getInputStream();
			final String encoding =
uc.getHeaderField("Content-Encoding");
			final InputStream in;
			if ("gzip".equals(encoding)) {
				in = new GZIPInputStream(raw);
			} else if ("deflate".equals(encoding)) {
				in = new InflaterInputStream(raw);
			} else {
				in = raw;
			}

			// Read the required data
			int nread = 0;
			for (int result; nread<nbytes; nread+=result) {
				result = in.read(bytes, nread,
nbytes-nread);
				if (result < 0) break;
			}

			// Check for incomplete reads
			if (nread < nbytes) {
	
DEMViewer.log("Incomplete,nbytes="+nbytes+",nread="+nread);
			}

			// Clear and close (implies Stay-Alive)
			while (raw.available() > 0) raw.read();
			raw.close();
		}
		catch(IOException ex) {
			DEMViewer.log(ex+",index="+index+",nbytes"+nbytes);
		}

		return bytes;
	}

Hope this helps :)

DW

Note: This message is private and confidential and hence
must be received without interception or distortion by the
intended recipients only. Permission to use the information
explicitly must come from the sender (and recipients).

My personal web site and alternate contact details are at:
http://homepage.ntlworld.com/dave.wain/.
Received on Tuesday, 22 December 2015 10:11:40 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 1 March 2016 11:11:40 UTC