--- /dev/null
+// HttpRange.java\r
+// $Id: HttpRange.java,v 1.1 2010/06/15 12:19:53 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1996.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.www.http;\r
+\r
+public class HttpRange extends BasicValue {\r
+ /**\r
+ * First position in the range.\r
+ */\r
+ protected int firstpos = -1;\r
+ /**\r
+ * Last position in the range.\r
+ */\r
+ protected int lastpos = -1;\r
+ /**\r
+ * The range's unit.\r
+ */\r
+ protected String unit = null;\r
+\r
+ /**\r
+ * The list we belong to, if any.\r
+ */\r
+ protected HttpRangeList list = null;\r
+\r
+ /**\r
+ * parse.\r
+ * @exception HttpParserException if parsing failed.\r
+ */\r
+ protected void parse()\r
+ throws HttpParserException\r
+ {\r
+ ParseState ps = new ParseState(roff, rlen);\r
+ ParseState it = new ParseState();\r
+ // Get the range's unit:\r
+ ps.separator = (byte) '=';\r
+ if ( HttpParser.nextItem(raw, ps) < 0 )\r
+ error("No byte range unit.");\r
+ unit = ps.toString(raw, true);\r
+ ps.prepare();\r
+ // Check for a suffix spec\r
+ int off = HttpParser.skipSpaces(raw, ps);\r
+ if ( raw[off] == '-' ) {\r
+ // Suffix spec (skip sign, and parse last pos)\r
+ ps.ioff = ++off ; \r
+ this.firstpos = -1;\r
+ this.lastpos = HttpParser.parseInt(raw, ps);\r
+ } else {\r
+ // Normal spec, get first position:\r
+ ps.separator = (byte) '-';\r
+ if ( HttpParser.nextItem(raw, ps) < 0 )\r
+ error("Invalid range spec: no first pos.");\r
+ it.prepare(ps);\r
+ this.firstpos = HttpParser.parseInt(raw, it);\r
+ // Get last position:\r
+ ps.prepare();\r
+ if ( HttpParser.nextItem(raw, ps) >= 0 ) {\r
+ it.prepare(ps);\r
+ this.lastpos = HttpParser.parseInt(raw, it);\r
+ }\r
+ }\r
+ }\r
+\r
+ protected void invalidateByteValue() {\r
+ super.invalidateByteValue();\r
+ if ( list != null )\r
+ list.invalidateByteValue();\r
+ }\r
+\r
+ protected void updateByteValue() {\r
+ HttpBuffer buf = new HttpBuffer();\r
+ buf.append(unit);\r
+ buf.append((byte) '=');\r
+ if ( firstpos >= 0 ) \r
+ buf.appendInt(firstpos);\r
+ else if ( lastpos < 0)\r
+ buf.append((byte) '0');\r
+ buf.append('-');\r
+ if ( lastpos >= 0 )\r
+ buf.appendInt(lastpos);\r
+ raw = buf.getByteCopy();\r
+ roff = 0;\r
+ rlen = raw.length;\r
+ }\r
+\r
+ public Object getValue() {\r
+ validate();\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Get the first position of the range.\r
+ * @return An integer giving the first pos for the range, or <strong>-1\r
+ * </strong> if undefined.\r
+ */\r
+\r
+ public int getFirstPosition() {\r
+ validate();\r
+ return firstpos;\r
+ }\r
+\r
+ /**\r
+ * Set the first position of the range.\r
+ * @param firstpos The first positon for the range.\r
+ */\r
+\r
+ public void setFirstPosition(int firstpos) {\r
+ if ( this.firstpos != firstpos )\r
+ invalidateByteValue();\r
+ this.firstpos = firstpos;\r
+ }\r
+\r
+ /**\r
+ * Get the last position for this range.\r
+ * If the first position is negative, then the last position is to be \r
+ * considered as the number of bytes relative to the end of the content.\r
+ * @return An integer giving the last position.\r
+ */\r
+\r
+ public int getLastPosition() {\r
+ validate();\r
+ return lastpos;\r
+ }\r
+\r
+ /**\r
+ * Set the last position for this range.\r
+ * If the given number is negative, it won't be displayed\r
+ * meaning that everything from firstpos to the end\r
+ * @param lastpos The new last position.\r
+ */\r
+\r
+ public void setLastPosition(int lastpos) {\r
+ if ( this.lastpos != lastpos )\r
+ invalidateByteValue();\r
+ this.lastpos = lastpos;\r
+ }\r
+\r
+ /**\r
+ * Set the unit in which this range is taken.\r
+ * @Param unit The unit in which the range is measured.\r
+ */\r
+\r
+ public void setUnit(String unit) {\r
+ invalidateByteValue();\r
+ this.unit = unit;\r
+ }\r
+\r
+ /**\r
+ * Get the unit in which this range is taken.\r
+ * @return The unit in which this range is measured, or <strong>\r
+ * null</strong> if undefined.\r
+ */\r
+\r
+ public String getUnit() {\r
+ validate();\r
+ return unit;\r
+ }\r
+\r
+ HttpRange(HttpRangeList list, byte raw[], int roff, int rlen) {\r
+ this.list = list;\r
+ this.raw = raw;\r
+ this.roff = roff;\r
+ this.rlen = rlen;\r
+ this.isValid = false;\r
+ }\r
+\r
+ HttpRange(boolean isValid, String unit, int firstpos, int lastpos) {\r
+ this.isValid = isValid;\r
+ setUnit(unit);\r
+ setFirstPosition(firstpos);\r
+ setLastPosition(lastpos);\r
+ }\r
+\r
+ public HttpRange() {\r
+ this.isValid = false;\r
+ }\r
+\r
+}\r