001: // HttpRange.java
002: // $Id: HttpRange.java,v 1.11 2000/08/16 21:38:00 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.http;
007:
008: public class HttpRange extends BasicValue {
009: /**
010: * First position in the range.
011: */
012: protected int firstpos = -1;
013: /**
014: * Last position in the range.
015: */
016: protected int lastpos = -1;
017: /**
018: * The range's unit.
019: */
020: protected String unit = null;
021:
022: /**
023: * The list we belong to, if any.
024: */
025: protected HttpRangeList list = null;
026:
027: /**
028: * parse.
029: * @exception HttpParserException if parsing failed.
030: */
031: protected void parse() throws HttpParserException {
032: ParseState ps = new ParseState(roff, rlen);
033: ParseState it = new ParseState();
034: // Get the range's unit:
035: ps.separator = (byte) '=';
036: if (HttpParser.nextItem(raw, ps) < 0)
037: error("No byte range unit.");
038: unit = ps.toString(raw, true);
039: ps.prepare();
040: // Check for a suffix spec
041: int off = HttpParser.skipSpaces(raw, ps);
042: if (raw[off] == '-') {
043: // Suffix spec (skip sign, and parse last pos)
044: ps.ioff = ++off;
045: this .firstpos = -1;
046: this .lastpos = HttpParser.parseInt(raw, ps);
047: } else {
048: // Normal spec, get first position:
049: ps.separator = (byte) '-';
050: if (HttpParser.nextItem(raw, ps) < 0)
051: error("Invalid range spec: no first pos.");
052: it.prepare(ps);
053: this .firstpos = HttpParser.parseInt(raw, it);
054: // Get last position:
055: ps.prepare();
056: if (HttpParser.nextItem(raw, ps) >= 0) {
057: it.prepare(ps);
058: this .lastpos = HttpParser.parseInt(raw, it);
059: }
060: }
061: }
062:
063: protected void invalidateByteValue() {
064: super .invalidateByteValue();
065: if (list != null)
066: list.invalidateByteValue();
067: }
068:
069: protected void updateByteValue() {
070: HttpBuffer buf = new HttpBuffer();
071: buf.append(unit);
072: buf.append((byte) '=');
073: if (firstpos >= 0)
074: buf.appendInt(firstpos);
075: else if (lastpos < 0)
076: buf.append((byte) '0');
077: buf.append('-');
078: if (lastpos >= 0)
079: buf.appendInt(lastpos);
080: raw = buf.getByteCopy();
081: roff = 0;
082: rlen = raw.length;
083: }
084:
085: public Object getValue() {
086: validate();
087: return this ;
088: }
089:
090: /**
091: * Get the first position of the range.
092: * @return An integer giving the first pos for the range, or <strong>-1
093: * </strong> if undefined.
094: */
095:
096: public int getFirstPosition() {
097: validate();
098: return firstpos;
099: }
100:
101: /**
102: * Set the first position of the range.
103: * @param firstpos The first positon for the range.
104: */
105:
106: public void setFirstPosition(int firstpos) {
107: if (this .firstpos != firstpos)
108: invalidateByteValue();
109: this .firstpos = firstpos;
110: }
111:
112: /**
113: * Get the last position for this range.
114: * If the first position is negative, then the last position is to be
115: * considered as the number of bytes relative to the end of the content.
116: * @return An integer giving the last position.
117: */
118:
119: public int getLastPosition() {
120: validate();
121: return lastpos;
122: }
123:
124: /**
125: * Set the last position for this range.
126: * If the given number is negative, it won't be displayed
127: * meaning that everything from firstpos to the end
128: * @param lastpos The new last position.
129: */
130:
131: public void setLastPosition(int lastpos) {
132: if (this .lastpos != lastpos)
133: invalidateByteValue();
134: this .lastpos = lastpos;
135: }
136:
137: /**
138: * Set the unit in which this range is taken.
139: * @Param unit The unit in which the range is measured.
140: */
141:
142: public void setUnit(String unit) {
143: invalidateByteValue();
144: this .unit = unit;
145: }
146:
147: /**
148: * Get the unit in which this range is taken.
149: * @return The unit in which this range is measured, or <strong>
150: * null</strong> if undefined.
151: */
152:
153: public String getUnit() {
154: validate();
155: return unit;
156: }
157:
158: HttpRange(HttpRangeList list, byte raw[], int roff, int rlen) {
159: this .list = list;
160: this .raw = raw;
161: this .roff = roff;
162: this .rlen = rlen;
163: this .isValid = false;
164: }
165:
166: HttpRange(boolean isValid, String unit, int firstpos, int lastpos) {
167: this .isValid = isValid;
168: setUnit(unit);
169: setFirstPosition(firstpos);
170: setLastPosition(lastpos);
171: }
172:
173: public HttpRange() {
174: this .isValid = false;
175: }
176:
177: }
|