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.support.model.dlna.types;
16  
17  import java.util.regex.Matcher;
18  import java.util.regex.Pattern;
19  import org.fourthline.cling.model.types.InvalidValueException;
20  import org.fourthline.cling.support.model.dlna.types.CodedDataBuffer.TransferMechanism;
21  
22  /**
23   *
24   * @author Mario Franco
25   */
26  public class BufferInfoType {
27  
28      final static Pattern pattern = Pattern.compile("^dejitter=(\\d{1,10})(;CDB=(\\d{1,10});BTM=(0|1|2))?(;TD=(\\d{1,10}))?(;BFR=(0|1))?$", Pattern.CASE_INSENSITIVE);
29      private Long dejitterSize;
30      private CodedDataBuffer cdb;
31      private Long targetDuration;
32      private Boolean fullnessReports;
33  
34      public BufferInfoType(Long dejitterSize) {
35          this.dejitterSize = dejitterSize;
36      }
37  
38      public BufferInfoType(Long dejitterSize, CodedDataBuffer cdb,
39              Long targetDuration, Boolean fullnessReports) {
40          this.dejitterSize = dejitterSize;
41          this.cdb = cdb;
42          this.targetDuration = targetDuration;
43          this.fullnessReports = fullnessReports;
44      }
45  
46      public static BufferInfoType valueOf(String s) throws InvalidValueException {
47          Matcher matcher = pattern.matcher(s);
48          if (matcher.matches()) {
49              try {
50                  Long dejitterSize = Long.parseLong(matcher.group(1));
51                  CodedDataBuffer cdb = null;
52                  Long targetDuration = null;
53                  Boolean fullnessReports = null;
54  
55                  if (matcher.group(2) != null) {
56                      cdb = new CodedDataBuffer(Long.parseLong(matcher.group(3)),
57                              TransferMechanism.values()[Integer.parseInt(matcher.group(4))]);
58                  }
59                  if (matcher.group(5) != null) {
60                      targetDuration = Long.parseLong(matcher.group(6));
61                  }
62                  if (matcher.group(7) != null) {
63                      fullnessReports = matcher.group(8).equals("1");
64                  }
65                  return new BufferInfoType(dejitterSize, cdb, targetDuration, fullnessReports);
66              } catch (NumberFormatException ex1) {
67              }
68          }
69          throw new InvalidValueException("Can't parse BufferInfoType: " + s);
70      }
71  
72      public String getString() {
73          String s = "dejitter=" + dejitterSize.toString();
74          if (cdb != null) {
75              s += ";CDB=" + cdb.getSize().toString() + ";BTM=" + cdb.getTranfer().ordinal();
76          }
77          if (targetDuration != null) {
78              s += ";TD=" + targetDuration.toString();
79          }
80          if (fullnessReports != null) {
81              s += ";BFR=" + (fullnessReports?"1":"0");
82          }
83          return s;
84      }
85  
86      /**
87       * @return the dejitter size
88       */
89      public Long getDejitterSize() {
90          return dejitterSize;
91      }
92  
93      /**
94       * @return the cdb
95       */
96      public CodedDataBuffer getCdb() {
97          return cdb;
98      }
99  
100     /**
101      * @return the targetDuration
102      */
103     public Long getTargetDuration() {
104         return targetDuration;
105     }
106 
107     /**
108      * @return the fullnessReports
109      */
110     public Boolean isFullnessReports() {
111         return fullnessReports;
112     }
113 }