FileDocCategorySizeDatePackage
Utils.javaAPI DocJaudiotagger 2.0.414473Wed Sep 21 17:49:54 BST 2011org.jaudiotagger.audio.generic

Utils

public class Utils extends Object
Contains various frequently used static functions in the different tag formats
author
Raphael Slinckx

Fields Summary
public static Logger
logger
private static final int
MAX_BASE_TEMP_FILENAME_LENGTH
Constructors Summary
Methods Summary
public static voidcopy(byte[] src, byte[] dst, int dstOffset)
Copies the bytes of srd to dst at the specified offset.

param
src The byte to be copied.
param
dst The array to copy to
param
dstOffset The start offset for the bytes to be copied.


                                                         
            
    
        System.arraycopy(src, 0, dst, dstOffset, src.length);
    
public static booleancopy(java.io.File fromFile, java.io.File toFile)
Copy a File

param
fromFile The existing File
param
toFile The new File
return
true if and only if the renaming succeeded; false otherwise

        try
        {
            FileInputStream in = new FileInputStream(fromFile);
            FileOutputStream out = new FileOutputStream(toFile);
            byte[] buf = new byte[8192];

            int len;

            while ((len = in.read(buf)) > -1)
            {
                out.write(buf, 0, len);
            }

            in.close();
            out.close();

            // cleanup if files are not the same length
            if (fromFile.length() != toFile.length())
            {
                toFile.delete();

                return false;
            }

            return true;
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return false;
        }
    
public static java.lang.StringgetBaseFilenameForTempFile(java.io.File file)
Get a base for temp file, this should be long enough so that it easy to work out later what file the temp file was created for if it is left lying round, but not ridiculously long as this can cause problems with max filename limits and is not very useful

param
file
return

        String filename = getMinBaseFilenameAllowedForTempFile(file);
        if(filename.length()<= MAX_BASE_TEMP_FILENAME_LENGTH)
        {
           return filename;
        }
        return filename.substring(0,MAX_BASE_TEMP_FILENAME_LENGTH);
    
