FileDocCategorySizeDatePackage
ID3DataInputStream.javaAPI Docjid3 0.467944Sun Feb 06 18:11:25 GMT 2005org.blinkenlights.jid3.io

ID3DataInputStream

public class ID3DataInputStream extends DataInputStream
author
paul Custom DataInputStream containing convenience methods for reading ID3 tags.

Fields Summary
Constructors Summary
public ID3DataInputStream(InputStream oIS)

        super(oIS);
    
Methods Summary
public intreadBE24()
Read an unsigned big-endian 24-bit value and returns an int.

return
the integer value read
throws
IOException

        int iThree = readUnsignedByte();
        int iTwo = readUnsignedByte();
        int iOne = readUnsignedByte();
        
        int iVal = (iOne | (iTwo << 8) | (iThree << 16));
        
        return iVal;
    
public intreadBE32()
Read a signed big-endian 32-bit value and returns an int.

return
the integer value read
throws
IOException

        int iFour = readUnsignedByte();
        int iThree = readUnsignedByte();
        int iTwo = readUnsignedByte();
        int iOne = readUnsignedByte();
        
        int iVal = (iOne | (iTwo << 8) | (iThree << 16) | (iFour << 24));
        
        return iVal;
    
public final intreadBEUnsigned16()
Reads an unsigned big-endian 16-bit value and returns an int.

return
the integer value read
throws
IOException

        int iHi = readUnsignedByte();
        int iLo = readUnsignedByte();
        
        int iVal = iLo | (iHi << 8);
        
        return iVal;
    
public intreadID3Four()
Read an encoded four byte value. The encoding method uses only the lowest seven bits of each byte, to prevent synchronization errors in the MP3 data stream.

        int iValue = 0;
        byte[] abyValue = new byte[4];
        readFully(abyValue);
        
        if ( ((abyValue[0] & 0x80) != 0) ||
             ((abyValue[1] & 0x80) != 0) ||
             ((abyValue[2] & 0x80) != 0) ||
             ((abyValue[3] & 0x80) != 0) )
        {
            throw new ID3Exception("High bit cannot be set in encoded values.");
        }
        
        iValue |= ((abyValue[0] & 0x7f) << (3 * 7));
        iValue |= ((abyValue[1] & 0x7f) << (2 * 7));
        iValue |= ((abyValue[2] & 0x7f) << (1 * 7));
        iValue |= ((abyValue[3] & 0x7f) << (0 * 7));
        
        return iValue;
    
public java.lang.StringreadStringToNull(TextEncoding oTextEncoding, int iMaxLength)
Read a string in the specified encoding format to null, not exceeding a predefined length. Note that Unicode strings must be terminated by a double null (two zero bytes).

param
oTextEncoding the encoding format of the string to be read
param
iMaxLength a length beyond which not to read further (in characters, not necessarily bytes)
return
a String, or null if string would be zero length
throws
IOException on I/O error, or if string would exceed allowed length

        if (oTextEncoding == null)
        {
            throw new NullPointerException("Text encoding cannot be null.");
        }

        ByteArrayOutputStream oStringBAOS = new ByteArrayOutputStream();
        int iStringByte1;
        int iStringByte2 = 0;
        int iLength = 0;
        do
        {
            iStringByte1 = readUnsignedByte();
            if (oTextEncoding.equals(TextEncoding.UNICODE))
            {
                iStringByte2 = readUnsignedByte();
            }
            if ((iStringByte1 != 0) || (iStringByte2 != 0))
            {
                if (iLength == iMaxLength)
                {
                    throw new IOException("String length exceeds set " + iMaxLength + " byte limit.");
                }
                oStringBAOS.write(iStringByte1);
                if (oTextEncoding.equals(TextEncoding.UNICODE))
                {
                    oStringBAOS.write(iStringByte2);
                }
                iLength++;
            }
        }
        while ((iStringByte1 != 0) || (iStringByte2 != 0)) ;
        
        // return byte array as a string
        byte[] abyShortDescription = oStringBAOS.toByteArray();
        return new String(abyShortDescription, oTextEncoding.getEncodingString());
    
public java.lang.StringreadStringToNull()
Read an ISO-8859-1 encoded string to null.

return
a String, or null if string would be zero length
throws
IOException

        return readStringToNull(Integer.MAX_VALUE);
    
public java.lang.StringreadStringToNull(int iMaxLength)
Read an ISO-8859-1 string to null, not exceeding a predefined length.

param
iMaxLength a length beyond which not to read further
return
a String, or null if string would be zero length
throws
IOException on I/O error, or if string would exceed allowed length

        ByteArrayOutputStream oStringBAOS = new ByteArrayOutputStream();
        int iStringByte;
        do
        {
            iStringByte = readUnsignedByte();
            if (iStringByte != 0)
            {
                if (oStringBAOS.size() == iMaxLength)
                {
                    throw new IOException("String length exceeds set " + iMaxLength + " byte limit.");
                }
                oStringBAOS.write(iStringByte);
            }
        }
        while (iStringByte != 0);
        
        // return byte array as a string
        byte[] abyShortDescription = oStringBAOS.toByteArray();
        return new String(abyShortDescription);
    
public java.lang.StringreadStringToNull(TextEncoding oTextEncoding)
Read a string in the specified encoding format to null. Note that Unicode strings must be terminated by a double null (two zero bytes).

param
oTextEncoding the encoding format of the string to be read
return
a String, or null if string would be zero length
throws
IOException

        return readStringToNull(oTextEncoding, Integer.MAX_VALUE);
    
public longreadUnsignedBE32()
Read an unsigned big-endian 32-bit value and returns a long.

return
the long value read
throws
IOException

        long lFour = readUnsignedByte();
        long lThree = readUnsignedByte();
        long lTwo = readUnsignedByte();
        long lOne = readUnsignedByte();
        
        long lVal = (lOne | (lTwo << 8) | (lThree << 16) | (lFour << 24));
        
        return lVal;