001: // HttpContentRange.java
002: // $Id: HttpContentRange.java,v 1.9 2003/02/11 10:55:18 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 HttpContentRange extends BasicValue {
009: int firstpos = -1;
010: int lastpos = -1;
011: int length = -1;
012: String unit = null;
013:
014: /**
015: * parse.
016: * @exception HttpParserException if parsing failed.
017: */
018: protected void parse() throws HttpParserException {
019: ParseState ps = new ParseState();
020: ps.ioff = 0;
021: ps.bufend = raw.length;
022: // Get the byte unit:
023: ps.separator = (byte) ' ';
024: if (HttpParser.nextItem(raw, ps) < 0)
025: error("Invalid byte range (no byte unit).");
026: this .unit = new String(raw, 0, ps.start, ps.end - ps.start);
027: // Get the first position
028: ps.separator = (byte) '-';
029: ps.prepare();
030: if (HttpParser.nextItem(raw, ps) < 0)
031: error("Invalid byte range (no first position).");
032: ParseState pos = new ParseState();
033: pos.ioff = ps.start;
034: pos.bufend = ps.end;
035: firstpos = HttpParser.parseInt(raw, pos);
036: // Get the last positon
037: ps.prepare();
038: ps.separator = (byte) '/';
039: if (HttpParser.nextItem(raw, ps) < 0)
040: error("Invalid byte range (no last position).");
041: pos.ioff = ps.start;
042: pos.bufend = ps.end;
043: lastpos = HttpParser.parseInt(raw, pos);
044: // Get the full length:
045: ps.prepare();
046: if (HttpParser.nextItem(raw, ps) < 0)
047: error("Invalid byte range (no full length).");
048: pos.ioff = ps.start;
049: pos.bufend = ps.end;
050: if ((raw[pos.ioff] == (byte) '*') && (ps.end - ps.start == 1)) {
051: length = -1;
052: } else {
053: length = HttpParser.parseInt(raw, pos);
054: }
055: }
056:
057: protected void updateByteValue() {
058: HttpBuffer buf = new HttpBuffer();
059: buf.append(unit);
060: buf.append((byte) ' ');
061: buf.appendInt(firstpos);
062: buf.append('-');
063: buf.appendInt(lastpos);
064: buf.append('/');
065: if (length < 0) {
066: buf.append('*');
067: } else {
068: buf.appendInt(length);
069: }
070: raw = buf.getByteCopy();
071: roff = 0;
072: rlen = raw.length;
073: }
074:
075: public Object getValue() {
076: return this ;
077: }
078:
079: /**
080: * Get this range first position.
081: * The meaning of the returne integer is to be understood relative to
082: * the unit, as obtained by <code>getUnit</code> method.
083: * @return An integer value for thr first position.
084: */
085:
086: public int getFirstPosition() {
087: validate();
088: return firstpos;
089: }
090:
091: /**
092: * Set this range first position.
093: * @param firstpos The firt position of the range.
094: */
095:
096: public void setFirstPosition(int firstpos) {
097: if (firstpos != this .firstpos)
098: invalidateByteValue();
099: this .firstpos = firstpos;
100: }
101:
102: /**
103: * Get this range last position.
104: * The meaning of the returne integer is to be understood relative to
105: * the unit, as obtained by <code>getUnit</code> method.
106: * @return An integer value giving the last position.
107: */
108:
109: public int getLastPosition() {
110: validate();
111: return lastpos;
112: }
113:
114: /**
115: * Set this range last position.
116: * @param The last position, as an integer.
117: */
118:
119: public void setLastPosition(int lastpos) {
120: if (lastpos != this .lastpos)
121: invalidateByteValue();
122: this .lastpos = lastpos;
123: }
124:
125: /**
126: * Get this range entity full length.
127: * @return The full length of the entity.
128: */
129:
130: public int getFullLength() {
131: validate();
132: return length;
133: }
134:
135: /**
136: * Set this range entity full length.
137: * @param length The new full length for the entity, -1 if unknown.
138: */
139:
140: public void setFullLength(int length) {
141: if (length != this .length)
142: invalidateByteValue();
143: this .length = length;
144: }
145:
146: /**
147: * Get this content range's unit.
148: * @return A String giving the unit for the range, or <strong>null</strong>
149: * if undefined.
150: */
151:
152: public String getUnit() {
153: validate();
154: return unit;
155: }
156:
157: /**
158: * Set this content range's unit.
159: * @param unit The unit in which this range was measured.
160: */
161:
162: public void setUnit(String unit) {
163: invalidateByteValue();
164: this .unit = unit;
165: }
166:
167: HttpContentRange() {
168: this .isValid = false;
169: }
170:
171: public HttpContentRange(boolean isValid, String unit, int firstpos,
172: int lastpos, int length) {
173: this .isValid = true;
174: setUnit(unit);
175: setFirstPosition(firstpos);
176: setLastPosition(lastpos);
177: setFullLength(length);
178: }
179:
180: }
|