~ubuntu-branches/ubuntu/utopic/libjaudiotagger-java/utopic-proposed

« back to all changes in this revision

Viewing changes to src/org/jaudiotagger/audio/generic/Utils.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath, Damien Raude-Morvan, Varun Hiremath
  • Date: 2009-04-01 19:17:56 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090401191756-bygniim270guy7o1
Tags: 1.0.9-1
[ Damien Raude-Morvan ]
* New upstream release
* debian/watch: Use java.net repository (which contains new releases!)
* debian/control:
  - Build-Depends on default-jdk-builddep
  - Bump Standards-Version to 3.8.1 (no changes needed)
  - Change section to "java"
* debian/rules: use default-java as JAVA_HOME
* debina/orig-tar.{sh|excludes}: strip audio and others binary files from ZIP
* debian/build.xml:
  - compile with "nowarn" to keep build log readable
  - exclude LogFormatter from build (use com.sun classes)
* debian/ant.properties: new source directory is "src" in orig.tar.gz
* Add myself as Uploaders

[ Varun Hiremath ]
* Accept changes made by Damien Raude-Morvan (Closes: #522130)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Entagged Audio Tag library
 
3
 * Copyright (c) 2003-2005 Rapha�l Slinckx <raphael@slinckx.net>
 
4
 * 
 
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.
 
9
 *  
 
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.
 
14
 * 
 
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
 
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
 * Contains various frequently used static functions in the different tag
 
28
 * formats
 
29
 *
 
30
 * @author Raphael Slinckx
 
31
 */
 
32
public class Utils
 
33
{
 
34
 
 
35
    /**
 
36
     * Copies the bytes of <code>srd</code> to <code>dst</code> at the
 
37
     * specified offset.
 
38
     *
 
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.
 
42
     */
 
43
    public static void copy(byte[] src, byte[] dst, int dstOffset)
 
44
    {
 
45
        System.arraycopy(src, 0, dst, dstOffset, src.length);
 
46
    }
 
47
 
 
48
 
 
49
    /**
 
50
     * Returns {@link String#getBytes()}.<br>
 
51
     *
 
52
     * @param s The String to call, decode bytes using the specfied charset
 
53
     * @return The bytes.
 
54
     */
 
55
    public static byte[] getDefaultBytes(String s, String charSet)
 
56
    {
 
57
        try
 
58
        {
 
59
            return s.getBytes(charSet);
 
60
        }
 
61
        catch (UnsupportedEncodingException uee)
 
62
        {
 
63
            throw new RuntimeException(uee);
 
64
        }
 
65
 
 
66
    }
 
67
 
 
68
    /*
 
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 "."
 
72
      *
 
73
      * @param f The file whose extension is requested
 
74
      * @return The extension of the given file
 
75
      */
 
76
    public static String getExtension(File f)
 
77
    {
 
78
        String name = f.getName().toLowerCase();
 
79
        int i = name.lastIndexOf(".");
 
80
        if (i == -1)
 
81
        {
 
82
            return "";
 
83
        }
 
84
 
 
85
        return name.substring(i + 1);
 
86
    }
 
87
 
 
88
 
 
89
    /*
 
90
    * Computes a number whereby the 1st byte is the least signifcant and the last
 
91
    * byte is the most significant.
 
92
    *
 
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.
 
97
    *
 
98
    * So if storing a number which only requires one byte it will be stored in the first
 
99
    * byte.
 
100
    */
 
101
    public static long getLongLE(ByteBuffer b, int start, int end)
 
102
    {
 
103
        long number = 0;
 
104
        for (int i = 0; i < (end - start + 1); i++)
 
105
        {
 
106
            number += ((b.get(start + i) & 0xFF) << i * 8);
 
107
        }
 
108
 
 
109
        return number;
 
110
    }
 
111
 
 
112
    /*
 
113
     * Computes a number whereby the 1st byte is the most significant and the last
 
114
     * byte is the least significant.
 
115
     *
 
116
     * So if storing a number which only requires one byte it will be stored in the last
 
117
     * byte.
 
118
     */
 
119
    public static long getLongBE(ByteBuffer b, int start, int end)
 
120
    {
 
121
        int number = 0;
 
122
        for (int i = 0; i < (end - start + 1); i++)
 
123
        {
 
124
            number += ((b.get(end - i) & 0xFF) << i * 8);
 
125
        }
 
126
 
 
127
        return number;
 
128
    }
 
129
 
 
130
    public static int getIntLE(byte[] b)
 
131
    {
 
132
        return (int) getLongLE(ByteBuffer.wrap(b), 0, b.length - 1);
 
133
    }
 
134
 
 
135
    /*
 
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
 
140
      * sequence.
 
141
      */
 
142
    public static int getIntLE(byte[] b, int start, int end)
 
143
    {
 
144
        return (int) getLongLE(ByteBuffer.wrap(b), start, end);
 
145
    }
 
146
 
 
147
    public static int getIntBE(byte[] b, int start, int end)
 
148
    {
 
149
        return (int) getLongBE(ByteBuffer.wrap(b), start, end);
 
150
    }
 
151
 
 
152
    public static int getIntBE(ByteBuffer b, int start, int end)
 
153
    {
 
154
        return (int) getLongBE(b, start, end);
 
155
    }
 
156
 
 
157
    public static short getShortBE(ByteBuffer b, int start, int end)
 
158
    {
 
159
        return (short) getIntBE(b, start, end);
 
160
    }
 
161
 
 
162
    /**
 
163
     * Convert int to byte representation - Big Endian (as used by mp4)
 
164
     *
 
165
     * @param size
 
166
     * @return byte represenetation
 
167
     */
 
168
    public static byte[] getSizeBEInt32(int size)
 
169
    {
 
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);
 
175
        return b;
 
176
    }
 
177
 
 
178
    /**
 
179
     * Convert short to byte representation - Big Endian (as used by mp4)
 
180
     *
 
181
     * @param size
 
182
     * @return byte represenetation
 
183
     */
 
184
    public static byte[] getSizeBEInt16(short size)
 
185
    {
 
186
        byte[] b = new byte[2];
 
187
        b[0] = (byte) ((size >> 8) & 0xFF);
 
188
        b[1] = (byte) (size & 0xFF);
 
189
        return b;
 
190
    }
 
191
 
 
192
 
 
193
    /**
 
194
     * Convert int to byte representation - Little Endian (as used by ogg vorbis)
 
195
     *
 
196
     * @param size
 
197
     * @return byte represenetation
 
198
     */
 
199
    public static byte[] getSizeLEInt32(int size)
 
200
    {
 
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);
 
206
        return b;
 
207
    }
 
