Example #1
0
 public static void Skip(this Stream stream, long count, int buffSize, bool readMethod)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (count < 0)
     {
         throw new ArgumentOutOfRangeException("count");
     }
     if (buffSize <= 0)
     {
         throw new ArgumentOutOfRangeException("bufferSize");
     }
     if (count == 0)
     {
         return;
     }
     if (!readMethod)
     {
         stream.Position += count;
         return;
     }
     byte[] buffer = new byte[Comparable.Min(count, buffSize)];
     while (count > 0)
     {
         int read = stream.Read(buffer, 0, (int)Comparable.Min(count, buffer.Length));
         if (read == 0)
         {
             break;
         }
         count -= read;
     }
 }
Example #2
0
        public static SqlChars ReadSqlChars(this StreamReader streamReader, Int64 count)
        {
            if (streamReader == null)
            {
                throw new ArgumentNullException("streamReader");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            SqlChars chars  = new SqlChars(new char[count]);
            var      buffer = new char[Comparable.Min(SqlRuntime.IoBufferSize, count)];
            long     offset = 0L;

            chars.SetLength(0);
            while (!streamReader.EndOfStream && count > 0)
            {
                int read = streamReader.ReadBlock(buffer, 0, (int)Comparable.Min(buffer.Length, count));
                chars.SetLength(offset + read);
                chars.Write(offset, buffer, 0, read);
                offset += read;
                count  -= read;
            }
            return(chars);
        }
Example #3
0
        public override Int32 Read(Byte[] buffer, Int32 offset, Int32 count)
        {
            if (_ptr == IntPtr.Zero)
            {
                throw new ObjectDisposedException(null);
            }
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0 || offset >= buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (count < 0 || count > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            if (_length >= 0)
            {
                count = (int)Comparable.Min(_length - _position, count);
            }
            if (count > 0)
            {
                Marshal.Copy(new IntPtr(_ptr.ToInt64() + _offset + _position), buffer, offset, count);
                _position += count;
            }
            return(count);
        }
Example #4
0
        public static SqlBytes ReadSqlBytes(this Stream stream, Int64 count)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }

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

            bytes.SetLength(0);
            while (count > 0)
            {
                int read = stream.Read(buffer, 0, (int)Comparable.Min(buffer.Length, count));
                if (read == 0)
                {
                    break;
                }
                bytes.SetLength(index + read);
                bytes.Write(index, buffer, 0, read);
                index += read;
                count -= read;
            }
            return(bytes);
        }
Example #5
0
        public static void WriteSqlChars(this Stream stream, SqlChars chars, Encoding encoding)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (chars == null)
            {
                throw new ArgumentNullException("chars");
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }

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

            while (count > 0)
            {
                int read = (int)chars.Read(index, buffer, 0, (int)Comparable.Min(buffer.Length, count));
                stream.WriteChars(buffer.AsEnumerable(), encoding);
                index += read;
                count -= read;
            }
        }
Example #6
0
 public override void Write(byte[] buffer, int offset, int count)
 {
     _stream.Write(buffer, offset, count);
     if (count == 0)
     {
         return;
     }
     try
     {
         long position = _stream.Position;
         if (position == _position + count)
         {
             if (count > _buffer.Capacity - _buffer.Count)
             {
                 _buffer.RemoveRange(0, Comparable.Min(count - (_buffer.Capacity - _buffer.Count), _buffer.Count));
             }
             _buffer.AddRange(buffer.Skip(offset + Comparable.Max(count - _buffer.Capacity, 0)).Take(Comparable.Min(count, _buffer.Capacity - _buffer.Count)));
             _position = position;
             _offset   = _buffer.Count;
         }
     }
     catch (NotSupportedException)
     {
     }
 }
Example #7
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            try
            {
                long position = _stream.Position;
                if (position != _position - (_buffer.Count - _offset))
                {
                    _buffer.Clear();
                    _offset   = _buffer.Count;
                    _position = position;
                }
            }
            catch (NotSupportedException)
            {
            }
            int cached = Comparable.Min(count, _buffer.Count - _offset);

            if (cached > 0)
            {
                //buffer.MoveRange();
                buffer.Fill(i => _buffer[_offset + i], new Range(offset, cached));
            }
            int read = count - cached;

            if (read > 0)
            {
                read = _stream.Read(buffer, offset, read);
                if (read > _buffer.Capacity - _buffer.Count)
                {
                    _buffer.RemoveRange(0, Comparable.Min(read - (_buffer.Capacity - _buffer.Count), _buffer.Count));
                }
                _buffer.AddRange(buffer.Skip(offset + cached + Comparable.Max(0, read - (_buffer.Capacity - _buffer.Count))).Take(Comparable.Min(read, _buffer.Capacity - _buffer.Count)));
            }
            return(cached + read);
        }
