1 /***
2 * Java DAB EPG API - Serialize/Deserialize To/From POJOs to XML/Binary as per
3 * ETSI specifications TS 102 818 (XML Specification for DAB EPG) and TS 102
4 * 371 (Transportation and Binary Encoding Specification for EPG).
5 *
6 * Copyright (C) 2007 GCap Media PLC
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22 package com.gcapmedia.dab.epg.binary;
23
24 import java.math.BigInteger;
25
26 import org.apache.commons.lang.StringUtils;
27
28 /***
29 *
30 */
31 public class BitParser {
32
33 /***
34 * Binary string
35 */
36 private String binary;
37
38 /***
39 * Create a new bithelper
40 * @param size
41 */
42 public BitParser(byte[] bytes) {
43 BigInteger big = new BigInteger(1, bytes);
44 binary = StringUtils.leftPad(big.toString(2), bytes.length * 8, '0');
45 }
46
47 /***
48 * Returns the size of the construct
49 * @return the size of the construct
50 */
51 public int size() {
52 return binary.length();
53 }
54
55 /***
56 *
57 */
58 public int getInt(int index, int length) {
59 String toParse = binary.substring(index, index + length);
60 return Integer.parseInt(toParse, 2);
61 }
62
63 /***
64 *
65 */
66 public long getLong(int index, int length) {
67 String toParse = binary.substring(index, index + length);
68 return Long.parseLong(toParse, 2);
69 }
70
71 /***
72 *
73 */
74 public boolean getBoolean(int index) {
75 return binary.charAt(index) == '1';
76 }
77
78 public byte getByte(int index) {
79 byte[] bytes = getByteArray(index, 8);
80 return bytes[0];
81 }
82
83 /***
84 *
85 */
86 public byte[] getByteArray(int index, int length) {
87 if(index + length > size()) {
88 throw new ArrayIndexOutOfBoundsException("Requested array (" + index + "->" + (index + length) + ") is beyond the bounds (" + size() + ")");
89 }
90 String tmp = binary.substring(index, index + length);
91 byte[] bytes = new byte[(int)Math.ceil(((double)tmp.length()) / 8)];
92 for(int i = 0; i < tmp.length(); i +=8) {
93 int end = i + 8;
94 if(end > tmp.length()) {
95 end = tmp.length();
96 }
97 String chunk = tmp.substring(i, end);
98 long lng = Long.parseLong(chunk, 2);
99 bytes[i / 8] = (byte)lng;
100 }
101 return bytes;
102 }
103
104 /***
105 *
106 */
107 public static byte[] parseByteArray(String hex) {
108
109 hex = hex.replaceAll(" ", "");
110 byte[] bytes = new byte[hex.length() / 2];
111 for(int i = 0; i < hex.length(); i+=2) {
112 int num = Integer.parseInt(hex.substring(i, i+2), 16);
113 bytes[i / 2] = (byte)num;
114 }
115 return bytes;
116 }
117
118 /***
119 * @see java.lang.Object#toString()
120 */
121 public String toString() {
122 return "BitParser [" + size() + "]: " + binary;
123 }
124
125 }