Seek() public method

Seek to any location in the stream. Seeking past the end of the file is allowed. Any writes to that location will cause the file to grow to that size. Any holes that may be created from the seek will be zero filled on close.
public Seek ( long offset, SeekOrigin origin ) : long
offset long
origin SeekOrigin
return long
Beispiel #1
0
        public void TestNonSequentialWriteToOneChunk()
        {
            string         filename  = "nonsequential1.txt";
            GridFileStream gfs       = fs.Create(filename);
            Object         id        = gfs.GridFileInfo.Id;
            int            chunksize = gfs.GridFileInfo.ChunkSize;

            gfs.Seek(chunksize / 2, SeekOrigin.Begin);
            byte[] two = new byte[] { 2 };
            for (int i = chunksize; i > chunksize / 2; i--)
            {
                gfs.Write(two, 0, 1);
            }

            gfs.Seek(0, SeekOrigin.Begin);
            byte[] one = new byte[] { 1 };
            for (int i = 0; i < chunksize / 2; i++)
            {
                gfs.Write(one, 0, 1);
            }
            gfs.Close();

            Assert.AreEqual(1, CountChunks(filesystem, id));
            Document chunk = GrabChunk(id, 0);
            Binary   b     = (Binary)chunk["data"];

            Assert.AreEqual(2, b.Bytes[chunksize - 1]);
            Assert.AreEqual(2, b.Bytes[chunksize / 2]);
            Assert.AreEqual(1, b.Bytes[chunksize / 2 - 1]);
            Assert.AreEqual(1, b.Bytes[0]);
        }
Beispiel #2
0
        public void TestNonSequentialWriteToTwoChunks()
        {
            GridFileStream gfs = fs.Create("nonsequential2.txt");

            Object id       = gfs.GridFileInfo.Id;
            int    chunks   = 3;
            int    buffsize = 256 * 1024;

            for (int c = 0; c < chunks; c++)
            {
                Byte[] buff = CreateBuffer(buffsize, (byte)c);
                if (c == 2)
                {
                    gfs.Seek(0, SeekOrigin.Begin);     //On last chunk seek to start.
                }
                gfs.Write(buff, 0, buff.Length);
            }
            Assert.AreEqual(buffsize, gfs.Position, "Position is incorrect");
            gfs.Close();
            Assert.AreEqual(chunks - 1, CountChunks(filesystem, id));
            Document chunk = GrabChunk(id, 0);
            Binary   b     = (Binary)chunk["data"];

            Assert.AreEqual(2, b.Bytes[buffsize - 1]);
            Assert.AreEqual(2, b.Bytes[0]);
            chunk = GrabChunk(id, 1);
            b     = (Binary)chunk["data"];
            Assert.AreEqual(1, b.Bytes[buffsize - 1]);
        }
        public void TestCopy()
        {
            GridFile       fs  = new GridFile(db["tests"], "gfcopy");
            GridFileStream gfs = fs.Create("original.txt");

            gfs.WriteByte(1);
            gfs.Seek(1024 * 256 * 2, SeekOrigin.Begin);
            gfs.WriteByte(2);
            gfs.Close();
            fs.Copy("original.txt", "copy.txt");
            Assert.IsTrue(fs.Exists("original.txt"));
            Assert.IsTrue(fs.Exists("copy.txt"));
            //TODO Assert chunk data is the same too.
        }
Beispiel #4
0
        public void TestNonSequentialWriteToPartialChunk()
        {
            string         filename  = "nonsequentialpartial.txt";
            GridFileStream gfs       = fs.Create(filename);
            Object         id        = gfs.GridFileInfo.Id;
            int            chunksize = gfs.GridFileInfo.ChunkSize;

            gfs.Write(BitConverter.GetBytes(0), 0, 4);
            Assert.AreEqual(4, gfs.Position);
            gfs.Write(BitConverter.GetBytes(7), 0, 4);
            Assert.AreEqual(8, gfs.Position);
            gfs.Seek(0, SeekOrigin.Begin);
            gfs.Write(BitConverter.GetBytes(15), 0, 4);
            gfs.Close();

            Document chunk = GrabChunk(id, 0);
            Binary   b     = (Binary)chunk["data"];

            Assert.AreEqual(8, b.Bytes.Length);
        }
Beispiel #5
0
        public GridFileStream Open(FileMode mode, FileAccess access)
        {
            switch (mode)
            {
            case FileMode.Create:
                if (gridFile.Exists(this.FileName) == true)
                {
                    return(this.Open(FileMode.Truncate, access));
                }
                else
                {
                    return(this.Create(FileMode.CreateNew, access));
                }

            case FileMode.CreateNew:
                return(this.Create(mode, access));

            case FileMode.Open:
                LoadFileData();
                return(new GridFileStream(this, this.gridFile.Files, this.gridFile.Chunks, access));

            case FileMode.OpenOrCreate:
                if (gridFile.Exists(this.FileName) == false)
                {
                    return(this.Create(mode, access));
                }
                LoadFileData();
                return(new GridFileStream(this, this.gridFile.Files, this.gridFile.Chunks, access));

            case FileMode.Truncate:
                this.Truncate();
                return(new GridFileStream(this, this.gridFile.Files, this.gridFile.Chunks, access));

            case FileMode.Append:
                LoadFileData();
                GridFileStream gfs = new GridFileStream(this, this.gridFile.Files, this.gridFile.Chunks, access);
                gfs.Seek(0, SeekOrigin.End);
                return(gfs);
            }
            throw new NotImplementedException("Mode not implemented.");
        }
Beispiel #6
0
 public GridFileStream Open(FileMode mode, FileAccess access)
 {
     switch (mode) {
         case FileMode.Create:
             if(gridFile.Exists(this.FileName) == true){
                 return this.Open(FileMode.Truncate, access);
             }else{
                 return this.Create(FileMode.CreateNew, access);
             }
         case FileMode.CreateNew:
             return this.Create(mode, access);
         case FileMode.Open:
             LoadFileData();
             return new GridFileStream(this,this.gridFile.Files, this.gridFile.Chunks, access);
         case FileMode.OpenOrCreate:
             if(gridFile.Exists(this.FileName) == false) return this.Create(mode, access);
             LoadFileData();
             return new GridFileStream(this, this.gridFile.Files, this.gridFile.Chunks, access);
         case FileMode.Truncate:
             this.Truncate();
             return new GridFileStream(this,this.gridFile.Files, this.gridFile.Chunks, access);
         case FileMode.Append:
             LoadFileData();
             GridFileStream gfs = new GridFileStream(this,this.gridFile.Files, this.gridFile.Chunks, access);
             gfs.Seek(0,SeekOrigin.End);
             return gfs;
     }
     throw new NotImplementedException("Mode not implemented.");
 }