Example #8
0
        public void SetRange(SqlInt64Array array, SqlInt32 index)
        {
            if (array.IsNull)
            {
                return;
            }

            int indexValue = index.IsNull ? _array.Length - Comparable.Min(_array.Length, array._array.Length) : index.Value;

            _array.Fill(i => array._array[i - indexValue]);
        }
        public void SetRange(SqlInt32 index, SqlHourAngleCollection range)
        {
            if (range.IsNull)
            {
                return;
            }

            int indexValue = index.IsNull ? _list.Count - Comparable.Min(_list.Count, range._list.Count) : index.Value;

            _list.SetRange(indexValue, range.List);
        }
Example #10
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (_available == 0 || count == 0)
            {
                return(0);
            }

            int read = _stream.Read(buffer, offset, (int)Comparable.Min(_available, count));

            _available -= read;
            return(read);
        }
Example #11
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (count > _available)
            {
                throw new InvalidOperationException("Exceeded write operation limit.");
            }

            if (_available > 0 && count > 0)
            {
                int write = (int)Comparable.Min(_available, count);
                _stream.Write(buffer, offset, write);
                _available -= write;
            }
        }
Example #12
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 #13
0
        public static int Compare(this Stream xStream, Stream yStream, long count, int buffSize, Comparison <byte> comparison)
        {
            if (xStream == null)
            {
                throw new ArgumentNullException("xStream");
            }
            if (yStream == null)
            {
                throw new ArgumentNullException("yStream");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }
            if (buffSize <= 0)
            {
                throw new ArgumentOutOfRangeException("buffSize");
            }
            if (comparison == null)
            {
                comparison = Comparer <byte> .Default.Compare;
            }

            byte[] xBuffer = new byte[buffSize];
            byte[] yBuffer = new byte[buffSize];
            long   total = 0;
            int    xRead, yRead, result;

            do
            {
                xRead  = xStream.Read(xBuffer, 0, (int)Comparable.Min(xBuffer.Length, count - total));
                yRead  = yStream.Read(yBuffer, 0, (int)Comparable.Min(yBuffer.Length, count - total));
                result = xBuffer.Take(xRead).SequenceCompare(yBuffer.Take(yRead), comparison);
                if (result == 0)
                {
                    total += Comparable.Max(xRead, yRead);
                }
            }while (result == 0 && xRead > 0 && yRead > 0 && total < count);
            return(result);
        }
Example #14
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;
            }
        }
Example #15
0
        public static void WriteSqlChars(this StreamWriter streamWriter, SqlChars chars)
        {
            if (streamWriter == null)
            {
                throw new ArgumentNullException("streamWriter");
            }
            if (chars == null)
            {
                throw new ArgumentNullException("chars");
            }

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

            while (count > 0)
            {
                int read = (int)chars.Read(index, buffer, 0, (int)Comparable.Min(buffer.Length, count));
                streamWriter.Write(buffer, 0, read);
                index += read;
                count -= read;
            }
        }
Example #16
0
        public static SqlChars ReadSqlChars(this Stream stream, Encoding encoding, Int64 count)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            SqlChars chars  = new SqlChars(new char[count]);
            var      buffer = new char[Comparable.Min(SqlRuntime.IoBufferSize, count)];

            using (var e = stream.ReadChars(encoding).Take(count).GetEnumerator())
            {
                long offset = 0L;
                int  read   = 0;
                chars.SetLength(0);
                do
                {
                    read = buffer.Fill(e);
                    if (read > 0)
                    {
                        chars.SetLength(offset + read);
                        chars.Write(offset, buffer, 0, read);
                        offset += read;
                    }
                }while (read == buffer.Length);
            }
            return(chars);
        }
Example #17
0
        public static SqlBoolean EqualRange(SqlBytes xData, SqlInt64 xIndex, SqlBytes yData, SqlInt64 yIndex, SqlInt64 count)
        {
            if (xData.IsNull || yData.IsNull)
            {
                return(SqlBoolean.Null);
            }

            long xIndexValue = !xIndex.IsNull ? xIndex.Value : count.IsNull ? 0 : xData.Length - Comparable.Min(count.Value, xData.Length);
            long yIndexValue = !yIndex.IsNull ? yIndex.Value : count.IsNull ? 0 : yData.Length - Comparable.Min(count.Value, yData.Length);
            long countValue  = !count.IsNull ? count.Value : int.MaxValue;

            return(xData.Buffer.SequenceEqual((int)xIndexValue, yData.Buffer, (int)yIndexValue, (int)countValue));
        }
Example #18
0
        public SqlChars ReadTextByCpName(SqlInt64 offset, SqlInt64 count, SqlBoolean detectEncoding, [SqlFacet(MaxSize = 128)] SqlString cpName)
        {
            if (IsNull)
            {
                return(SqlChars.Null);
            }

            var fileEncoding = cpName.IsNull ? SqlRuntime.FileEncoding : Encoding.GetEncoding(cpName.Value);

            using (var fs = _fi.OpenRead())
            {
                if (detectEncoding.IsTrue)
                {
                    var preamble = EncodingPreamble.Detect(fs.ReadBytesMost(1, 4) ?? new byte[0]);
                    if (preamble != null)
                    {
                        fileEncoding = Encoding.GetEncoding(preamble.CodePage);
                    }
                    else
                    {
                        fs.Position = 0;
                    }
                }
                if (!offset.IsNull)
                {
                    fs.Position = offset.Value;
                }
                return(fs.ReadSqlChars(fileEncoding, !count.IsNull ? count.Value : (fs.Length - fs.Position).Yield(t => (int)Comparable.Min(t, int.MaxValue), (t, r) => t - r, t => t > 0).Aggregate(0L, (t, p) => t + fileEncoding.GetMaxCharCount(p))));
            }
        }
