1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.fourthline.cling.model.types;
16
17
18
19
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
43
44 public Long getFirstByte() {
45 return firstByte;
46 }
47
48
49
50
51 public Long getLastByte() {
52 return lastByte;
53 }
54
55
56
57
58 public Long getByteLength() {
59 return byteLength;
60 }
61
62
63
64
65
66 public String getString() {
67 return getString(false,null);
68 }
69
70
71
72
73
74 public String getString(boolean includeDuration) {
75 return getString(includeDuration,null);
76 }
77
78
79
80
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 }