1 16 package org.mortbay.http; 17 18 import java.util.Enumeration ; 19 import java.util.List ; 20 import java.util.StringTokenizer ; 21 22 import org.apache.commons.logging.Log; 23 import org.mortbay.log.LogFactory; 24 import org.mortbay.util.LazyList; 25 import org.mortbay.util.LogSupport; 26 27 28 48 public class InclusiveByteRange { 49 private static Log log = LogFactory.getLog(InclusiveByteRange.class); 50 51 52 long first = 0; 53 long last = 0; 54 55 public InclusiveByteRange(long first, long last) 56 { 57 this.first = first; 58 this.last = last; 59 } 60 61 public long getFirst() 62 { 63 return first; 64 } 65 66 public long getLast() 67 { 68 return last; 69 } 70 71 72 73 74 79 public static List satisfiableRanges(Enumeration headers,long size) 80 { 81 Object satRanges=null; 82 83 headers: 85 while (headers.hasMoreElements()) 86 { 87 String header = (String ) headers.nextElement(); 88 StringTokenizer tok = new StringTokenizer (header,"=,",false); 89 String t=null; 90 try 91 { 92 while (tok.hasMoreTokens()) 94 { 95 t=tok.nextToken().trim(); 96 97 long first = -1; 98 long last = -1; 99 int d=t.indexOf('-'); 100 if (d<0 || t.indexOf("-",d+1)>=0) 101 { 102 if ("bytes".equals(t)) 103 continue; 104 log.warn("Bad range format: "+t); 105 continue headers; 106 } 107 else if (d==0) 108 { 109 if (d+1<t.length()) 110 last = Long.parseLong(t.substring(d+1).trim()); 111 else 112 { 113 log.warn("Bad range format: "+t); 114 continue headers; 115 } 116 } 117 else if (d+1<t.length()) 118 { 119 first = Long.parseLong(t.substring(0,d).trim()); 120 last = Long.parseLong(t.substring(d+1).trim()); 121 } 122 else 123 first = Long.parseLong(t.substring(0,d).trim()); 124 125 126 if (first == -1 && last == -1) 127 continue headers; 128 129 if (first != -1 && last != -1 && (first > last)) 130 continue headers; 131 132 if (first<size) 133 { 134 InclusiveByteRange range = new 135 InclusiveByteRange(first, last); 136 satRanges=LazyList.add(satRanges,range); 137 } 138 } 139 } 140 catch(Exception e) 141 { 142 log.warn("Bad range format: "+t); 143 LogSupport.ignore(log,e); 144 } 145 } 146 return LazyList.getList(satRanges,true); 147 } 148 149 150 public long getFirst(long size) 151 { 152 if (first<0) 153 { 154 long tf=size-last; 155 if (tf<0) 156 tf=0; 157 return tf; 158 } 159 return first; 160 } 161 162 163 public long getLast(long size) 164 { 165 if (first<0) 166 return size-1; 167 168 if (last<0 ||last>=size) 169 return size-1; 170 return last; 171 } 172 173 174 public long getSize(long size) 175 { 176 return getLast(size)-getFirst(size)+1; 177 } 178 179 180 181 public String toHeaderRangeString(long size) 182 { 183 StringBuffer sb = new StringBuffer (40); 184 sb.append("bytes "); 185 sb.append(getFirst(size)); 186 sb.append('-'); 187 sb.append(getLast(size)); 188 sb.append("/"); 189 sb.append(size); 190 return sb.toString(); 191 } 192 193 194 public static String to416HeaderRangeString(long size) 195 { 196 StringBuffer sb = new StringBuffer (40); 197 sb.append("bytes */"); 198 sb.append(size); 199 return sb.toString(); 200 } 201 202 203 204 public String toString() 205 { 206 StringBuffer sb = new StringBuffer (60); 207 sb.append(Long.toString(first)); 208 sb.append(":"); 209 sb.append(Long.toString(last)); 210 return sb.toString(); 211 } 212 213 214 215 } 216 217 218 219 | Popular Tags |