Ejemplo n.º 1
0
        public void Write(NSObject root)
        {
            // magic bytes
            Write(new[] { (byte)'b', (byte)'p', (byte)'l', (byte)'i', (byte)'s', (byte)'t' });

            //version
            switch (version)
            {
            case VERSION_00:
            {
                Write(new[] { (byte)'0', (byte)'0' });
                break;
            }

            case VERSION_10:
            {
                Write(new[] { (byte)'1', (byte)'0' });
                break;
            }

            case VERSION_15:
            {
                Write(new[] { (byte)'1', (byte)'5' });
                break;
            }

            case VERSION_20:
            {
                Write(new[] { (byte)'2', (byte)'0' });
                break;
            }
            }

            // assign IDs to all the objects.
            root.AssignIDs(this);

            idSizeInBytes = ComputeIdSizeInBytes(idDict.Count);

            // offsets of each object, indexed by ID
            long[] offsets = new long[idDict.Count];

            // write each object, save offset
            foreach (KeyValuePair <NSObject, int> pair in idDict)
            {
                NSObject obj = pair.Key;
                int      id  = pair.Value;
                offsets[id] = count;
                if (obj == null)
                {
                    Write(0x00);
                }
                else
                {
                    obj.ToBinary(this);
                }
            }

            // write offset table
            long offsetTableOffset = count;
            int  offsetSizeInBytes = ComputeOffsetSizeInBytes(count);

            foreach (long offset in offsets)
            {
                WriteBytes(offset, offsetSizeInBytes);
            }

            if (version != VERSION_15)
            {
                // write trailer
                // 6 null bytes
                Write(new byte[6]);
                // size of an offset
                Write(offsetSizeInBytes);
                // size of a ref
                Write(idSizeInBytes);
                // number of objects
                WriteLong(idDict.Count);
                // top object
                int rootID = idDict[root];
                WriteLong(rootID);
                // offset table offset
                WriteLong(offsetTableOffset);
            }

            outStream.Flush();
        }
        void Write(NSObject root)
        {
            // magic bytes
            Write(new[] { (byte)'b', (byte)'p', (byte)'l', (byte)'i', (byte)'s', (byte)'t' });

            //version
            switch (version)
            {
                case VERSION_00:
                    {
                        Write(new[] { (byte)'0', (byte)'0' });
                        break;
                    }
                case VERSION_10:
                    {
                        Write(new[] { (byte)'1', (byte)'0' });
                        break;
                    }
                case VERSION_15:
                    {
                        Write(new[] { (byte)'1', (byte)'5' });
                        break;
                    }
                case VERSION_20:
                    {
                        Write(new[] { (byte)'2', (byte)'0' });
                        break;
                    }
            }

            // assign IDs to all the objects.
            root.AssignIDs(this);

            idSizeInBytes = ComputeIdSizeInBytes(idMap.Count);

            // offsets of each object, indexed by ID
            long[] offsets = new long[idMap.Count];

            // write each object, save offset
            for (int i = 0; i < idMap.Count; i++)
            {
                NSObject obj = idMap[i];
                int id = i;
                offsets[id] = count;
                if (obj == null)
                {
                    Write(0x00);
                }
                else
                {
                    obj.ToBinary(this);
                }
            }

            // write offset table
            long offsetTableOffset = count;
            int offsetSizeInBytes = ComputeOffsetSizeInBytes(count);
            foreach (long offset in offsets)
            {
                WriteBytes(offset, offsetSizeInBytes);
            }

            if (version != VERSION_15)
            {
                // write trailer
                // 6 null bytes
                Write(new byte[6]);
                // size of an offset
                Write(offsetSizeInBytes);
                // size of a ref
                Write(idSizeInBytes);
                // number of objects
                WriteLong(idMap.Count);
                // top object
                int rootID = idMap.IndexOf(root);
                WriteLong(rootID);
                // offset table offset
                WriteLong(offsetTableOffset);
            }

            outStream.Flush();
        }