Read() public method

Reads data from the stream into the specified array. It will fill the array in starting at offset and adding count bytes returning the number of bytes read from the stream.
public Read ( byte array, int offset, int count ) : int
array byte
offset int
count int
return int
Example #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.");
        }
Example #2
0
        public void TestWriteMultipleBytesWithOffset()
        {
            String filename  = "multioffset.txt";
            int    offset    = 4;
            int    chunks    = 2;
            int    chunksize = 100;
            int    size      = chunks * chunksize;

            Object id = CreateDummyFile(filename, size, chunksize, offset);

            Assert.AreEqual(2, CountChunks(filesystem, id));

            GridFileStream gfs = fs.OpenRead(filename);

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

            for (int i = 1; i <= size / 4 - offset; 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 #3
0
        public void TestReadIntoBufferBiggerThanChunk()
        {
            string filename = "largereadbuffer.txt";
            int    size     = 100;
            Object id       = CreateDummyFile(filename, size, 50, 0);

            GridFileStream gfs = fs.OpenRead(filename);

            byte[] buff = new byte[size];
            int    read = gfs.Read(buff, 0, size);

            Assert.AreEqual(size, read, "Not all bytes read back from file.");
            int expected = 0;

            for (int i = 0; i < size; i += 4)
            {
                int val = BitConverter.ToInt32(buff, i);
                Assert.AreEqual(expected, val, "Val was not same as expected. Pos: " + i);
                expected++;
            }
        }
Example #4
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 #5
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 #6
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);
            }
        }