208
 
 
209
    /**
 
210
     * Create String starting from offset upto length using encoding
 
211
     *
 
212
     * @param b
 
213
     * @param offset
 
214
     * @param length
 
215
     * @param encoding
 
216
     * @return
 
217
     * @throws UnsupportedEncodingException
 
218
     */
 
219
    public static String getString(byte[] b, int offset, int length, String encoding)
 
220
    {
 
221
        try
 
222
        {
 
223
            return new String(b, offset, length, encoding);
 
224
        }
 
225
        catch (UnsupportedEncodingException ue)
 
226
        {
 
227
            //Shouldnt have to worry about this exception as should only be calling with well defined charsets
 
228
            throw new RuntimeException(ue);
 
229
        }
 
230
    }
 
231
 
 
232
    /**
 
233
     * Create String offset from position by offset upto length using encoding, and position of buffer
 
234
     * is moved to after position + offset + length
 
235
     *
 
236
     * @param buffer
 
237
     * @param offset
 
238
     * @param length
 
239
     * @param encoding
 
240
     * @return
 
241
     */
 
242
    public static String getString(ByteBuffer buffer, int offset, int length, String encoding)
 
243
    {
 
244
        byte[] b = new byte[length];
 
245
        buffer.position(buffer.position() + offset);
 
246
        buffer.get(b);
 
247
        try
 
248
        {
 
249
            return new String(b, 0, length, encoding);
 
250
        }
 
251
        catch (UnsupportedEncodingException uee)
 
252
        {
 
253
            //TODO, will we ever use unsupported encodings
 
254
            throw new RuntimeException(uee);
 
255
        }
 
256
    }
 
