View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  package org.fourthline.cling.model.types;
16  
17  /**
18   *
19   * @author Mario Franco
20   */
21  public class BytesRange {
22  
23      public static final String PREFIX = "bytes=";
24      
25      private Long firstByte;
26      private Long lastByte;
27      private Long byteLength;
28  
29      public BytesRange(Long firstByte, Long lastByte) {
30          this.firstByte = firstByte;
31          this.lastByte = lastByte;
32          this.byteLength = null;
33      }
34  
35      public BytesRange(Long firstByte, Long lastByte, Long byteLength) {
36          this.firstByte = firstByte;
37          this.lastByte = lastByte;
38          this.byteLength = byteLength;
39      }
40  
41      /**
42       * @return the firstByte
43       */
44      public Long getFirstByte() {
45          return firstByte;
46      }
47  
48      /**
49       * @return the lastByte
50       */
51      public Long getLastByte() {
52          return lastByte;
53      }
54  
55      /**
56       * @return the byteLength
57       */
58      public Long getByteLength() {
59          return byteLength;
60      }
61  
62      /**
63       * 
64       * @return String format of Bytes Range for response message header 
65       */
66      public String getString() {
67          return getString(false,null);
68      }
69  
70      /**
71       * 
72       * @return String format of Bytes Range for response message header 
73       */
74      public String getString(boolean includeDuration) {
75          return getString(includeDuration,null);
76      }
77      
78      /**
79       * 
80       * @return String format of Bytes Range for response message header 
81       */
82      public String getString(boolean includeDuration, String rangePrefix) {
83          String s = (rangePrefix!=null)?rangePrefix:PREFIX;
84  
85          if (firstByte!=null)
86              s += firstByte.toString();
87          s += "-";
88          if (lastByte!=null)
89              s+= lastByte.toString();
90          if (includeDuration) {
91              s += "/" + (byteLength != null ? byteLength.toString() : "*");
92          }
93  
94          return s;
95      }
96  
97      public static BytesRange valueOf(String s) throws InvalidValueException {
98          return valueOf(s,null);
99      }
100     
101     public static BytesRange valueOf(String s, String rangePrefix) throws InvalidValueException {
102         if (s.startsWith((rangePrefix!=null)?rangePrefix:PREFIX)) {
103             Long firstByte=null, lastByte = null, byteLength = null;
104             String[] params = s.substring( ((rangePrefix!=null)?rangePrefix:PREFIX).length()).split("[-/]");
105             switch (params.length) {
106                 case 3:
107                     if (params[2].length() != 0 && !params[2].equals("*")) {
108                         byteLength = Long.parseLong(params[2]);
109                     }
110                 case 2:
111                     if (params[1].length() != 0) {
112                         lastByte = Long.parseLong(params[1]);
113                     }
114                 case 1:
115                     if (params[0].length() != 0) {
116                         firstByte = Long.parseLong(params[0]);
117                     }
118                     if (firstByte!=null || lastByte!= null)
119                         return new BytesRange(firstByte, lastByte, byteLength);
120                 default:
121                     break;
122             }
123         }
124 
125         throw new InvalidValueException("Can't parse Bytes Range: " + s);
126     }
127 }