Ejemplo n.º 1
0
        private void Load()
        {
            if (TheStore.TheFile.Length >= BitmapToPos(Ix + 1))
            {
                TheStore.TheFile.Position = BitmapToPos(Ix);

                if (StreamUtils.ReadInt8(TheStore.TheFile) != (byte)Store.SectorTypes.Bitmap)
                {
                    InitializeBitmapSector();
                    return;
                }

                var nextix = StreamUtils.ReadInt32(TheStore.TheFile);
                SectorData = StreamUtils.Read(TheStore.TheFile, (int)TheStore.SectorDataSize);

                if (nextix != Store.LAST_SECTOR_IN_CHAIN)
                {
                    NextBitmap = new BitmapSector(TheStore, nextix, SectorSize);
                }
                else
                {
                    NextBitmap = null;
                }
            }
            else
            {
                InitializeBitmapSector();
            }
        }
Ejemplo n.º 2
0
        private byte[] ReadInternal(int ix, long maxlen)
        {
            TheFile.Position = BitmapToPos(ix);

            var sectortype = (Store.SectorTypes)StreamUtils.ReadInt8(TheFile);

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

            if (maxlen > 0)
            {
                totallen = Math.Min(totallen, maxlen);
            }

            var result    = new byte[totallen];
            var resultpos = 0;
            var buflen    = FirstSectorDataSize;

            while (resultpos < totallen)
            {
                var len     = (int)Math.Min(totallen - resultpos, buflen);
                var readlen = TheFile.Read(result, resultpos, len);

                if (nextsector == LAST_SECTOR_IN_CHAIN)
                {
                    var curlen = resultpos + readlen;
                    if (totallen > curlen)
                    {
                        DebugUtils.LogWarning("Store: Warning! Stored length of the chain is faulty!");
                        return(result.Copy(0, curlen));
                    }
                    break;
                }
                TheFile.Position = BitmapToPos(nextsector);

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

                resultpos += readlen;
                buflen     = SectorDataSize;
            }

            return(result);
        }
Ejemplo n.º 3
0
            public StoreStream(Store store, int ix, long offset)
            {
                TheStore = store;
                Ix       = ix;

                if (offset < 0)
                {
                    throw new ArgumentException("offset must be >= 0!");
                }
                PositionOffset = offset;

                var file = TheStore.TheFile;

                // Get current state
                file.Position = TheStore.BitmapToPos(ix);

                var sectortype = (Store.SectorTypes)StreamUtils.ReadInt8(file);

                if (sectortype != Store.SectorTypes.Data)
                {
                    throw new Exception("Trying to read in non data area");
                }
                var nextsector    = StreamUtils.ReadInt32(file);
                var CurrentLength = StreamUtils.ReadInt64(file);

                while (sectortype == SectorTypes.Data || sectortype == SectorTypes.Continuation)
                {
                    Sectors.Add(ix);

                    if (nextsector == LAST_SECTOR_IN_CHAIN)
                    {
                        break;
                    }
                    file.Position = TheStore.BitmapToPos(nextsector);
                    ix            = nextsector;

                    sectortype = (Store.SectorTypes)StreamUtils.ReadInt8(file);
                    if (sectortype != Store.SectorTypes.Data && sectortype != SectorTypes.Continuation)
                    {
                        throw new Exception("Trying to read in non data area");
                    }
                    nextsector = StreamUtils.ReadInt32(file);
                }
            }
Ejemplo n.º 4
0
        public long GetDataLength(int ix)
        {
            if (!Bits[ix])
            {
                return(0);             // Free
            }
            TheFile.Position = BitmapToPos(ix);

            var sectortype = StreamUtils.ReadInt8(TheFile);

            if ((Store.SectorTypes)sectortype != Store.SectorTypes.Data)
            {
                return(0);
            }

            var nextsector = StreamUtils.ReadInt32(TheFile);

            return(StreamUtils.ReadInt64(TheFile));
        }
Ejemplo n.º 5
0
        public void Delete(int ix)
        {
            if (ix < RESERVED_SECTORS)
            {
                throw new Exception("Cannot delete reserved indexes!");
            }
            if (!Bits[ix])
            {
                return;
            }

            TheFile.Position = BitmapToPos(ix);

            var sectortype = (Store.SectorTypes)StreamUtils.ReadInt8(TheFile);

            if (sectortype != Store.SectorTypes.Data)
            {
                throw new Exception("Index is not pointing to a data index!");
            }

            DeleteFrom(ix);
        }
Ejemplo n.º 6
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;
                    }
                }
            }
        }