Example #1
0
        public void Read_NullBufferTest()
        {
            byte[]   b1    = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
            SqlBytes bytes = new SqlBytes(b1);

            byte[] b2 = null;

            Assert.Throws <ArgumentNullException>(() => bytes.Read(0, b2, 0, 10));
        }
Example #2
0
        public void Read_InvalidCountTest2()
        {
            byte[]   b1    = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
            SqlBytes bytes = new SqlBytes(b1);

            byte[] b2 = new byte[5];

            Assert.Throws <ArgumentOutOfRangeException>(() => bytes.Read(0, b2, 3, 4));
        }
Example #3
0
        public void Read_NegativeOffsetInBufferTest()
        {
            byte[]   b1    = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
            SqlBytes bytes = new SqlBytes(b1);

            byte[] b2 = new byte[5];

            Assert.Throws <ArgumentOutOfRangeException>(() => bytes.Read(0, b2, -1, 4));
        }
Example #4
0
        public void Read_SuccessTest1()
        {
            byte[]   b1    = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
            SqlBytes bytes = new SqlBytes(b1);

            byte[] b2 = new byte[10];

            bytes.Read(0, b2, 0, (int)bytes.Length);
            Assert.Equal(bytes.Value[5], b2[5]);
        }
        public void Read_InvalidCountTest2()
        {
            byte []  b1    = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
            SqlBytes bytes = new SqlBytes(b1);

            byte [] b2 = new byte [5];

            bytes.Read(0, b2, 3, 4);
            Assert.Fail("#12 Should throw ArgumentOutOfRangeException");
        }
        public void Read_NegativeCountTest()
        {
            byte []  b1    = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
            SqlBytes bytes = new SqlBytes(b1);

            byte [] b2 = new byte [5];

            bytes.Read(0, b2, 0, -1);
            Assert.Fail("#11 Should throw ArgumentOutOfRangeException");
        }
        public void Read_InvalidOffsetInBufferTest()
        {
            byte []  b1    = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
            SqlBytes bytes = new SqlBytes(b1);

            byte [] b2 = new byte [5];

            bytes.Read(0, b2, 8, 4);
            Assert.Fail("#6 Should throw ArgumentOutOfRangeException");
        }
        public void Read_NullBufferTest()
        {
            byte []  b1    = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
            SqlBytes bytes = new SqlBytes(b1);

            byte [] b2 = null;

            bytes.Read(0, b2, 0, 10);
            Assert.Fail("#2 Should throw ArgumentNullException");
        }
Example #9
0
 public static byte[] Range(SqlBytes binary, int from, int count)
 {
     if (binary.IsNull)
     {
         return(null);
     }
     byte[] buf = new byte[count];
     binary.Read(from, buf, 0, count);
     return(buf);
 }
Example #10
0
 public static byte[] Skip(SqlBytes binary, int count)
 {
     if (binary.IsNull)
     {
         return(null);
     }
     byte[] buf = new byte[binary.Length - count];
     binary.Read(count, buf, 0, buf.Length);
     return(buf);
 }
Example #11
0
        public void Read_SuccessTest2()
        {
            byte[]   b1    = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
            SqlBytes bytes = new SqlBytes(b1);

            byte[] b2 = new byte[10];

            bytes.Read(5, b2, 0, 10);
            Assert.Equal(bytes.Value[5], b2[0]);
            Assert.Equal(bytes.Value[9], b2[4]);
        }
Example #12
0
        public void Read_NullBufferAndInstanceValueTest()
        {
            ExceptionAssert.Throws <ArgumentNullException>(
                delegate
            {
                byte [] b2     = null;
                SqlBytes bytes = new SqlBytes();

                bytes.Read(0, b2, 8, 4);
                Assert.Fail("#10 Should throw ArgumentNullException");
            });
        }
