KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > util > ByteRange


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.util;
17
18 /**
19  * @author <a HREF="mailto:stuart.roebuck@adolos.co.uk">Stuart Roebuck</a>
20  * @version CVS $Id: ByteRange.java 30932 2004-07-29 17:35:38Z vgritsenko $
21  */

22 final public class ByteRange {
23
24     
25     private final long start;
26     private final long end;
27
28     
29     public ByteRange(long start, long end) {
30         this.start = start;
31         this.end = end;
32     }
33
34     
35     public ByteRange(String JavaDoc string) throws NumberFormatException JavaDoc {
36         string = string.trim();
37         int dashPos = string.indexOf('-');
38         int length = string.length();
39         if (string.indexOf(',') != -1) {
40             throw new NumberFormatException JavaDoc("Simple ByteRange String contains a comma.");
41         }
42         if (dashPos > 0) {
43             this.start = Integer.parseInt(string.substring(0, dashPos));
44         } else {
45             this.start = Long.MIN_VALUE;
46         }
47         if (dashPos < length - 1) {
48             this.end = Integer.parseInt(string.substring(dashPos + 1, length));
49         } else {
50             this.end = Long.MAX_VALUE;
51         }
52         if (this.start > this.end) {
53             throw new NumberFormatException JavaDoc("Start value is greater than end value.");
54         }
55     }
56
57     
58     public long getStart() {
59         return this.start;
60     }
61
62     
63     public long getEnd() {
64         return this.end;
65     }
66
67     
68     public long length() {
69         return this.end - this.start + 1;
70     }
71
72     
73     public ByteRange intersection(ByteRange range) {
74         if (range.end < this.start || this.end < range.start) {
75             return null;
76         } else {
77             long start = (this.start > range.start) ? this.start : range.start;
78             long end = (this.end < range.end) ? this.end : range.end;
79             return new ByteRange(start, end);
80         }
81     }
82
83
84     public String JavaDoc toString() {
85         return this.start + "-" + this.end;
86     }
87
88     
89 }
90
Popular Tags