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

« back to all changes in this revision

Viewing changes to org/jaudiotagger/tag/id3/ID3SyncSafeInteger.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
 
package org.jaudiotagger.tag.id3;
2
 
 
3
 
import java.nio.ByteBuffer;
4
 
 
5
 
/**
6
 
 *  Peforms encoding/decoding of an syncsafe integer
7
 
 *
8
 
 *  <p>Syncsafe integers are used for the size in the tag header of v23 and v24 tags, and in the frame size in
9
 
 *  the frame header of v24 frames.
10
 
 *
11
 
 *  <p>In some parts of the tag it is inconvenient to use the
12
 
 *  unsychronisation scheme because the size of unsynchronised data is
13
 
 *  not known in advance, which is particularly problematic with size
14
 
 *  descriptors. The solution in ID3v2 is to use synchsafe integers, in
15
 
 *  which there can never be any false synchs. Synchsafe integers are
16
 
 *  integers that keep its highest bit (bit 7) zeroed, making seven bits
17
 
 *  out of eight available. Thus a 32 bit synchsafe integer can store 28
18
 
 *  bits of information.
19
 
 
20
 
 *  Example:
21
 
 *
22
 
 *    255 (%11111111) encoded as a 16 bit synchsafe integer is 383
23
 
 *    (%00000001 01111111).
24
 
 */
25
 
public class ID3SyncSafeInteger
26
 
{
27
 
    public static final int INTEGRAL_SIZE = 4;
28
 
 
29
 
    /** Sizes equal or smaller than this are the same whether held as sync safe integer or normal integer so
30
 
     *  it doesnt matter.
31
 
     */
32
 
    public static final int MAX_SAFE_SIZE  = 127;
33
 
 
34
 
    /**
35
 
     * Read syncsafe value from byteArray in format specified in spec and convert to int.
36
 
     *
37
 
     * @param buffer syncsafe integer
38
 
     * @return decoded int
39
 
     */
40
 
    private static int bufferToValue(byte[] buffer)
41
 
    {
42
 
        //Note Need to && with 0xff otherwise if value is greater than 128 we get a negative number
43
 
        //when cast byte to int
44
 
        return (int) ((buffer[0] & 0xff) << 21)
45
 
                + ((buffer[1] & 0xff) << 14)
46
 
                + ((buffer[2] & 0xff) << 7)
47
 
                + ((buffer[3]) & 0xff);
48
 
    }
49
 
 
50
 
     /**
51
 
     * Read syncsafe value from buffer in format specified in spec and convert to int.
52
 
     *
53
 
     * The buffers position is moved to just after the location of the syscsafe integer
54
 
     *
55
 
     * @param buffer syncsafe integer
56
 
     * @return decoded int
57
 
     */
58
 
    protected static int bufferToValue(ByteBuffer buffer)
59
 
    {
60
 
        byte byteBuffer [] = new byte[INTEGRAL_SIZE];
61
 
        buffer.get(byteBuffer,0,INTEGRAL_SIZE);
62
 
        return bufferToValue(byteBuffer);
63
 
    }
64
 
 
65
 
    /**
66
 
     * Is buffer holding a value that is definently not sysncsafe
67
 
     *
68
 
     * We cannot guarantee a buffer is holding a syncsafe integer but there are some checks
69
 
     * we can do to show that it definently is not.
70
 
     *
71
 
     * The buffer is NOT moved after reading.
72
 
     *
73
 
     * This function is useful for reading ID3v24 frames created in iTunes because iTunes does not use syncsafe
74
 
     * integers in  its frames.
75
 
     *
76
 
     * @param buffer
77
 
     * @return true if this buffer is definently not holding a syncsafe integer
78
 
     */
79
 
    protected static boolean isBufferNotSyncSafe(ByteBuffer buffer)
80
 
    {
81
 
        int position = buffer.position();
82
 
 
83
 
        //Check Bit7 not set
84
 
        for(int i=0;i<INTEGRAL_SIZE;i++)
85
 
        {
86
 
            byte nextByte = buffer.get(position+i);
87
 
            if((nextByte & 0x80)>0)
88
 
            {
89
 
                return true;
90
 
            }
91
 
        }
92
 
        return false;
93
 
    }
94
 
 
95
 
    /**
96
 
     * Checks if the buffer just contains zeros
97
 
     *
98
 
     * This can be used to identify when accessing padding of a tag
99
 
     *
100
 
     * @param buffer
101
 
     * @return true if buffer only contains zeros
102
 
     */
103
 
    protected static boolean isBufferEmpty(byte[] buffer)
104
 
    {
105
 
        for (byte aBuffer : buffer)
106
 
        {
107
 
            if (aBuffer != 0)
108
 
            {
109
 
                return false;
110
 
            }
111
 
        }
112
 
        return true;
113
 
    }
114
 
 
115
 
    /**
116
 
     * Convert int value to syncsafe value held in bytearray
117
 
     *
118
 
     * @param size
119
 
     * @return buffer syncsafe integer
120
 
     */
121
 
    protected static byte[] valueToBuffer(int size)
122
 
    {
123
 
        byte[] buffer = new byte[4];
124
 
        buffer[0] = (byte) ((size & 0x0FE00000) >> 21);
125
 
        buffer[1] = (byte) ((size & 0x001FC000) >> 14);
126
 
        buffer[2] = (byte) ((size & 0x00003F80) >> 7);
127
 
        buffer[3] = (byte) (size & 0x0000007F);
128
 
        return buffer;
129
 
    }
130
 
}