public ArrayList ParseFile(InputFile file)
        {
            ArrayList atoms = new ArrayList();
            UInt64 position = 0;

            while (position < file.Length)
            {
                string type = GetAtomType(file, position);
                Atom atom = null;

                if (IsContainer(type))
                {
                    atom = new ContainerAtom(type);
                    ParseContainer(file, position, (ContainerAtom)atom);
                }
                else if (IsAtom(type))
                {
                    atom = new LeafAtom(type);
                    ParseAtom(file, position, atom);
                }
                else
                {
                    throw new Exception("Invalid atom at pos 0x" + position.ToString("X16"));
                }

                atoms.Add(atom);
                position += atom.TotalLength;
            }

            return atoms;
        }
                outFile.WriteUInt64(leaf.TotalLength);
            }

            if (leaf.PayloadData != null)
            {
                outFile.WriteBytes(leaf.PayloadData);
            }
            else
            {
                if (leaf.OriginalPayloadFileOffset > 0)
                {
                    outFile.WriteFromInput(inFile, leaf.OriginalPayloadFileOffset, leaf.PayloadLength);
                }
                else
                {
                    outFile.WriteFromInput(inFile, leaf.PayloadFileOffset, leaf.PayloadLength);
                }
            }
        }
    }
}
        private void ParseContainer(InputFile file, ulong position, ContainerAtom container)
        {
            ParseAtom(file, position, container);

            ArrayList atoms = new ArrayList();
            UInt64 offset = container.HeaderLength;

            while (offset < container.TotalLength)
            {
                string type = GetAtomType(file, position + offset);
                Atom atom = null;

                if (IsContainer(type))
                {
                    atom = new ContainerAtom(type);
                    ParseContainer(file, position + offset, (ContainerAtom)atom);
                }
                else
                {
                    atom = new LeafAtom(type);
                    ParseAtom(file, position + offset, atom);
                }

                container.Children.Add(atom);
                offset += atom.TotalLength;
            }
        }