2
* Entagged Audio Tag library
3
* Copyright (c) 2003-2005 Rapha�l Slinckx <raphael@slinckx.net>
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
package org.jaudiotagger.audio.generic;
21
import org.jaudiotagger.audio.AudioFile;
24
import java.nio.ByteBuffer;
27
* Contains various frequently used static functions in the different tag
30
* @author Raphael Slinckx
36
* Copies the bytes of <code>srd</code> to <code>dst</code> at the
39
* @param src The byte to be copied.
40
* @param dst The array to copy to
41
* @param dstOffset The start offset for the bytes to be copied.
43
public static void copy(byte[] src, byte[] dst, int dstOffset)
45
System.arraycopy(src, 0, dst, dstOffset, src.length);
50
* Returns {@link String#getBytes()}.<br>
52
* @param s The String to call, decode bytes using the specfied charset
55
public static byte[] getDefaultBytes(String s, String charSet)
59
return s.getBytes(charSet);
61
catch (UnsupportedEncodingException uee)
63
throw new RuntimeException(uee);
69
* Returns the extension of the given file.
70
* The extension is empty if there is no extension
71
* The extension is the string after the last "."
73
* @param f The file whose extension is requested
74
* @return The extension of the given file
76
public static String getExtension(File f)
78
String name = f.getName().toLowerCase();
79
int i = name.lastIndexOf(".");
85
return name.substring(i + 1);
90
* Computes a number whereby the 1st byte is the least signifcant and the last
91
* byte is the most significant.
93
* @param b The byte array @param start The starting offset in b
94
* (b[offset]). The less significant byte @param end The end index
95
* (included) in b (b[end]). The most significant byte @return a long number
96
* represented by the byte sequence.
98
* So if storing a number which only requires one byte it will be stored in the first
101
public static long getLongLE(ByteBuffer b, int start, int end)
104
for (int i = 0; i < (end - start + 1); i++)
106
number += ((b.get(start + i) & 0xFF) << i * 8);
113
* Computes a number whereby the 1st byte is the most significant and the last
114
* byte is the least significant.
116
* So if storing a number which only requires one byte it will be stored in the last
119
public static long getLongBE(ByteBuffer b, int start, int end)
122
for (int i = 0; i < (end - start + 1); i++)
124
number += ((b.get(end - i) & 0xFF) << i * 8);
130
public static int getIntLE(byte[] b)
132
return (int) getLongLE(ByteBuffer.wrap(b), 0, b.length - 1);
136
* same as above, but returns an int instead of a long @param b The byte
137
* array @param start The starting offset in b (b[offset]). The less
138
* significant byte @param end The end index (included) in b (b[end]). The
139
* most significant byte @return a int number represented by the byte
142
public static int getIntLE(byte[] b, int start, int end)
144
return (int) getLongLE(ByteBuffer.wrap(b), start, end);
147
public static int getIntBE(byte[] b, int start, int end)
149
return (int) getLongBE(ByteBuffer.wrap(b), start, end);
152
public static int getIntBE(ByteBuffer b, int start, int end)
154
return (int) getLongBE(b, start, end);
157
public static short getShortBE(ByteBuffer b, int start, int end)
159
return (short) getIntBE(b, start, end);
163
* Convert int to byte representation - Big Endian (as used by mp4)
166
* @return byte represenetation
168
public static byte[] getSizeBEInt32(int size)
170
byte[] b = new byte[4];
171
b[0] = (byte) ((size >> 24) & 0xFF);
172
b[1] = (byte) ((size >> 16) & 0xFF);
173
b[2] = (byte) ((size >> 8) & 0xFF);
174
b[3] = (byte) (size & 0xFF);
179
* Convert short to byte representation - Big Endian (as used by mp4)
182
* @return byte represenetation
184
public static byte[] getSizeBEInt16(short size)
186
byte[] b = new byte[2];
187
b[0] = (byte) ((size >> 8) & 0xFF);
188
b[1] = (byte) (size & 0xFF);
194
* Convert int to byte representation - Little Endian (as used by ogg vorbis)
197
* @return byte represenetation
199
public static byte[] getSizeLEInt32(int size)
201
byte[] b = new byte[4];
202
b[0] = (byte) (size & 0xff);
203
b[1] = (byte) ((size >>> 8) & 0xffL);
204
b[2] = (byte) ((size >>> 16) & 0xffL);
205
b[3] = (byte) ((size >>> 24) & 0xffL);
210
* Create String starting from offset upto length using encoding
217
* @throws UnsupportedEncodingException
219
public static String getString(byte[] b, int offset, int length, String encoding)
223
return new String(b, offset, length, encoding);
225
catch (UnsupportedEncodingException ue)
227
//Shouldnt have to worry about this exception as should only be calling with well defined charsets
228
throw new RuntimeException(ue);
233
* Create String offset from position by offset upto length using encoding, and position of buffer
234
* is moved to after position + offset + length
242
public static String getString(ByteBuffer buffer, int offset, int length, String encoding)
244
byte[] b = new byte[length];
245
buffer.position(buffer.position() + offset);
249
return new String(b, 0, length, encoding);
251
catch (UnsupportedEncodingException uee)
253
//TODO, will we ever use unsupported encodings
254
throw new RuntimeException(uee);
259
* Tries to convert a string into an UTF8 array of bytes If the conversion
260
* fails, return the string converted with the default encoding.
262
* @param s The string to convert @return The byte array representation of
263
* this string in UTF8 encoding
265
public static byte[] getUTF8Bytes(String s) throws UnsupportedEncodingException
267
return s.getBytes("UTF-8");
271
* Overflow checking since java can't handle unsigned numbers.
273
public static int readUint32AsInt(DataInput di) throws IOException
275
final long l = readUint32(di);
276
if (l > Integer.MAX_VALUE)
278
throw new IOException("uint32 value read overflows int");
283
public static long readUint32(DataInput di) throws IOException
285
final byte[] buf8 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
286
di.readFully(buf8, 4, 4);
287
final long l = ByteBuffer.wrap(buf8).getLong();
291
public static int readUint16(DataInput di) throws IOException
293
final byte[] buf = {0x00, 0x00, 0x00, 0x00};
294
di.readFully(buf, 2, 2);
295
final int i = ByteBuffer.wrap(buf).getInt();
299
public static String readString(DataInput di, int charsToRead) throws IOException
301
final byte[] buf = new byte[charsToRead];
303
return new String(buf);
306
public static long readUInt64(ByteBuffer b)
309
result += (readUBEInt32(b) << 32);
310
result += readUBEInt32(b);
314
public static int readUBEInt32(ByteBuffer b)
317
result += readUBEInt16(b) << 16;
318
result += readUBEInt16(b);
322
public static int readUBEInt24(ByteBuffer b)
325
result += readUBEInt16(b) << 16;
326
result += readUInt8(b);
330
public static int readUBEInt16(ByteBuffer b)
333
result += readUInt8(b) << 8;
334
result += readUInt8(b);
338
public static int readUInt8(ByteBuffer b)
344
public static int read(ByteBuffer b)
346
int result = (b.get() & 0xFF);
353
* @return filename with audioformat seperator stripped of, lengthened to ensure not too small for calid tempfile
356
public static String getMinBaseFilenameAllowedForTempFile(File file)
358
String s = AudioFile.getBaseFilename(file);
367
else if(s.length()==1)
371
else if(s.length()==2)