001    package helliker.id3;
002    
003    /*
004      Copyright (C) 2001,2002 Jonathan Hilliker
005    
006      This library is free software; you can redistribute it and/or
007      modify it under the terms of the GNU Lesser General Public
008      License as published by the Free Software Foundation; either
009      version 2.1 of the License, or (at your option) any later version.
010    
011      This library is distributed in the hope that it will be useful,
012      but WITHOUT ANY WARRANTY; without even the implied warranty of
013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014      Lesser General Public License for more details.
015    
016      You should have received a copy of the GNU Lesser General Public
017      License along with this library; if not, write to the Free Software
018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019     */
020    /**
021     *  This class contains serveral utility functions for converting, modifying,
022     *  and testing bytes.
023     */
024    
025    public final class BinaryParser  {
026      /**???
027      */
028      private static final int NUM_BYTES = 4;
029    
030      /**???
031       */
032      private static final int NUM_BITS = 8;
033    
034      /**???
035       */
036      private static final int SYNCHSAFE_BITS = 7;
037    
038      /**
039       * Tests to see if the bit at postion pos is set in byte b
040       *
041       * @param b the byte to test
042       * @param pos a value between 0 (least significant bit) and 7
043       *            (most significant bit) indicating the position to test
044       * @return true if the bit at the location is set
045       */
046      public static boolean bitSet(byte b, int pos) {
047        boolean retval = false;
048    
049        if ((pos >= 0) && (pos < NUM_BITS)) {
050          retval = ((b & (byte) (1 << pos)) != 0);
051        }
052    
053        return retval;
054      }
055    
056      /**
057       * This function takes an 8 character string representation of a byte
058       * in binary mode.  It will match 0's and 1's at the locations specified
059       * and skip other characters (x is the standard wildcard).  If the values
060       * of the bits at the locations where a 0 is set is 0 and values of the
061       * bits at the locations where 1 is set is 1, then the function will
062       * return true.
063       *
064       * @param b the byte to test
065       * @param pattern the 8 character long pattern to test the byte
066       * @return true if the bits at the locations specified are set
067       */
068      public static boolean matchPattern(byte b, String pattern) {
069        boolean retval = true;
070    
071        for (int i = 0; (i < NUM_BITS) && (i < pattern.length()) && retval; i++) {
072          if (pattern.charAt(i) == '1') {
073            retval = retval && bitSet (b, NUM_BITS - i - 1);
074          } else if (pattern.charAt(i) == '0') {
075            retval = retval && !bitSet(b, NUM_BITS - i - 1);
076          }
077        }
078    
079        return retval;
080      }
081    
082      /**
083       * Convert a portion of a byte to an integer.  This assumes that the length
084       * of the byte is end - start bits long with end being the most
085       * significant bit.  The start and end values must be between 0 and 7.  If
086       * the start and end values are incorrect, it will set start to 0 and end
087       * to 7.
088       *
089       * @param b the byte to convert from
090       * @param start the starting bit
091       * @param end the ending bit
092       * @return the converted value (unsigned)
093       */
094      public static int convertToDecimal(byte b, int start, int end) {
095        byte ret = 0;
096        int bCount = 0;
097        int s = start;
098        int e = end;
099    
100        if ((start < 0) || (start >= NUM_BITS)) {
101          s = 0;
102        }
103    
104        if ((end < 0) || (end >= NUM_BITS)) {
105          e = NUM_BITS - 1;
106        }
107    
108        if (start > end) {
109          s = end;
110          e = start;
111        }
112    
113        for (int i = s; i <= e; i++) {
114          if (bitSet(b, i)) {
115            ret = setBit(ret, bCount);
116          }
117    
118          bCount++;
119        }
120    
121        return ret;
122      }
123    
124      /**
125       * Convert an array of bytes into an integer.  The array must not contain
126       * more than 4 bytes (32 bits).  This assumes that the most significant
127       * byte is in the first index of the array.  If an array is not 4 bytes
128       * long, then it will assume that the bytes are preceded by leading 0's.
129       *
130       * @param b the array of bytes
131       * @return the converted integer in decimal
132       */
133      public static int convertToInt(byte[] b) {
134        int retval = 0;
135        int pos = 0;
136        int start = b.length - 1;
137    
138        if (start >= NUM_BYTES) {
139          start = NUM_BYTES - 1;
140        }
141    
142    
143        for (int i = start; i >= 0; i--) {
144          for (int j = 0; j < NUM_BITS; j++) {
145            if (bitSet(b[i], j)) {
146              retval += Math.pow(2, pos);
147            }
148    
149            pos++;
150          }
151        }
152    
153        return retval;
154      }
155    
156      /**
157       * Converts the byte array to a synchsafe integer as specified in section
158       * 6.2 of the id3v2 specification.  Works exactly same as the covnertToInt
159       * method except that it only calculates using the first 7 bits of each
160       * byte.
161       *
162       * @param b the array of bytes
163       * @return the converted integer in decimal
164       */
165      public static int convertToSynchsafeInt(byte[] b) {
166        int retval = 0;
167        int pos = 0;
168        int start = b.length - 1;
169    
170        if (start >= NUM_BYTES) {
171          start = NUM_BYTES - 1;
172        }
173    
174        for (int i = start; i >= 0; i--) {
175          for (int j = 0; j < SYNCHSAFE_BITS; j++) {
176            if (bitSet(b[i], j)) {
177              retval += Math.pow(2, pos);
178            }
179    
180            pos++;
181          }
182        }
183    
184        return retval;
185      }
186    
187      /**
188       * Convert the integer passed to a array of 4 bytes (32-bits).  Does the
189       * opposite of the convertToInt method.
190       *
191       * @param num the integer to convert
192       * @return the integer converted to a byte array
193       */
194      public static byte[] convertToBytes(int num) {
195        byte[] b = new byte[NUM_BYTES];
196        int count = num;
197        boolean done = false;
198    
199        for (int i = b.length - 1; (i >= 0) && !done; i--) {
200          for (int j = 0; (j < NUM_BITS) && !done; j++) {
201            if ((count % 2) == 1) {
202              b[i] = setBit(b[i], j);
203            }
204    
205            count = count / 2;
206            done = (count == 0);
207          }
208        }
209    
210        return b;
211      }
212    
213      /**
214       * Converts the integer passed to an array of bytes as specified in
215       * section 6.2 of the id3v2 specification.  Works the same as the
216       * convertToBytes method except the most significant bit of each byte is
217       * zeroed.
218       *
219       * @param num the integer to convert
220       * @return the integer converted to a byte array
221       */
222      public static byte[] convertToSynchsafeBytes(int num) {
223        byte[] b = new byte[NUM_BYTES];
224        int count = num;
225        boolean done = false;
226    
227        for (int i = b.length - 1; (i >= 0) && !done; i--) {
228          for (int j = 0; (j < SYNCHSAFE_BITS) && !done; j++) {
229            if ((count % 2) == 1) {
230              b[i] = setBit(b[i], j);
231            }
232    
233            count = count / 2;
234            done = (count == 0);
235          }
236        }
237    
238        return b;
239      }
240    
241      /**
242       * Sets the bit at the specified location in the byte given.  Location
243       * should be between 0 and 7.  If an invalid location is specified, 0
244       * will be returned.
245       *
246       * @param b the byte to set the index in
247       * @param location the index to set in b
248       * @return b with the bit at location set to 1
249       */
250      public static byte setBit(byte b, int location) {
251        byte ret = 0;
252    
253        if ((location >= 0) && (location < NUM_BITS)) {
254          ret = (byte) (b | (byte) (1 << location));
255        }
256    
257        return ret;
258      }
259    
260    }// BinaryParser