Ejemplo n.º 1
0
        private void WriteInternal(IEnumerable <BufLen> datablocks, int ix)
        {
            var thissector = ix;

            if (!Bits[ix])
            {
                throw new Exception("Cannot update an unallocated sector!");
            }
            TheFile.Position = BitmapToPos(thissector);

            if ((Store.SectorTypes)StreamUtils.ReadInt8(TheFile) != Store.SectorTypes.Data)
            {
                throw new Exception("Trying to write in non data area");
            }
            var nextsector = StreamUtils.ReadInt32(TheFile);

            TheFile.WriteInt64(datablocks.Sum(r => (long)r.Length));

            var sectorspaceleft = FirstSectorDataSize;

            foreach (var datab in datablocks)
            {
                var data = (BufRefLen)datab;

                while (data.Length > 0)
                {
                    var len = (int)Math.Min(data.Length, sectorspaceleft);
                    TheFile.Write(data.BaseArray, data.BaseArrayOffset, len);

                    data.Seek(len);
                    sectorspaceleft -= len;

                    if (data.Length == 0)
                    {
                        break;
                    }

                    if (sectorspaceleft == 0)
                    {
                        if (nextsector != LAST_SECTOR_IN_CHAIN)
                        {
                            if (!Bits[nextsector])
                            {
                                throw new Exception("Cannot update an unallocated sector!");
                            }
                            TheFile.Position = BitmapToPos(nextsector);

                            if ((Store.SectorTypes)StreamUtils.ReadInt8(TheFile) != Store.SectorTypes.Continuation)
                            {
                                throw new Exception("Trying to update a sector outside of the allocated sector chain!");
                            }

                            thissector = nextsector;
                            nextsector = StreamUtils.ReadInt32(TheFile);
                        }
                        else
                        {
                            thissector = ExtendLastSector(thissector);
                            nextsector = LAST_SECTOR_IN_CHAIN;
                        }

                        sectorspaceleft = SectorDataSize;
                    }
                }
            }
        }