Esempio n. 1
0
        /// <summary>
        ///   Reads a stream of bytes from the specified column, starting at location indicated by <paramref name="dataOffset" />, into the buffer, starting at the location indicated by <paramref
        ///    name="bufferOffset" />.
        /// </summary>
        /// <param name="ordinal"> The zero-based column ordinal. </param>
        /// <param name="dataOffset"> The index within the row from which to begin the read operation. </param>
        /// <param name="buffer"> The buffer into which to copy the data. </param>
        /// <param name="bufferOffset"> The index with the buffer to which the data will be copied. </param>
        /// <param name="length"> The maximum number of characters to read. </param>
        /// <returns> The actual number of bytes read. </returns>
        public override long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffset, int length)
        {
            // [APIS-217] Check the index is correct.
            if (ordinal >= FieldCount || ordinal < 0)
            {
                throw new IndexOutOfRangeException();
            }

            if (currentRow > resultCount) //Are we at the end of the data?
            {
                throw new InvalidOperationException(Utils.GetStr(MsgId.BufferIndexMustBeValidIndexInBuffer));
            }

            object val  = GetValue(ordinal);
            string type = GetColumnTypeName(ordinal);

            if (type != "BLOB" && type != "CLOB" && type != "BIT" && type != "VARBIT")
            {
                throw new CUBRIDException(Utils.GetStr(MsgId.GetBytesCanBeCalledOnlyOnBinaryColumns));
            }

            // [APIS-217] Check the offset value is correct.
            if (dataOffset < 0)
            {
                throw new IndexOutOfRangeException(Utils.GetStr(MsgId.DataOffsetMustBeValidPositionInField));
            }

            // [APIS-217] If buffer is a null pointer, return 0.
            if (buffer == null)
            {
                return(0);
            }

            if (bufferOffset >= buffer.Length || bufferOffset < 0)
            {
                throw new IndexOutOfRangeException(Utils.GetStr(MsgId.BufferIndexMustBeValidIndexInBuffer));
            }

            if (buffer.Length < (bufferOffset + length))
            {
                throw new ArgumentException(Utils.GetStr(MsgId.BufferNotLargeEnoughToHoldRequestedData));
            }

            //[APIS-217] Does not determine the val is a NULL pointer.
            if (val == null)
            {
                return(0);
            }

            byte[] bytes;
            //[APIS-217] CUBRIDDataReader.GetBytes, threw an exception.

            if (type == "BIT" || type == "VARBIT")
            {
                bytes = (byte[])GetObject(ordinal);
                Debug.Assert(bytes != null, "bit != null");
                if (dataOffset < bytes.Length && dataOffset + length <= bytes.Length)
                {
                    for (long i = 0; i < length; i++)
                    {
                        buffer[i + bufferOffset] = bytes[i + dataOffset];
                    }
                }
                else
                {
                    throw new IndexOutOfRangeException(Utils.GetStr(MsgId.DataOffsetMustBeValidPositionInField));
                }

                return(length);
            }

            if (type == "BLOB")
            {
                CUBRIDBlob blob = val as CUBRIDBlob;
                // [APIS-217] Check the offset value is correct.
                Debug.Assert(blob != null, "blob != null");
                if (dataOffset < blob.BlobLength && dataOffset + length <= blob.BlobLength)
                {
                    bytes = blob.GetBytes(dataOffset + 1, length);
                }
                else
                {
                    throw new IndexOutOfRangeException(Utils.GetStr(MsgId.DataOffsetMustBeValidPositionInField));
                }
            }
            else // if it is a CLOB type.
            {
                CUBRIDClob clob = val as CUBRIDClob;
                Debug.Assert(clob != null, "clob != null");
                if (dataOffset < clob.ClobLength && dataOffset + length <= clob.ClobLength)
                {
                    bytes = conn.GetEncoding().GetBytes(clob.GetString(dataOffset + 1, length));
                }
                else
                {
                    throw new IndexOutOfRangeException(Utils.GetStr(MsgId.DataOffsetMustBeValidPositionInField));
                }
            }

            dataOffset = 0;

            Buffer.BlockCopy(bytes, (int)dataOffset, buffer, bufferOffset, length);

            return(length);
        }