KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > http > InclusiveByteRange


1 // ========================================================================
2
// $Id: InclusiveByteRange.java,v 1.11 2005/08/13 00:01:24 gregwilkins Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.http;
17
18 import java.util.Enumeration JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
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 /** Byte range inclusive of end points.
29  * <PRE>
30  *
31  * parses the following types of byte ranges:
32  *
33  * bytes=100-499
34  * bytes=-300
35  * bytes=100-
36  * bytes=1-2,2-3,6-,-2
37  *
38  * given an entity length, converts range to string
39  *
40  * bytes 100-499/500
41  *
42  * </PRE>
43  *
44  * Based on RFC2616 3.12, 14.16, 14.35.1, 14.35.2
45  * @version $version$
46  * @author Helmut Hissen
47  */

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     /**
75      * @param headers Enumeration of Range header fields.
76      * @param size Size of the resource.
77      * @return LazyList of satisfiable ranges
78      */

79     public static List JavaDoc satisfiableRanges(Enumeration JavaDoc headers,long size)
80     {
81         Object JavaDoc satRanges=null;
82         
83         // walk through all Range headers
84
headers:
85         while (headers.hasMoreElements())
86         {
87             String JavaDoc header = (String JavaDoc) headers.nextElement();
88             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(header,"=,",false);
89             String JavaDoc t=null;
90             try
91             {
92                 // read all byte ranges for this header
93
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 JavaDoc 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 JavaDoc toHeaderRangeString(long size)
182     {
183         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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 JavaDoc to416HeaderRangeString(long size)
195     {
196         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(40);
197         sb.append("bytes */");
198         sb.append(size);
199         return sb.toString();
200     }
201
202
203     /* ------------------------------------------------------------ */
204     public String JavaDoc toString()
205     {
206         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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