ikrs.httpd
Class ContentRange

java.lang.Object
  extended by ikrs.httpd.ContentRange

public class ContentRange
extends java.lang.Object

This is a wrapper/parser class for the HTTP Content-Range header. Note that the HTTP specs expect the last-byte-position to be inclusive which is pretty unlikely in Java (unsually read limits are specified the exclusive way). Don't get confused with that! Specs: (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) Content-Range = "Content-Range" ":" content-range-spec content-range-spec = byte-content-range-spec byte-content-range-spec = bytes-unit SP byte-range-resp-spec "/" ( instance-length | "*" ) byte-range-resp-spec = (first-byte-pos "-" last-byte-pos) | "*" instance-length = 1*DIGIT Examples: . The first 500 bytes: bytes 0-499/1234 . The second 500 bytes: bytes 500-999/1234 . All except for the first 500 bytes: bytes 500-1233/1234 . The last 500 bytes: bytes 734-1233/1234


Field Summary
static java.lang.String NAME_BYTESUNIT_BYTES
           
 
Constructor Summary
ContentRange(java.lang.String bytesUnit, long firstBytePos, long lastBytePos, long instanceLen)
          Create a new 'Content-Range' wrapper with the given values.
 
Method Summary
 long calculateLength()
           
 java.lang.String getBytesUnit()
          Get the 'bytes-unit' value.
 long getFirstBytePosition()
          Get the 'first-byte-position' value.
 long getInstanceLength()
          Get the 'instance-length' value; the returned value is larger than or equal to 'last-byte-position' OR it is -1L, which means that the instance-length is currently unknown.
 long getLastBytePosition()
          Get the 'last-byte-position' value.
static void main(java.lang.String[] argv)
          This is just for testing.
static ContentRange parse(java.lang.String headerValue)
          This static method parses the given 'Content-Range' header field value.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NAME_BYTESUNIT_BYTES

public static final java.lang.String NAME_BYTESUNIT_BYTES
See Also:
Constant Field Values
Constructor Detail

ContentRange

public ContentRange(java.lang.String bytesUnit,
                    long firstBytePos,
                    long lastBytePos,
                    long instanceLen)
             throws java.lang.NullPointerException,
                    java.lang.IllegalArgumentException
Create a new 'Content-Range' wrapper with the given values.

Parameters:
bytesUnit - The bytes-unit value (the only sipported value is NAME_BYTESUNIT_BYTES so far).
firstBytePos - The first byte position (absolute abd inclusive).
lastBytePos - The last byte position (absolute and inclusive).
instanceLen - The length of the whole instance.
Throws:
java.lang.NullPointerException - If 'bytesUnit' is null.
java.lang.IllegalArgumentException - If any of the passed arguments is out of range.
Method Detail

getBytesUnit

public java.lang.String getBytesUnit()
Get the 'bytes-unit' value. Currently the only supported value is "bytes".

Returns:
The value of the 'bytes-unit'; the returned string is never null.

getFirstBytePosition

public long getFirstBytePosition()
Get the 'first-byte-position' value.

Returns:
The value of 'first-byte-position'; the returned value is never negative.

getLastBytePosition

public long getLastBytePosition()
Get the 'last-byte-position' value.

Returns:
The value of 'last-byte-position'; the returned value is never negative and always greater than or equal to 'first-byte-position'.

calculateLength

public long calculateLength()

getInstanceLength

public long getInstanceLength()
Get the 'instance-length' value; the returned value is larger than or equal to 'last-byte-position' OR it is -1L, which means that the instance-length is currently unknown.

Returns:
The value of 'instance-length'.

parse

public static ContentRange parse(java.lang.String headerValue)
                          throws java.lang.NullPointerException,
                                 MalformedRequestException
This static method parses the given 'Content-Range' header field value. The passed string must not be null and match the specification in RFC 2616/sec14.

Throws:
java.lang.NullPointerException
MalformedRequestException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

main

public static void main(java.lang.String[] argv)
This is just for testing.