01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.util;
18:
19: /**
20: * @author <a href="mailto:stuart.roebuck@adolos.co.uk">Stuart Roebuck</a>
21: * @version CVS $Id: ByteRange.java 433543 2006-08-22 06:22:54Z crossley $
22: */
23: final public class ByteRange {
24:
25: private final long start;
26: private final long end;
27:
28: public ByteRange(long start, long end) {
29: this .start = start;
30: this .end = end;
31: }
32:
33: public ByteRange(String string) throws NumberFormatException {
34: string = string.trim();
35: int dashPos = string.indexOf('-');
36: int length = string.length();
37: if (string.indexOf(',') != -1) {
38: throw new NumberFormatException(
39: "Simple ByteRange String contains a comma.");
40: }
41: if (dashPos > 0) {
42: this .start = Integer.parseInt(string.substring(0, dashPos));
43: } else {
44: this .start = Long.MIN_VALUE;
45: }
46: if (dashPos < length - 1) {
47: this .end = Integer.parseInt(string.substring(dashPos + 1,
48: length));
49: } else {
50: this .end = Long.MAX_VALUE;
51: }
52: if (this .start > this .end) {
53: throw new NumberFormatException(
54: "Start value is greater than end value.");
55: }
56: }
57:
58: public long getStart() {
59: return this .start;
60: }
61:
62: public long getEnd() {
63: return this .end;
64: }
65:
66: public long length() {
67: return this .end - this .start + 1;
68: }
69:
70: public ByteRange intersection(ByteRange range) {
71: if (range.end < this .start || this .end < range.start) {
72: return null;
73: } else {
74: long start = (this .start > range.start) ? this .start
75: : range.start;
76: long end = (this .end < range.end) ? this .end : range.end;
77: return new ByteRange(start, end);
78: }
79: }
80:
81: public String toString() {
82: return this .start + "-" + this.end;
83: }
84:
85: }
|