001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.util;
021:
022: /////////////////////////
023: //$Archive: /JADE/SourceCode/com/salmonllc/util/ByteRangeParser.java $
024: //$Author: Dan $
025: //$Revision: 8 $
026: //$Modtime: 10/30/02 2:59p $
027: /////////////////////////
028:
029: import java.util.*;
030:
031: /**
032: * This class is used by the ObjectStore for byte range requests. Pass a byte range http header and the class will parse it.
033: */
034: public class ByteRangeParser {
035: StringTokenizer _tok;
036: long _fileSize = 0;
037: long _byteStart = 0;
038: long _byteEnd = 0;
039:
040: /**
041: * Create a new parser for a file of the specified size
042: */
043: public ByteRangeParser(String range, long fileSize) {
044: super ();
045: _tok = new StringTokenizer(range, ",=", false);
046: _fileSize = fileSize;
047: }
048:
049: /**
050: * returns the first btye of the current range
051: */
052:
053: public long getFirstByte() {
054: return _byteStart;
055: }
056:
057: /**
058: * returns the last btye of the current range
059: */
060:
061: public long getLastByte() {
062: return _byteEnd;
063: }
064:
065: /**
066: * parses the next range in specified in the header
067: * @return true if there is another range
068: */
069:
070: public boolean nextRange() {
071: if (!_tok.hasMoreTokens())
072: return false;
073:
074: String range = _tok.nextToken();
075: if (range.equals("bytes"))
076: range = _tok.nextToken();
077:
078: String work = "";
079:
080: int pos = range.indexOf("-");
081: if (pos == -1)
082: return false;
083: else if (pos == 0) {
084: work = range.substring(1).trim();
085: _byteEnd = _fileSize - 1;
086: _byteStart = _fileSize - Integer.parseInt(work);
087: } else if (pos == (range.length() - 1)) {
088: work = range.substring(0, range.length() - 1).trim();
089: _byteEnd = _fileSize - 1;
090: _byteStart = Integer.parseInt(work);
091: } else {
092: work = range.substring(0, pos).trim();
093: _byteStart = Integer.parseInt(work);
094: work = range.substring(pos + 1).trim();
095: _byteEnd = Integer.parseInt(work);
096: }
097:
098: if (_byteStart > _byteEnd)
099: return false;
100: else
101: return true;
102: }
103: }
|