| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.jaudiotagger.audio.generic; |
| 20 | |
|
| 21 | |
import org.jaudiotagger.audio.AudioFile; |
| 22 | |
|
| 23 | |
import java.io.*; |
| 24 | |
import java.nio.ByteBuffer; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | 0 | public class Utils |
| 33 | |
{ |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public static void copy(byte[] src, byte[] dst, int dstOffset) |
| 44 | |
{ |
| 45 | 0 | System.arraycopy(src, 0, dst, dstOffset, src.length); |
| 46 | 0 | } |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public static byte[] getDefaultBytes(String s, String charSet) |
| 56 | |
{ |
| 57 | |
try |
| 58 | |
{ |
| 59 | 3779 | return s.getBytes(charSet); |
| 60 | |
} |
| 61 | 0 | catch (UnsupportedEncodingException uee) |
| 62 | |
{ |
| 63 | 0 | throw new RuntimeException(uee); |
| 64 | |
} |
| 65 | |
|
| 66 | |
} |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public static String getExtension(File f) |
| 77 | |
{ |
| 78 | 475 | String name = f.getName().toLowerCase(); |
| 79 | 475 | int i = name.lastIndexOf("."); |
| 80 | 475 | if (i == -1) |
| 81 | |
{ |
| 82 | 0 | return ""; |
| 83 | |
} |
| 84 | |
|
| 85 | 475 | return name.substring(i + 1); |
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
public static long getLongLE(ByteBuffer b, int start, int end) |
| 102 | |
{ |
| 103 | 13743 | long number = 0; |
| 104 | 68637 | for (int i = 0; i < (end - start + 1); i++) |
| 105 | |
{ |
| 106 | 54894 | number += ((b.get(start + i) & 0xFF) << i * 8); |
| 107 | |
} |
| 108 | |
|
| 109 | 13743 | return number; |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
public static long getLongBE(ByteBuffer b, int start, int end) |
| 120 | |
{ |
| 121 | 30813 | int number = 0; |
| 122 | 148487 | for (int i = 0; i < (end - start + 1); i++) |
| 123 | |
{ |
| 124 | 117674 | number += ((b.get(end - i) & 0xFF) << i * 8); |
| 125 | |
} |
| 126 | |
|
| 127 | 30813 | return number; |
| 128 | |
} |
| 129 | |
|
| 130 | |
public static int getIntLE(byte[] b) |
| 131 | |
{ |
| 132 | 462 | return (int) getLongLE(ByteBuffer.wrap(b), 0, b.length - 1); |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
public static int getIntLE(byte[] b, int start, int end) |
| 143 | |
{ |
| 144 | 13281 | return (int) getLongLE(ByteBuffer.wrap(b), start, end); |
| 145 | |
} |
| 146 | |
|
| 147 | |
public static int getIntBE(byte[] b, int start, int end) |
| 148 | |
{ |
| 149 | 19626 | return (int) getLongBE(ByteBuffer.wrap(b), start, end); |
| 150 | |
} |
| 151 | |
|
| 152 | |
public static int getIntBE(ByteBuffer b, int start, int end) |
| 153 | |
{ |
| 154 | 11187 | return (int) getLongBE(b, start, end); |
| 155 | |
} |
| 156 | |
|
| 157 | |
public static short getShortBE(ByteBuffer b, int start, int end) |
| 158 | |
{ |
| 159 | 541 | return (short) getIntBE(b, start, end); |
| 160 | |
} |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
public static byte[] getSizeBEInt32(int size) |
| 169 | |
{ |
| 170 | 7133 | byte[] b = new byte[4]; |
| 171 | 7133 | b[0] = (byte) ((size >> 24) & 0xFF); |
| 172 | 7133 | b[1] = (byte) ((size >> 16) & 0xFF); |
| 173 | 7133 | b[2] = (byte) ((size >> 8) & 0xFF); |
| 174 | 7133 | b[3] = (byte) (size & 0xFF); |
| 175 | 7133 | return b; |
| 176 | |
} |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
public static byte[] getSizeBEInt16(short size) |
| 185 | |
{ |
| 186 | 246 | byte[] b = new byte[2]; |
| 187 | 246 | b[0] = (byte) ((size >> 8) & 0xFF); |
| 188 | 246 | b[1] = (byte) (size & 0xFF); |
| 189 | 246 | return b; |
| 190 | |
} |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
public static byte[] getSizeLEInt32(int size) |
| 200 | |
{ |
| 201 | 78 | byte[] b = new byte[4]; |
| 202 | 78 | b[0] = (byte) (size & 0xff); |
| 203 | 78 | b[1] = (byte) ((size >>> 8) & 0xffL); |
| 204 | 78 | b[2] = (byte) ((size >>> 16) & 0xffL); |
| 205 | 78 | b[3] = (byte) ((size >>> 24) & 0xffL); |
| 206 | 78 | return b; |
| 207 | |
} |
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
public static String getString(byte[] b, int offset, int length, String encoding) |
| 220 | |
{ |
| 221 | |
try |
| 222 | |
{ |
| 223 | 20169 | return new String(b, offset, length, encoding); |
| 224 | |
} |
| 225 | 0 | catch (UnsupportedEncodingException ue) |
| 226 | |
{ |
| 227 | |
|
| 228 | 0 | throw new RuntimeException(ue); |
| 229 | |
} |
| 230 | |
} |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
public static String getString(ByteBuffer buffer, int offset, int length, String encoding) |
| 243 | |
{ |
| 244 | 4964 | byte[] b = new byte[length]; |
| 245 | 4964 | buffer.position(buffer.position() + offset); |
| 246 | 4964 | buffer.get(b); |
| 247 | |
try |
| 248 | |
{ |
| 249 | 4964 | return new String(b, 0, length, encoding); |
| 250 | |
} |
| 251 | 0 | catch (UnsupportedEncodingException uee) |
| 252 | |
{ |
| 253 | |
|
| 254 | 0 | throw new RuntimeException(uee); |
| 255 | |
} |
| 256 | |
} |
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
public static byte[] getUTF8Bytes(String s) throws UnsupportedEncodingException |
| 266 | |
{ |
| 267 | 76 | return s.getBytes("UTF-8"); |
| 268 | |
} |
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
public static int readUint32AsInt(DataInput di) throws IOException |
| 274 | |
{ |
| 275 | 98 | final long l = readUint32(di); |
| 276 | 98 | if (l > Integer.MAX_VALUE) |
| 277 | |
{ |
| 278 | 0 | throw new IOException("uint32 value read overflows int"); |
| 279 | |
} |
| 280 | 98 | return (int) l; |
| 281 | |
} |
| 282 | |
|
| 283 | |
public static long readUint32(DataInput di) throws IOException |
| 284 | |
{ |
| 285 | 202 | final byte[] buf8 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 286 | 202 | di.readFully(buf8, 4, 4); |
| 287 | 202 | final long l = ByteBuffer.wrap(buf8).getLong(); |
| 288 | 202 | return l; |
| 289 | |
} |
| 290 | |
|
| 291 | |
public static int readUint16(DataInput di) throws IOException |
| 292 | |
{ |
| 293 | 91 | final byte[] buf = {0x00, 0x00, 0x00, 0x00}; |
| 294 | 91 | di.readFully(buf, 2, 2); |
| 295 | 91 | final int i = ByteBuffer.wrap(buf).getInt(); |
| 296 | 91 | return i; |
| 297 | |
} |
| 298 | |
|
| 299 | |
public static String readString(DataInput di, int charsToRead) throws IOException |
| 300 | |
{ |
| 301 | 137 | final byte[] buf = new byte[charsToRead]; |
| 302 | 137 | di.readFully(buf); |
| 303 | 137 | return new String(buf); |
| 304 | |
} |
| 305 | |
|
| 306 | |
public static long readUInt64(ByteBuffer b) |
| 307 | |
{ |
| 308 | 0 | long result = 0; |
| 309 | 0 | result += (readUBEInt32(b) << 32); |
| 310 | 0 | result += readUBEInt32(b); |
| 311 | 0 | return result; |
| 312 | |
} |
| 313 | |
|
| 314 | |
public static int readUBEInt32(ByteBuffer b) |
| 315 | |
{ |
| 316 | 64 | int result = 0; |
| 317 | 64 | result += readUBEInt16(b) << 16; |
| 318 | 64 | result += readUBEInt16(b); |
| 319 | 64 | return result; |
| 320 | |
} |
| 321 | |
|
| 322 | |
public static int readUBEInt24(ByteBuffer b) |
| 323 | |
{ |
| 324 | 0 | int result = 0; |
| 325 | 0 | result += readUBEInt16(b) << 16; |
| 326 | 0 | result += readUInt8(b); |
| 327 | 0 | return result; |
| 328 | |
} |
| 329 | |
|
| 330 | |
public static int readUBEInt16(ByteBuffer b) |
| 331 | |
{ |
| 332 | 144 | int result = 0; |
| 333 | 144 | result += readUInt8(b) << 8; |
| 334 | 144 | result += readUInt8(b); |
| 335 | 144 | return result; |
| 336 | |
} |
| 337 | |
|
| 338 | |
public static int readUInt8(ByteBuffer b) |
| 339 | |
{ |
| 340 | 384 | return read(b); |
| 341 | |
} |
| 342 | |
|
| 343 | |
|
| 344 | |
public static int read(ByteBuffer b) |
| 345 | |
{ |
| 346 | 384 | int result = (b.get() & 0xFF); |
| 347 | 384 | return result; |
| 348 | |
} |
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
public static String getMinBaseFilenameAllowedForTempFile(File file) |
| 357 | |
{ |
| 358 | 126 | String s = AudioFile.getBaseFilename(file); |
| 359 | 126 | if(s.length()>=3) |
| 360 | |
{ |
| 361 | 125 | return s; |
| 362 | |
} |
| 363 | 1 | if(s.length()==1) |
| 364 | |
{ |
| 365 | 0 | return s + "000"; |
| 366 | |
} |
| 367 | 1 | else if(s.length()==1) |
| 368 | |
{ |
| 369 | 0 | return s + "00"; |
| 370 | |
} |
| 371 | 1 | else if(s.length()==2) |
| 372 | |
{ |
| 373 | 1 | return s + "0"; |
| 374 | |
} |
| 375 | 0 | return s; |
| 376 | |
} |
| 377 | |
} |