Write() public method

Copies from the source array into the grid file.
public Write ( byte array, int offset, int count ) : void
array byte /// A The source array to copy from. ///
offset int /// A The offset within the source array. ///
count int /// A The number of bytes from within the source array to copy. ///
return void
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 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 #3
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 #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 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));
        }
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);
        }
        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 #9
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 #10
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);
            }
        }