Stream for reading and writing to a file in GridFS.
When using the stream for random io it is possible to produce chunks in the begining and middle of the file that are not full size followed by other chunks that are full size. This only affects the md5 sum that is calculated on the file on close. Because of this do not rely on the md5 sum of a file when doing random io. Writing to the stream sequentially works fine and will produce a consistent md5.
Inheritance: Stream
Example #1
0
        public void TestReadFrom3Chunks()
        {
            string filename  = "read3chunks.txt";
            int    chunks    = 3;
            int    chunkSize = 256 * 1024;
            int    size      = (256 * 1024 * chunks) - 5000;


            Object id = CreateDummyFile(filename, size, chunkSize, 0);


            using (GridFileStream gfs = fs.OpenRead(filename)){
                int    buffsize  = 10240;
                Byte[] buff      = new Byte[buffsize];
                int    read      = 0;
                int    totalRead = 0;
                while ((read = gfs.Read(buff, 0, buffsize)) != 0)
                {
                    totalRead += read;
                }
                Assert.AreEqual(size, totalRead, "Not all bytes read back");
            }
        }
Example #2
0
        public void TestRead()
        {
            string         filename = "readme.txt";
            GridFileStream gfs      = fs.Create(filename);

            for (int i = 1; i <= 50; i++)
            {
                gfs.Write(BitConverter.GetBytes(i), 0, 4);
            }
            gfs.Close();

            gfs = fs.OpenRead(filename);

            Byte[] buff = new Byte[4];
            int    read;

            for (int i = 1; i <= 50; i++)
            {
                read = gfs.Read(buff, 0, 4);
                Assert.AreEqual(4, read, "Not enough bytes were read");
                Assert.AreEqual(i, BitConverter.ToInt32(buff, 0), "value read back was not the same as written");
            }
        }
Example #3
0
        public void TestWriteWithMultipleFlushes()
        {
            string         filename = "multiflush.txt";
            GridFileStream gfs      = fs.Create(filename);
            Object         id       = gfs.GridFileInfo.Id;
            int            size     = gfs.GridFileInfo.ChunkSize * 2;

            byte[] buff;

            int x = 0;

            for (int i = 0; i < size; i += 4)
            {
                buff = BitConverter.GetBytes(x);
                gfs.Write(buff, 0, buff.Length);
                x++;
                if (i % size / 4 == 0)
                {
                    gfs.Flush();
                }
            }
            gfs.Close();

            gfs = fs.OpenRead(filename);
            int read;
            int val;

            buff = new byte[4];
            for (int i = 0; i < size / 4; i++)
            {
                read = gfs.Read(buff, 0, 4);
                val  = BitConverter.ToInt32(buff, 0);
                Assert.AreEqual(4, read, "Not enough bytes were read. Pos: " + gfs.Position);
                Assert.AreEqual(i, val, "value read back was not the same as written. Pos: " + gfs.Position);
            }
        }
Example #4
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.");
 }