views:

30

answers:

0

Hey, I've been trying to be able to read and write iTunes style M4A atoms and while I've successfully done the reading part, I've come to a bit of a halt in regards to the free space atoms. I figured that I should be able edit and shift the padding around to accommodate writing an atom with more data than it originally had. I've been stuck on this for about a day now, and I've been trying to figure out how to determine the closest free space atom with enough size to accommodate the new data. so far I have:

private freeAtom acquireFreeSpaceAtom( long position )
{
    long atomStart = Long.MAX_VALUE;

    freeAtom atom = null;
    for( freeAtom a : freeSpace )
    {
        if( Math.abs( position - atomStart ) > Math.abs( position - a.getAtomStart() ) )
            atomStart = ( atom = a ).getAtomStart();
    }

    return atom;
}

That code only takes into account the closest free space atom and completely disregards the fact that it should be greater than or equal to a certain size, but I can't quite figure out how I should check for both closeness and size efficiently.