Example #13
0
        // The Read/Write/ReadByte/WriteByte simply delegates to SqlBytes
        public override int Read(byte[] buffer, int offset, int count)
        {
            CheckIfStreamClosed();

            ValidateBufferArguments(buffer, offset, count);

            int iBytesRead = (int)_sb.Read(_lPosition, buffer, offset, count);

            _lPosition += iBytesRead;

            return(iBytesRead);
        }
Example #14
0
        public void Read_NullInstanceValueTest()
        {
            ExceptionAssert.Throws <SqlNullValueException>(
                delegate
            {
                byte [] b2     = new byte [5];
                SqlBytes bytes = new SqlBytes();

                bytes.Read(0, b2, 8, 4);
                Assert.Fail("#7 Should throw SqlNullValueException");
            });
        }
Example #15
0
        public void Read_InvalidCountTest1()
        {
            ExceptionAssert.Throws <ArgumentOutOfRangeException>(
                delegate
            {
                byte [] b1     = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
                SqlBytes bytes = new SqlBytes(b1);
                byte [] b2     = new byte [5];

                bytes.Read(0, b2, 0, 10);
                Assert.Fail("#3 Should throw ArgumentOutOfRangeException");
            });
        }
Example #16
0
        public void Read_NegativeOffsetInBufferTest()
        {
            ExceptionAssert.Throws <ArgumentOutOfRangeException>(
                delegate
            {
                byte [] b1     = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
                SqlBytes bytes = new SqlBytes(b1);
                byte [] b2     = new byte [5];

                bytes.Read(0, b2, -1, 4);
                Assert.Fail("#5 Should throw ArgumentOutOfRangeException");
            });
        }
        /// <summary>
        /// Extends Read so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// sqlbytes.Read(offset, buffer);
        /// </example>
        /// </summary>
        public static Int64 Read(this SqlBytes sqlbytes, Int64 offset, Byte[] buffer)
        {
            if (sqlbytes == null)
            {
                throw new ArgumentNullException("sqlbytes");
            }

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            return(sqlbytes.Read(offset, buffer, 0, buffer.Length));
        }
Example #18
0
 private static void PutBinary(WebRequest request, SqlBytes bytes, NameValueCollection attributes)
 {
     using (var stream = request.GetRequestStream())
     {
         long offset = 0L;
         var  buffer = new byte[SqlRuntime.IoBufferSize];
         while (offset < bytes.Length)
         {
             int read = (int)bytes.Read(offset, buffer, 0, buffer.Length);
             stream.Write(buffer, 0, read);
             offset += read;
         }
         stream.Flush();
     }
 }
Example #19
0
        public static SqlBytes Replicate(SqlBytes input, SqlInt64 count)
        {
            if (input.IsNull || count.IsNull)
            {
                return(SqlBytes.Null);
            }

            SqlBytes result = CreateResult(input.Length * count.Value);

            for (int offset = 0, length = (int)input.Length, c = (int)count.Value; c > 0; offset += length, c--)
            {
                input.Read(0L, result.Buffer, offset, length);
            }
            return(result);
        }
Example #20
0
        private static void Copy(SqlBytes source, long srcOffset, SqlBytes destination, long dstOffset, long length)
        {
            if (length == 0)
            {
                return;
            }
            byte[] buffer = new byte[Comparable.Min(SqlRuntime.IoBufferSize, length)];
            long   read   = 0;

            while (length > 0 && (read = source.Read(srcOffset, buffer, 0, (int)Comparable.Min(length, buffer.Length))) > 0L)
            {
                destination.Write(dstOffset, buffer, 0, (int)read);
                length    -= read;
                srcOffset += read;
                dstOffset += read;
            }
        }
Example #21
0
        /// <summary>
        /// Reads a varbinary column from the given reader.
        /// </summary>
        /// <param name="reader">Input reader.</param>
        /// <param name="colIndex">Index of the column.</param>
        /// <returns>Buffer representing the data value.</returns>
        internal static byte[] ReadSqlBytes(SqlDataReader reader, int colIndex)
        {
            Debug.Assert(reader != null);

            SqlBytes data = reader.GetSqlBytes(colIndex);

            if (data.IsNull)
            {
                return(null);
            }
            else
            {
                byte[] buffer = new byte[data.Length];

                data.Read(0, buffer, 0, (int)data.Length);

                return(buffer);
            }
        }