257
 
 
258
    /*
 
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.
 
261
      *
 
262
      * @param s The string to convert @return The byte array representation of
 
263
      * this string in UTF8 encoding
 
264
      */
 
265
    public static byte[] getUTF8Bytes(String s) throws UnsupportedEncodingException
 
266
    {
 
267
        return s.getBytes("UTF-8");
 
268
    }
 
269
 
 
270
    /**
 
271
     * Overflow checking since java can't handle unsigned numbers.
 
272
     */
 
273
    public static int readUint32AsInt(DataInput di) throws IOException
 
274
    {
 
275
        final long l = readUint32(di);
 
276
        if (l > Integer.MAX_VALUE)
 
277
        {
 
278
            throw new IOException("uint32 value read overflows int");
 
279
        }
 
280
        return (int) l;
 
281
    }
 
282
 
 
283
    public static long readUint32(DataInput di) throws IOException
 
284
    {
 
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();
 
288
        return l;
 
289
    }
 
290
 
 
291
    public static int readUint16(DataInput di) throws IOException
 
292
    {
 
293
        final byte[] buf = {0x00, 0x00, 0x00, 0x00};
 
294
        di.readFully(buf, 2, 2);
 
295
        final int i = ByteBuffer.wrap(buf).getInt();
 
296
        return i;
 
297
    }
 
298
 
 
299
    public static String readString(DataInput di, int charsToRead) throws IOException
 
300
    {
 
301
        final byte[] buf = new byte[charsToRead];
 
302
        di.readFully(buf);
 
303
        return new String(buf);
 
304
    }
 
305
 
 
306
    public static long readUInt64(ByteBuffer b) 
 
307
    {
 
308
        long result = 0;
 
309
        result += (readUBEInt32(b) << 32);
 
310
        result += readUBEInt32(b);
 
311
        return result;
 
312
    }
 
313
 
 
314
    public static int readUBEInt32(ByteBuffer b)
 
315
    {
 
316
        int result = 0;
 
317
        result += readUBEInt16(b) << 16;
 
318
        result += readUBEInt16(b);
 
319
        return result;
 
320
    }
 
321
 
 
322
    public static int readUBEInt24(ByteBuffer b)
 
323
    {
 
324
        int result = 0;
 
325
        result += readUBEInt16(b) << 16;
 
326
        result += readUInt8(b);
 
327
        return result;
 
328
    }
 
329
 
 
330
    public static int readUBEInt16(ByteBuffer b)
 
331
    {
 
332
        int result = 0;
 
333
        result += readUInt8(b) << 8;
 
334
        result += readUInt8(b);
 
335
        return result;
 
336
    }
 
337
 
 
338
    public static int readUInt8(ByteBuffer b)
 
339
    {
 
340
        return read(b);
 
341
    }
 
342
      
 
343
 
 
344
    public static int read(ByteBuffer b)
 
345
    {
 
346
        int result = (b.get() & 0xFF);
 
347
        return result;
 
348
    }
 
349
 
 
350
     /**
 
351
     *
 
352
     * @param file
 
353
     * @return filename with audioformat seperator stripped of, lengthened to ensure not too small for calid tempfile
 
354
     * creation.
 
355
     */
 
356
    public static String getMinBaseFilenameAllowedForTempFile(File file)
 
357
    {
 
358
        String s = AudioFile.getBaseFilename(file);
 
359
        if(s.length()>=3)
 
360
        {
 
361
            return s;
 
362
        }
 
363
        if(s.length()==1)
 
364
        {
 
365
            return s + "000";
 
366
        }
 
367
        else if(s.length()==1)
 
368
        {
 
369
            return s + "00";
 
370
        }
 
371
        else if(s.length()==2)
 
372
        {
 
373
            return s + "0";
 
374
        }
 
375
        return s;
 
376
    }
 
377
}