1 16 package org.apache.cocoon.components.midi.xmidi; 17 18 27 28 import org.apache.cocoon.ProcessingException; 29 30 public final class Utils 31 { 32 public static final String VERSION = "1.2"; 33 34 37 static public byte[] hexToBa(String h, int len) throws ProcessingException 38 { 39 int l = h.length(); 40 int cnt = 0; 41 int bs = 0; 42 int bc = 0; 43 byte[] ba = new byte[len]; 44 for (int i = 0; i < l; i++) 45 { 46 String s = h.substring(i, i + 1); 47 int x = " \n\t\r".indexOf(s); 48 if (x != -1) 49 { 50 continue; 51 } 52 int tmp = "0123456789ABCDEF".indexOf(s.toUpperCase()); 53 if (bc == 0) 54 { 55 bs = tmp; 56 } 57 else 58 if (bc == 1) 59 { 60 bs <<= 4; 61 bs |= tmp; 62 } 63 else 64 { 65 throw new ProcessingException("hexToBa: internal error"); 66 } 67 bc++; 68 if (bc >= 2) 69 { 70 ba[cnt] = (byte) bs; 71 cnt++; 72 bc = 0; 73 } 74 } 75 if (bc != 0) 76 { 77 throw new ProcessingException("un-even number of hex digits"); 78 } 79 if (cnt != len) 80 { 81 throw new ProcessingException( 82 "hexToBa: count (" + cnt + ") != length (" + len + ")"); 83 } 84 return ba; 85 } 86 87 90 public static String baToString(byte[] b, int strt, int end) 91 { 92 int l = end - strt + 1; 93 char[] c = new char[l]; 94 for (int j = 0; j < l; j++) 95 { 96 c[j] = (char) b[j]; 97 } 98 return new String (c); 99 } 100 101 104 public static String baToHex(byte[] b, int strt, int end) 105 throws ProcessingException 106 { 107 int l = end - strt + 1; 108 if (b.length < end) 109 { 110 throw new ProcessingException( 111 "baToHex: length error; b.length=" 112 + b.length 113 + ", strt=" 114 + strt 115 + ", end=" 116 + end 117 + ", l=" 118 + l); 119 } 120 StringBuffer sb = new StringBuffer (""); 121 for (int i = 0; i < l; i++) 122 { 123 int t = getUnsignedByte(b[strt + i]); 124 int a = t / 16; 125 int aa = t % 16; 126 sb.append("0123456789ABCDEF".substring(a, a + 1)); 127 sb.append("0123456789ABCDEF".substring(aa, aa + 1)); 128 } 129 return sb.toString(); 130 } 131 132 135 static public byte[] intToBa(int n, int c) throws ProcessingException 136 { 137 byte[] b = new byte[c]; 138 int t = n; 139 for (int i = 0; i < c; i++) 140 { 141 int j = c - i - 1; 142 int k = t % 256; 143 b[j] = (byte) k; 144 t /= 256; 145 } 146 if (t > 0) 147 { 148 throw new ProcessingException( 149 "intToBa: t is " + t + ", n = " + n + ", c = " + c); 150 } 151 return b; 152 } 153 154 157 static public int baToInt(byte[] b, int strt, int end) 158 throws ProcessingException 159 { 160 if (end > b.length - 1) 161 { 162 throw new ProcessingException( 163 "baToInt: strt = " 164 + strt 165 + ", end = " 166 + end 167 + ", b.length = " 168 + b.length); 169 } 170 int l = end - strt + 1; 171 int i = 0; 172 for (int j = 0; j < l; j++) 173 { 174 int p = strt + l - j - 1; 175 if (p > b.length - 1) 177 { 178 throw new ProcessingException( 179 "baToInt: p = " 180 + p 181 + ", strt = " 182 + strt 183 + ", end = " 184 + end 185 + ", l = " 186 + l 187 + ", j = " 188 + j 189 + ", i = " 190 + i); 191 } 192 int k = getUnsignedByte(b[p]); 193 int n = pow(256, j); 194 i += n * k; 195 } 196 return i; 197 } 198 199 202 static public int getUnsignedByte(byte b) 203 { 204 int t = 0; 205 if ((b & 128) == 128) 206 { 207 t = 1; 208 } 209 b &= 127; 210 int k = b; 211 k += t * 128; 212 return k; 213 } 214 215 220 static public ByteLen deltaToInt(byte[] b, int offset) 221 throws ProcessingException 222 { 223 230 int j = 0; 231 byte[] ba = new byte[4]; 232 boolean jFlag = true; 233 while (jFlag) 234 { 235 if ((j + offset) > b.length) 236 { 237 throw new ProcessingException( 238 "deltaToInt: length error; j = " 239 + j 240 + ", offset = " 241 + offset 242 + ", b.length = " 243 + b.length); 244 } 245 ba[j] = b[j + offset]; 246 if (ba[j] >= 0) 247 { 248 jFlag = false; 249 } 250 else 251 { 252 ba[j] &= 127; 253 } 254 256 j++; 257 } 258 int s = 0; 259 for (int i = 0; i < j; i++) 260 { 261 int k = j - i - 1; 262 int p = pow(128, i); 263 int m = ba[k]; 264 s += m * p; 265 278 } 279 280 ByteLen bl = new ByteLen(intToBa(s, 4), j); 281 return bl; 282 } 283 284 287 static public int pow(int b, int e) 288 { 289 int a = 1; 290 for (int i = 0; i < e; i++) 291 { 292 a *= b; 293 } 294 return a; 295 } 296 297 public static int getPW(byte[] dta, int offset) throws ProcessingException 298 { 299 int hi = baToInt(dta, offset, offset); 300 int lo = baToInt(dta, offset + 1, offset + 1); 301 hi <<= 7; 302 lo |= hi; 303 return lo; 304 } 305 306 public static int getNextHiOrd(byte[] dta, int offset) 307 throws ProcessingException 308 { 309 int ol = 0; 310 boolean tflag = true; 311 for (int o = offset + 1; o < dta.length - 1; o++) 312 { 313 if (tflag) 314 { 315 int x = baToInt(dta, o, o); 316 if ((x & 128) == 128) 317 { 318 tflag = false; 319 ol = o; 320 } 321 } 322 } 323 if (tflag) 324 { 325 ol = offset + dta.length; 326 } 327 return ol; 328 } 329 330 335 public static byte[] intToDelta(byte[] t) throws ProcessingException 336 { 337 350 int i1 = baToInt(t, 0, t.length - 1); 351 if (i1 == 0) 353 { 354 byte[] b = new byte[1]; 355 b[0] = 0; 356 return b; 357 } 358 int i2 = i1; 359 byte[] b1 = new byte[4]; 360 for (int i = 0; i < 4; i++) 361 { 362 int j = 4 - i - 1; 363 int k = i2 % 128; 364 i2 /= 128; 365 b1[j] = (byte) k; 366 } 367 int j = -1; 368 boolean bFlag = false; 369 for (int i = 0; i < 4; i++) 370 { 371 if (bFlag) 372 { 373 continue; 374 } 375 if (b1[i] != 0) 376 { 377 j = i; 378 bFlag = true; 379 } 380 } 381 int k = 4 - j; 382 byte[] b2 = new byte[k]; 383 for (int i = 0; i < k; i++) 384 { 385 b2[i] = b1[i + j]; 386 if (i != (k - 1)) 387 { 388 b2[i] |= 128; 389 } 390 } 391 return b2; 392 } 393 394 public static int stringToInt(String s) 395 { 396 Integer i = new Integer (s); 397 int r = i.intValue(); 398 return r; 399 } 400 401 } 402 | Popular Tags |