Esempio n. 1
0
        static TeaFile OpenRead(Stream stream, bool ownsStream)
        {
            var tf = new TeaFile();

            try
            {
                tf.core = new TeaFileCore(stream, ownsStream);
                tf.core.ReadHeader();

                //	protect against empty Item structs
                if (tf.Description.ItemDescription != null)
                {
                    if (!tf.Description.ItemDescription.Fields.Any())
                    {
                        throw new ItemException("Cannot read this file because the item has no fields according to its item description in the file.");
                    }
                }

                tf.reader = new BinaryReader(tf.core.Stream);

                if (tf.Description.ItemDescription != null)
                {
                    // A field might occupy equal or more bytes than the size of its type due to padding bytes
                    // inserted between the fields or between items in the ItemArray. The paddingBytes array
                    // holds the number of bytes that will be read after reading a field in order to set the
                    // file pointer to the next field.
                    tf.fieldSpacings = tf.GetFieldSpacings();
                }

                return(tf);
            }
            catch
            {
                tf.Dispose(); // since we will not hand out the disposable TeaFile instance,
                // we must close the filestream if header reading failed!
                throw;
            }
        }
Esempio n. 2
0
        static TeaFile OpenRead(Stream stream, bool ownsStream)
        {
            var tf = new TeaFile();
            try
            {
                tf.core = new TeaFileCore(stream, ownsStream);
                tf.core.ReadHeader();

                //	protect against empty Item structs
                if (tf.Description.ItemDescription != null)
                {
                    if (!tf.Description.ItemDescription.Fields.Any())
                    {
                        throw new ItemException("Cannot read this file because the item has no fields according to its item description in the file.");
                    }
                }

                tf.reader = new BinaryReader(tf.core.Stream);

                if (tf.Description.ItemDescription != null)
                {
                    // A field might occupy equal or more bytes than the size of its type due to padding bytes
                    // inserted between the fields or between items in the ItemArray. The paddingBytes array
                    // holds the number of bytes that will be read after reading a field in order to set the
                    // file pointer to the next field.
                    tf.fieldSpacings = tf.GetFieldSpacings();
                }

                return tf;
            }
            catch
            {
                tf.Dispose(); // since we will not hand out the disposable TeaFile instance,
                // we must close the filestream if header reading failed!
                throw;
            }
        }