Example #19
0
        public SqlChars ReadAllTextByCpId(SqlBoolean detectEncoding, SqlInt32 cpId)
        {
            if (IsNull)
            {
                return(SqlChars.Null);
            }

            var fileEncoding = cpId.IsNull ? SqlRuntime.FileEncoding : Encoding.GetEncoding(cpId.Value);

            using (var fs = _fi.OpenRead())
            {
                if (detectEncoding.IsTrue)
                {
                    var preamble = EncodingPreamble.Detect(fs.ReadBytesMost(1, 4) ?? new byte[0]);
                    if (preamble != null)
                    {
                        fileEncoding = Encoding.GetEncoding(preamble.CodePage);
                    }
                    else
                    {
                        fs.Position = 0;
                    }
                }
                return(fs.ReadSqlChars(fileEncoding, (fs.Length - fs.Position).Yield(t => (int)Comparable.Min(t, int.MaxValue), (t, r) => t - r, t => t > 0).Aggregate(0L, (t, p) => t + fileEncoding.GetMaxCharCount(p))));
            }
        }
Example #20
0
        public static long Move(this Stream stream, long destination, long count, int buffSize, ExpandMethod expandMethod, IProgress <long> progress, long unitSize)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            long source = stream.Position;

            if (destination < 0)
            {
                throw new ArgumentOutOfRangeException("destination");
            }
            if (count < 0 || count > stream.Length - source)
            {
                throw new ArgumentOutOfRangeException("count");
            }
            if (buffSize <= 0)
            {
                throw new ArgumentOutOfRangeException("bufferSize");
            }
            if (unitSize < 0)
            {
                throw new ArgumentOutOfRangeException("unitSize");
            }

            bool reverse = source < destination;
            long total   = 0L;
            long before  = 0L;
            int  read;

            byte[] buffer = new byte[buffSize];
            if (reverse)
            {
                if (stream.Length < destination + count)
                {
                    switch (expandMethod)
                    {
                    case ExpandMethod.SetLength:
                        stream.SetLength(destination + count);
                        break;

                    case ExpandMethod.WriteZero:
                    {
                        stream.Position = stream.Length;
                        long length = destination + count - stream.Length;
                        while (length > 0L)
                        {
                            int block = (int)Comparable.Min(length, (long)filler.Length);
                            stream.Write(filler, 0, block);
                            length -= block;
                        }
                    }
                    break;
                    }
                }
                stream.Position = source + count - Comparable.Min(count, buffer.Length);
            }
            while (total < count && (read = stream.Read(buffer, 0, (int)Comparable.Min(count - total, buffer.Length))) > 0)
            {
                stream.Position = destination + (reverse ? count - total - read : total);
                stream.Write(buffer, 0, read);
                stream.Position = source + (reverse ? count - total - read : total);
                total          += read;
                if (progress != null && total - before >= unitSize)
                {
                    before = total;
                    progress.Report(total);
                }
            }
            return(total);
        }
Example #21
0
        public static long Copy(this Stream srcStream, Stream dstStream, long count, int buffSize, IProgress <long> progress, long unitSize)
        {
            if (srcStream == null)
            {
                throw new ArgumentNullException("srcStream");
            }
            if (dstStream == null)
            {
                throw new ArgumentNullException("dstStream");
            }
            if (srcStream == dstStream)
            {
                throw new ArgumentException("Both streams are same");
            }
            if (buffSize <= 0)
            {
                throw new ArgumentOutOfRangeException("buffSize");
            }
            if (unitSize < 0)
            {
                throw new ArgumentOutOfRangeException("unitSize");
            }
            if (!srcStream.CanRead)
            {
                if (!srcStream.CanWrite)
                {
                    throw new ObjectDisposedException("srcStream");
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
            if (!dstStream.CanWrite)
            {
                if (!dstStream.CanRead)
                {
                    throw new ObjectDisposedException(null);
                }
                else
                {
                    throw new NotSupportedException("dstStream");
                }
            }

            long total  = 0;
            long before = 0;
            int  read;

            byte[] buffer = new byte[buffSize];
            while (total < count && (read = srcStream.Read(buffer, 0, (int)Comparable.Min(count - total, buffer.Length))) != 0)
            {
                dstStream.Write(buffer, 0, read);
                total += read;
                if (progress != null && total - before >= unitSize)
                {
                    before = total;
                    progress.Report(total);
                }
            }
            return(total);
        }