FileDocCategorySizeDatePackage
PartOfSet.javaAPI DocJaudiotagger 2.0.411304Wed Jun 08 11:59:18 BST 2011org.jaudiotagger.tag.datatype

PartOfSet

public class PartOfSet extends AbstractString
Represents the form 01/10 whereby the second part is optional. This is used by frame such as TRCK and TPOS Some applications like to prepend the count with a zero to aid sorting, (i.e 02 comes before 10)

Fields Summary
Constructors Summary
public PartOfSet(String identifier, org.jaudiotagger.tag.id3.AbstractTagFrameBody frameBody)
Creates a new empty PartOfSet datatype.

param
identifier identifies the frame type
param
frameBody

        super(identifier, frameBody);
    
public PartOfSet(PartOfSet object)
Copy constructor

param
object

        super(object);
    
Methods Summary
public booleanequals(java.lang.Object obj)

        if(obj==this)
        {
            return true;
        }

        if (!(obj instanceof PartOfSet))
        {
            return false;
        }

        PartOfSet that = (PartOfSet) obj;

        return EqualsUtil.areEqual(value, that.value);
    
protected java.lang.StringgetTextEncodingCharSet()
Get the text encoding being used.

The text encoding is defined by the frame body that the text field belongs to.

return
the text encoding charset

        byte textEncoding = this.getBody().getTextEncoding();
        String charSetName = TextEncoding.getInstanceOf().getValueForId(textEncoding);
        logger.finest("text encoding:" + textEncoding + " charset:" + charSetName);
        return charSetName;
    
public org.jaudiotagger.tag.datatype.PartOfSet$PartOfSetValuegetValue()

        return (PartOfSetValue)value;
    
public voidreadByteArray(byte[] arr, int offset)
Read a 'n' bytes from buffer into a String where n is the framesize - offset so thefore cannot use this if there are other objects after it because it has no delimiter.

Must take into account the text encoding defined in the Encoding Object ID3 Text Frames often allow multiple strings seperated by the null char appropriate for the encoding.

param
arr this is the buffer for the frame
param
offset this is where to start reading in the buffer for this field
throws
NullPointerException
throws
IndexOutOfBoundsException

        logger.finest("Reading from array from offset:" + offset);

        //Get the Specified Decoder
        String charSetName = getTextEncodingCharSet();
        CharsetDecoder decoder = Charset.forName(charSetName).newDecoder();

        //Decode sliced inBuffer
        ByteBuffer inBuffer = ByteBuffer.wrap(arr, offset, arr.length - offset).slice();
        CharBuffer outBuffer = CharBuffer.allocate(arr.length - offset);
        decoder.reset();
        CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true);
        if (coderResult.isError())
        {
            logger.warning("Decoding error:" + coderResult.toString());
        }
        decoder.flush(outBuffer);
        outBuffer.flip();

        //Store value
        String stringValue = outBuffer.toString();
        value = new PartOfSetValue(stringValue);

        //SetSize, important this is correct for finding the next datatype
        setSize(arr.length - offset);
        logger.config("Read SizeTerminatedString:" + value + " size:" + size);
    
public java.lang.StringtoString()

        return value.toString();
    
public byte[]writeByteArray()
Write String into byte array

It will remove a trailing null terminator if exists if the option RemoveTrailingTerminatorOnWrite has been set.

return
the data as a byte array in format to write to file

        String value = getValue().toString();
        byte[] data;
        //Try and write to buffer using the CharSet defined by getTextEncodingCharSet()
        try
        {
            if (TagOptionSingleton.getInstance().isRemoveTrailingTerminatorOnWrite())
            {
                if (value.length() > 0)
                {
                    if (value.charAt(value.length() - 1) == '\0")
                    {
                        value = value.substring(0, value.length() - 1);                        
                    }
                }
            }

            String charSetName = getTextEncodingCharSet();
            if (charSetName.equals(TextEncoding.CHARSET_UTF_16))
            {
                charSetName = TextEncoding.CHARSET_UTF_16_LE_ENCODING_FORMAT;
                CharsetEncoder encoder = Charset.forName(charSetName).newEncoder();
                //Note remember LE BOM is ff fe but this is handled by encoder Unicode char is fe ff
                ByteBuffer bb = encoder.encode(CharBuffer.wrap('\ufeff" +  value));
                data = new byte[bb.limit()];
                bb.get(data, 0, bb.limit());

            }
            else
            {
                 CharsetEncoder encoder = Charset.forName(charSetName).newEncoder();
                ByteBuffer bb = encoder.encode(CharBuffer.wrap( value));
                data = new byte[bb.limit()];
                bb.get(data, 0, bb.limit());
            }
        }
        //Should never happen so if does throw a RuntimeException
        catch (CharacterCodingException ce)
        {
            logger.severe(ce.getMessage());
            throw new RuntimeException(ce);
        }
        setSize(data.length);
        return data;