public static byte[]getDefaultBytes(java.lang.String s, java.lang.String charSet)
Returns {@link String#getBytes()}.

param
s The String to call, decode bytes using the specfied charset
param
charSet
return
The bytes.

        try
        {
            return s.getBytes(charSet);
        }
        catch (UnsupportedEncodingException uee)
        {
            throw new RuntimeException(uee);
        }

    
public static java.lang.StringgetExtension(java.io.File f)

        String name = f.getName().toLowerCase();
        int i = name.lastIndexOf(".");
        if (i == -1)
        {
            return "";
        }

        return name.substring(i + 1);
    
public static intgetIntBE(byte[] b, int start, int end)

        return (int) getLongBE(ByteBuffer.wrap(b), start, end);
    
public static intgetIntBE(java.nio.ByteBuffer b, int start, int end)

        return (int) getLongBE(b, start, end);
    
public static intgetIntLE(byte[] b)

        return (int) getLongLE(ByteBuffer.wrap(b), 0, b.length - 1);
    
public static intgetIntLE(byte[] b, int start, int end)

        return (int) getLongLE(ByteBuffer.wrap(b), start, end);
    
public static longgetLongBE(java.nio.ByteBuffer b, int start, int end)

        long number = 0;
        for (int i = 0; i < (end - start + 1); i++)
        {
            number += ((long)((b.get(end - i) & 0xFF)) << i * 8);
        }

        return number;
    
public static longgetLongLE(java.nio.ByteBuffer b, int start, int end)

        long number = 0;
        for (int i = 0; i < (end - start + 1); i++)
        {
            number += ((b.get(start + i) & 0xFF) << i * 8);
        }

        return number;
    
public static java.lang.StringgetMinBaseFilenameAllowedForTempFile(java.io.File file)

param
file
return
filename with audioformat separator stripped of, lengthened to ensure not too small for valid tempfile creation.

        String s = AudioFile.getBaseFilename(file);
        if (s.length() >= 3)
        {
            return s;
        }
        if (s.length() == 1)
        {
            return s + "000";
        }
        else if (s.length() == 1)
        {
            return s + "00";
        }
        else if (s.length() == 2)
        {
            return s + "0";
        }
        return s;
    
public static shortgetShortBE(java.nio.ByteBuffer b, int start, int end)

        return (short) getIntBE(b, start, end);
    
public static byte[]getSizeBEInt16(short size)
Convert short to byte representation - Big Endian (as used by mp4)

param
size
return
byte represenetation

        byte[] b = new byte[2];
        b[0] = (byte) ((size >> 8) & 0xFF);
        b[1] = (byte) (size & 0xFF);
        return b;
    
public static byte[]getSizeBEInt32(int size)
Convert int to byte representation - Big Endian (as used by mp4)

param
size
return
byte represenetation

        byte[] b = new byte[4];
        b[0] = (byte) ((size >> 24) & 0xFF);
        b[1] = (byte) ((size >> 16) & 0xFF);
        b[2] = (byte) ((size >> 8) & 0xFF);
        b[3] = (byte) (size & 0xFF);
        return b;
    
public static byte[]getSizeLEInt32(int size)
Convert int to byte representation - Little Endian (as used by ogg vorbis)

param
size
return
byte represenetation

        byte[] b = new byte[4];
        b[0] = (byte) (size & 0xff);
        b[1] = (byte) ((size >>> 8) & 0xffL);
        b[2] = (byte) ((size >>> 16) & 0xffL);
        b[3] = (byte) ((size >>> 24) & 0xffL);
        return b;
    
public static java.lang.StringgetString(byte[] b, int offset, int length, java.lang.String encoding)
Create String starting from offset upto length using encoding

param
b
param
offset
param
length
param
encoding
return
throws
UnsupportedEncodingException

        try
        {
            return new String(b, offset, length, encoding);
        }
        catch (UnsupportedEncodingException ue)
        {
            //Shouldnt have to worry about this exception as should only be calling with well defined charsets
            throw new RuntimeException(ue);
        }
    
public static java.lang.StringgetString(java.nio.ByteBuffer buffer, int offset, int length, java.lang.String encoding)
Create String offset from position by offset upto length using encoding, and position of buffer is moved to after position + offset + length

param
buffer
param
offset
param
length
param
encoding
return

        byte[] b = new byte[length];
        buffer.position(buffer.position() + offset);
        buffer.get(b);
        try
        {
            return new String(b, 0, length, encoding);
        }
        catch (UnsupportedEncodingException uee)
        {
            //TODO, will we ever use unsupported encodings
            throw new RuntimeException(uee);
        }
    
public static byte[]getUTF8Bytes(java.lang.String s)

        return s.getBytes("UTF-8");
    
public static intread(java.nio.ByteBuffer b)

        int result = (b.get() & 0xFF);
        return result;
    
public static java.lang.StringreadString(java.io.DataInput di, int charsToRead)

        final byte[] buf = new byte[charsToRead];
        di.readFully(buf);
        return new String(buf);
    
public static intreadUBEInt16(java.nio.ByteBuffer b)

        int result = 0;
        result += readUInt8(b) << 8;
        result += readUInt8(b);
        return result;
    
public static intreadUBEInt24(java.nio.ByteBuffer b)

        int result = 0;
        result += readUBEInt16(b) << 16;
        result += readUInt8(b);
        return result;
    
public static intreadUBEInt32(java.nio.ByteBuffer b)

        int result = 0;
        result += readUBEInt16(b) << 16;
        result += readUBEInt16(b);
        return result;
    
public static longreadUInt64(java.nio.ByteBuffer b)

        long result = 0;
        result += (readUBEInt32(b) << 32);
        result += readUBEInt32(b);
        return result;
    
public static intreadUInt8(java.nio.ByteBuffer b)

        return read(b);
    
public static intreadUint16(java.io.DataInput di)

        final byte[] buf = {0x00, 0x00, 0x00, 0x00};
        di.readFully(buf, 2, 2);
        final int i = ByteBuffer.wrap(buf).getInt();
        return i;
    
public static longreadUint32(java.io.DataInput di)

        final byte[] buf8 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
        di.readFully(buf8, 4, 4);
        final long l = ByteBuffer.wrap(buf8).getLong();
        return l;
    
public static intreadUint32AsInt(java.io.DataInput di)
Overflow checking since java can't handle unsigned numbers.

param
di
throws
java.io.IOException
return

        final long l = readUint32(di);
        if (l > Integer.MAX_VALUE)
        {
            throw new IOException("uint32 value read overflows int");
        }
        return (int) l;
    
public static booleanrename(java.io.File fromFile, java.io.File toFile)
Rename file, and if normal rename fails, try copy and delete instead

param
fromFile
param
toFile
return

        logger.log(Level.CONFIG,"Renaming From:"+fromFile.getAbsolutePath() + " to "+toFile.getAbsolutePath());

        if(toFile.exists())
        {
            logger.log(Level.SEVERE,"Destination File:"+toFile + " already exists");
            return false;
        }

        //Rename File, could fail because being  used or because trying to rename over filesystems
        final boolean result = fromFile.renameTo(toFile);
        if (!result)
        {
            // Might be trying to rename over filesystem, so try copy and delete instead
            if (copy(fromFile, toFile))
            {
                //If copy works but deletion of original file fails then it is because the file is being used
                //so we need to delete the file we have just created
                boolean deleteResult=fromFile.delete();
                if(!deleteResult)
                {
                    logger.log(Level.SEVERE,"Unable to delete File:"+fromFile);
                    toFile.delete();
                    return false;
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        return true;