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;
        }
            else
            {
                outFile.WriteUInt32(1);
                outFile.WriteChars(container.Type.ToCharArray());
                outFile.WriteUInt64(container.TotalLength);
            }

            foreach (var obj in container.Children)
            {
                if (obj is ContainerAtom)
                {
                    SaveContainer(inFile, outFile, (ContainerAtom)obj);
                }
                else
                {
                    SaveAtom(inFile, outFile, (LeafAtom)obj);
                }
            }
        }

        private void SaveAtom(InputFile inFile, OutputFile outFile, LeafAtom leaf)
        {
            if (leaf.HeaderLength == 8)
            {
                outFile.WriteUInt32((UInt32)leaf.TotalLength);
                outFile.WriteChars(leaf.Type.ToCharArray());
            }
            else
            {
                outFile.WriteUInt32(1);
        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;
            }
        }