Close() public method

Close the stream and flush any changes to the database.
public Close ( ) : void
return void
Beispiel #1
0
        public void TestReadLengthIsSameAsWriteLength()
        {
            string         filename = "readwritelength.txt";
            GridFileStream gfs      = fs.Create(filename);
            int            length   = 0;

            for (int i = 1; i <= 50; i++)
            {
                gfs.Write(BitConverter.GetBytes(i), 0, 4);
                length += 4;
            }
            gfs.Close();
            Assert.AreEqual(length, gfs.GridFileInfo.Length, "File length written is not the same as in gridfileinfo");

            gfs = fs.OpenRead(filename);
            byte[] buffer     = new byte[16];
            int    read       = 0;
            int    readLength = read;

            while ((read = gfs.Read(buffer, 0, buffer.Length)) > 0)
            {
                readLength += read;
            }
            Assert.AreEqual(length, readLength, "Too much read back.");
        }
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]);
        }
Beispiel #3
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 #4
0
        public void TestWriteMultipleBytes()
        {
            GridFileStream gfs = fs.Create("multiplebytes.txt");
            Object         id  = gfs.GridFileInfo.Id;

            for (int x = 0; x < 256; x++)
            {
                gfs.Write(BitConverter.GetBytes(x), 0, 4);
            }
            gfs.Close();

            Assert.AreEqual(1, CountChunks(filesystem, id));
        }
        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 #6
0
        public void TestWriteTo3Chunks()
        {
            GridFileStream gfs = fs.Create("largewrite.txt");

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

            Byte[] buff = CreateBuffer(buffsize, (byte)6);
            gfs.Write(buff, 0, buff.Length);
            Assert.AreEqual(buff.Length, gfs.Position);
            gfs.Close();
            Assert.AreEqual(chunks, CountChunks(filesystem, id));
        }
Beispiel #7
0
        protected Object CreateDummyFile(string filename, int size, int chunksize, int initialOffset)
        {
            GridFileInfo gfi = new GridFileInfo(DB, "gfstream", filename);

            gfi.ChunkSize = chunksize;
            GridFileStream gfs = gfi.Create();
            Object         id  = gfs.GridFileInfo.Id;

            byte[] buff = CreateIntBuffer(size);
            gfs.Write(buff, initialOffset, buff.Length - initialOffset);
            gfs.Close();

            return(id);
        }
Beispiel #8
0
        public void TestSetLengthSmaller()
        {
            string filename  = "setlengthsmaller.txt";
            int    chunksize = 256 * 1024;
            int    chunks    = 4;
            int    size      = chunks * chunksize;
            int    newsize   = (size / 2) - (chunksize / 2);
            Object id        = this.CreateDummyFile(filename, size, chunksize, 0);

            GridFileStream gfs = fs.Open(filename, FileMode.Open, FileAccess.ReadWrite);

            gfs.SetLength(newsize);
            gfs.Close();
            Assert.AreEqual(newsize, gfs.GridFileInfo.Length);
        }
Beispiel #9
0
        public void TestSetLengthBigger()
        {
            string         filename = "setlengthbigger.txt";
            GridFileStream gfs      = fs.Create(filename);
            Object         id       = gfs.GridFileInfo.Id;
            long           length   = 256 * 1024 * 5;

            gfs.WriteByte(1);
            gfs.SetLength(length);
            gfs.WriteByte(2);
            gfs.Close();
            GridFileInfo gfi = new GridFileInfo(DB, filesystem, filename);

            Assert.AreEqual(length + 1, gfi.Length);
            Assert.AreEqual(6, CountChunks(filesystem, id));
        }
Beispiel #10
0
        public void TestWrite()
        {
            GridFileStream gfs = fs.Create("test.txt");
            Object         id  = gfs.GridFileInfo.Id;

            for (byte b = (byte)0; b < 128; b++)
            {
                gfs.WriteByte(b);
            }
            gfs.Close();

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

            Assert.AreEqual(127, bin.Bytes[127]);
            Assert.AreEqual(0, bin.Bytes[0]);
        }
Beispiel #11
0
        public void TestCreateExisting()
        {
            String         filename = "existing.txt";
            GridFile       gf       = new GridFile(DB, "gfcreate");
            GridFileInfo   gfi      = new GridFileInfo(DB, "gfcreate", filename);
            GridFileStream gfs      = gfi.Create();

            gfs.Close();

            bool thrown = false;

            try{
                gfi = new GridFileInfo(DB, "gfcreate", filename);
                gfi.Create();
            }catch (IOException) {
                thrown = true;
            }
            Assert.IsTrue(thrown, "Shouldn't be able to create the same file twice.");
        }
Beispiel #12
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 #13
0
        public void TestOpenReadOnly()
        {
            string         filename = "gfi-open.txt";
            GridFile       gf       = new GridFile(DB, "gfopen");
            GridFileStream gfs      = gf.Create(filename);

            gfs.Close();

            gfs = gf.OpenRead(filename);
            Assert.IsNotNull(gfs);
            bool thrown = false;

            try{
                gfs.Write(new byte[] { 0 }, 0, 1);
            }catch (System.NotSupportedException) {
                thrown = true;
            }catch (Exception ex) {
                Assert.Fail("Wrong exception thrown " + ex.GetType().Name);
            }
            Assert.IsTrue(thrown, "NotSupportedException not thrown");
        }
Beispiel #14
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");
            }
        }
Beispiel #15
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);
            }
        }