Ejemplo n.º 1
0
        public void Generate()
        {
            if (generateDone)
            {
                throw new InvalidOperationException
                          (_("Invalid_ResourceWriterClosed"));
            }
            BinaryWriter bw = new BinaryWriter(stream);

            try
            {
                // Write the resource file header.
                bw.Write(ResourceManager.MagicNumber);
                bw.Write(ResourceManager.HeaderVersionNumber);
                MemoryStream mem   = new MemoryStream();
                BinaryWriter membw = new BinaryWriter(mem);
                membw.Write("System.Resources.ResourceReader, mscorlib");
                membw.Write
                    ("System.Resources.RuntimeResourceSet, mscorlib");
                membw.Flush();
                bw.Write((int)(mem.Length));
                bw.Write(mem.GetBuffer(), 0, (int)(mem.Length));

                // Write the resource set header.
                bw.Write(1);                                    // Resource set version number.
                bw.Write(table.Count);                          // Number of resources.

                // Create streams for the name and value sections.
                MemoryStream names        = new MemoryStream();
                BinaryWriter namesWriter  = new BinaryWriter(names);
                MemoryStream values       = new MemoryStream();
                BinaryWriter valuesWriter = new BinaryWriter(values);
                int[]        nameHash     = new int [table.Count];
                int[]        namePosition = new int [table.Count];
                int          posn         = 0;

                // Process all values in the resource set.
                IDictionaryEnumerator e = table.GetEnumerator();
                while (e.MoveNext())
                {
                    // Hash the name and record the name position.
                    nameHash[posn]     = ResourceReader.Hash((String)(e.Key));
                    namePosition[posn] = (int)(names.Position);
                    ++posn;

                    // Write out the name and value index.
                    WriteKey(namesWriter, (String)(e.Key));
                    namesWriter.Write((int)(values.Position));

                    // Write the type table index to the value section.
                    Object value = e.Value;
                    if (value == null)
                    {
                        valuesWriter.Write7BitEncoded(-1);
                    }
                    else
                    {
                        valuesWriter.Write7BitEncoded
                            (types.IndexOf(value.GetType()));
                    }

                    // Write the value to the value section.
                    if (value != null)
                    {
                        WriteValue(values, valuesWriter, value);
                    }
                }

                // Write the type table.
                bw.Write(types.Count);
                foreach (Type t in types)
                {
                    bw.Write(t.AssemblyQualifiedName);
                }

                // Align on an 8-byte boundary.
                bw.Flush();
                while ((bw.BaseStream.Position & 7) != 0)
                {
                    bw.Write((byte)0);
                    bw.Flush();
                }

                // Sort the name hash and write it out.
                Array.Sort(nameHash, namePosition);
                foreach (int hash in nameHash)
                {
                    bw.Write(hash);
                }
                foreach (int pos in namePosition)
                {
                    bw.Write(pos);
                }

                // Write the offset of the value section.
                bw.Flush();
                namesWriter.Flush();
                valuesWriter.Flush();
                bw.Write((int)(bw.BaseStream.Position + names.Length + 4));

                // Write the name and value sections.
                bw.Write(names.GetBuffer(), 0, (int)(names.Length));
                bw.Write(values.GetBuffer(), 0, (int)(values.Length));
                names.Close();
                values.Close();
            }
            finally
            {
                generateDone = true;
                bw.Flush();
                ((IDisposable)bw).Dispose();
            }
        }