1 16 package org.apache.cocoon.util; 17 18 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 string) throws NumberFormatException { 36 string = string.trim(); 37 int dashPos = string.indexOf('-'); 38 int length = string.length(); 39 if (string.indexOf(',') != -1) { 40 throw new NumberFormatException ("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 ("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 toString() { 85 return this.start + "-" + this.end; 86 } 87 88 89 } 90 | Popular Tags |