Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.server;
 
 import java.util.List;
 
 
 /* ------------------------------------------------------------ */
Byte range inclusive of end points.
 
   parses the following types of byte ranges:
 
       bytes=100-499
       bytes=-300
       bytes=100-
       bytes=1-2,2-3,6-,-2

   given an entity length, converts range to string
 
       bytes 100-499/500
 
 
Based on RFC2616 3.12, 14.16, 14.35.1, 14.35.2

Version:
$version$
 
 public class InclusiveByteRange 
 {
     private static final Logger LOG = Log.getLogger(InclusiveByteRange.class);
 
     long first = 0;
     long last  = 0;    
 
     public InclusiveByteRange(long firstlong last)
     {
         this. = first;
         this. = last;
     }
     
     public long getFirst()
     {
         return ;
     }
 
     public long getLast()
     {
         return ;
     }    
 
 
     
     /* ------------------------------------------------------------ */
    

Parameters:
headers Enumeration of Range header fields.
size Size of the resource.
Returns:
LazyList of satisfiable ranges
 
     public static List satisfiableRanges(Enumeration headerslong size)
     {
         Object satRanges=null;
         
         // walk through all Range headers
     headers:
         while (headers.hasMoreElements())
         {
             String header = (Stringheaders.nextElement();
             StringTokenizer tok = new StringTokenizer(header,"=,",false);
             String t=null;
             try
             {
                 // read all byte ranges for this header 
                 while (tok.hasMoreTokens())
                 {
                     try
                     {
                         t = tok.nextToken().trim();
                        long first = -1;
                        long last = -1;
                        int d = t.indexOf('-');
                        if (d < 0 || t.indexOf("-",d + 1) >= 0)
                        {
                            if ("bytes".equals(t))
                                continue;
                            .warn("Bad range format: {}",t);
                            continue headers;
                        }
                        else if (d == 0)
                        {
                            if (d + 1 < t.length())
                                last = Long.parseLong(t.substring(d + 1).trim());
                            else
                            {
                                .warn("Bad range format: {}",t);
                                continue;
                            }
                        }
                        else if (d + 1 < t.length())
                        {
                            first = Long.parseLong(t.substring(0,d).trim());
                            last = Long.parseLong(t.substring(d + 1).trim());
                        }
                        else
                            first = Long.parseLong(t.substring(0,d).trim());
                        if (first == -1 && last == -1)
                            continue headers;
                        if (first != -1 && last != -1 && (first > last))
                            continue headers;
                        if (first < size)
                        {
                            InclusiveByteRange range = new InclusiveByteRange(first,last);
                            satRanges = LazyList.add(satRanges,range);
                        }
                    }
                    catch (NumberFormatException e)
                    {
                        .warn("Bad range format: {}",t);
                        .ignore(e);
                        continue;
                    }
                }
            }
            catch(Exception e)
            {
                .warn("Bad range format: {}",t);
                .ignore(e);
            }    
        }
        return LazyList.getList(satRanges,true);
    }
    /* ------------------------------------------------------------ */
    public long getFirst(long size)
    {
        if (<0)
        {
            long tf=size-;
            if (tf<0)
                tf=0;
            return tf;
        }
        return ;
    }
    
    /* ------------------------------------------------------------ */
    public long getLast(long size)
    {
        if (<0)
            return size-1;
        
        if (<0 ||>=size)
            return size-1;
        return ;
    }
    
    /* ------------------------------------------------------------ */
    public long getSize(long size)
    {
        return getLast(size)-getFirst(size)+1;
    }
    /* ------------------------------------------------------------ */
    public String toHeaderRangeString(long size)
    {
        StringBuilder sb = new StringBuilder(40);
        sb.append("bytes ");
        sb.append(getFirst(size));
        sb.append('-');
        sb.append(getLast(size));
        sb.append("/");
        sb.append(size);
        return sb.toString();
    }
    /* ------------------------------------------------------------ */
    public static String to416HeaderRangeString(long size)
    {
        StringBuilder sb = new StringBuilder(40);
        sb.append("bytes */");
        sb.append(size);
        return sb.toString();
    }
    /* ------------------------------------------------------------ */
    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder(60);
        sb.append(Long.toString());
        sb.append(":");
        sb.append(Long.toString());
        return sb.toString();
    }
New to GrepCode? Check out our FAQ X