Example #22
0
        public static SqlGuid ToGuid(SqlBytes input, SqlInt64 index)
        {
            if (input.IsNull || index.IsNull)
            {
                return(SqlGuid.Null);
            }
            if (input.Length < SizeOfGuid)
            {
                throw new ArgumentException("Binary has too small size.", "input");
            }
            if (index.Value < 0 || index.Value > input.Length - SizeOfGuid)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            byte[] buffer = new byte[SizeOfGuid];
            input.Read(index.Value, buffer, 0, SizeOfGuid);
            return(new SqlGuid(buffer));
        }
Example #23
0
        public static SqlDateTime ToDateTime(SqlBytes input, SqlInt64 index)
        {
            if (input.IsNull || index.IsNull)
            {
                return(SqlDateTime.Null);
            }
            if (input.Length < sizeof(long))
            {
                throw new ArgumentException("Binary has too small size.", "input");
            }
            if (index.Value < 0 || index.Value > input.Length - sizeof(long))
            {
                throw new ArgumentOutOfRangeException("index");
            }

            byte[] buffer = new byte[sizeof(long)];
            input.Read(index.Value, buffer, 0, sizeof(long));
            return(new SqlDateTime(new DateTime(BitConverter.ToInt64(buffer, 0))));
        }
    public static SqlInt32 ExportBinaryColumn(SqlBytes binaryData, SqlString targetFile, SqlBoolean createPath)
    {
        byte[] buffer = new byte[65536];

        if (createPath.IsTrue)
        {
            string filePath = Path.GetDirectoryName(targetFile.Value);
            Directory.CreateDirectory(filePath);
        }

        long bytesRead;
        long dataIndex = 0;

        using (FileStream fs = File.Open(targetFile.Value, FileMode.Create))
        {
            bytesRead  = binaryData.Read(dataIndex, buffer, 0, buffer.Length);
            dataIndex += bytesRead;
            fs.Write(buffer, 0, (int)bytesRead);
        }

        return(new SqlInt32((int)dataIndex));
    }
Example #25
0
        public static SqlBytes Range(SqlBytes input, SqlInt64 index, SqlInt64 count)
        {
            if (input.IsNull)
            {
                return(SqlBytes.Null);
            }
            if (!index.IsNull && index.Value > input.Length)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            if (!count.IsNull && count.Value > input.Length - (index.IsNull ? 0 : index.Value))
            {
                throw new ArgumentOutOfRangeException("count");
            }

            long     indexValue = !index.IsNull ? index.Value : count.IsNull ? 0 : input.Length - count.Value;
            long     countValue = !count.IsNull ? count.Value : index.IsNull ? input.Length : input.Length - index.Value;
            SqlBytes result     = CreateResult(countValue);

            input.Read(indexValue, result.Buffer, 0, (int)countValue);
            return(result);
        }
Example #26
0
        public static void WriteSqlBytes(this Stream stream, SqlBytes bytes)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes");
            }

            long count  = bytes.Length;
            var  buffer = new byte[Comparable.Min(SqlRuntime.IoBufferSize, count)];
            long index  = 0L;

            while (count > 0)
            {
                int read = (int)bytes.Read(index, buffer, 0, (int)Comparable.Min(buffer.Length, count));
                stream.Write(buffer, 0, read);
                index += read;
                count -= read;
            }
        }
        // The Read/Write/ReadByte/WriteByte simply delegates to SqlBytes
        public override int Read(byte[] buffer, int offset, int count)
        {
            CheckIfStreamClosed();

            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (count < 0 || count > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            int iBytesRead = (int)_sb.Read(_lPosition, buffer, offset, count);

            _lPosition += iBytesRead;

            return(iBytesRead);
        }