Esempio n. 1
0
        public override System.Int32 Read(System.Byte[] buffer, System.Int32 offset, System.Int32 count)
        {
            // According to MS documentation, any implementation of the IO.Stream.Read function must:
            // (a) throw an exception if offset & count reference an invalid part of the buffer,
            //     or if count < 0, or if buffer is null
            // (b) return 0 only upon EOF, or if count = 0
            // (c) if not EOF, then return at least 1 byte, up to <count> bytes

            if (_streamMode == StreamMode.Undefined)
            {
                if (!this._stream.CanRead) throw new ZlibException("The stream is not readable.");
                // for the first read, set up some controls.
                _streamMode = StreamMode.Reader;
                // (The first reference to _z goes through the private accessor which
                // may initialize it.)
                z.AvailableBytesIn = 0;
                if (_flavor == ZlibStreamFlavor.GZIP)
                {
                    _gzipHeaderByteCount = _ReadAndValidateGzipHeader();
                    // workitem 8501: handle edge case (decompress empty stream)
                    if (_gzipHeaderByteCount == 0)
                        return 0;
                }
            }

            if (_streamMode != StreamMode.Reader)
                throw new ZlibException("Cannot Read after Writing.");

            if (count == 0) return 0;
            // workitem 10562
            // this quits too early if the input buffer has been consumed but
            // there's still output which hasn't been created yet (e.g. block
            // data for tables / tree, or the trailing adler32 data). we
            // need to wait for a Z_STREAM_END from Deflate instead.
            //if (nomoreinput && _wantCompress) return 0;  // workitem 8557
            if (buffer == null) throw new ArgumentNullException("buffer");
            if (count < 0) throw new ArgumentOutOfRangeException("count");
            if (offset < buffer.GetLowerBound(0)) throw new ArgumentOutOfRangeException("offset");
            if ((offset + count) > buffer.GetLength(0)) throw new ArgumentOutOfRangeException("count");

            int rc = 0;

            // set up the output of the deflate/inflate codec:
            _z.OutputBuffer = buffer;
            _z.NextOut = offset;
            _z.AvailableBytesOut = count;

            // This is necessary in case _workingBuffer has been resized. (new byte[])
            // (The first reference to _workingBuffer goes through the private accessor which
            // may initialize it.)
            _z.InputBuffer = workingBuffer;

            do
            {
                // need data in _workingBuffer in order to deflate/inflate.  Here, we check if we have any.
                if ((_z.AvailableBytesIn == 0) && (!nomoreinput))
                {
                    // No data available, so try to Read data from the captive stream.
                    _z.NextIn = 0;
                    _z.AvailableBytesIn = _stream.Read(_workingBuffer, 0, _workingBuffer.Length);
                    if (_z.AvailableBytesIn == 0)
                        nomoreinput = true;

                }
                // workitem 10562
                // if we've consumed all the input then we need to generate any
                // remaining block data and checksums and put them in the pending array
                if (nomoreinput) _flushMode = FlushType.Finish;
                // we have data in InputBuffer; now compress or decompress as appropriate
                rc = (_wantCompress)
                    ? _z.Deflate(_flushMode)
                    : _z.Inflate(_flushMode);

                if (nomoreinput && (rc == ZlibConstants.Z_BUF_ERROR))
                    return 0;

                if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
                    throw new ZlibException(String.Format("{0}flating:  rc={1}  msg={2}", (_wantCompress ? "de" : "in"), rc, _z.Message));

                if ((nomoreinput || rc == ZlibConstants.Z_STREAM_END) && (_z.AvailableBytesOut == count))
                {
                    // workitem 10562
                    // we've genuinely reached the end of the output stream now,
                    // including any block data and adler32 which appears after
                    // the compressed input data. we don't have any more bytes
                    // to return so we can stop processing
                    return 0; // nothing more to read
                };
            }
            //while (_z.AvailableBytesOut == count && rc == ZlibConstants.Z_OK);
            //while (_z.AvailableBytesOut > 0 && !nomoreinput && rc == ZlibConstants.Z_OK);
            while (_z.AvailableBytesOut > 0 && rc == ZlibConstants.Z_OK);

            // workitem 10562
            // the following is no longer required as we now call _z.Deflate
            // in the main loop above instead
            //// workitem 8557
            //// is there more room in output?
            //if (_z.AvailableBytesOut > 0)
            //{
            //    if (rc == ZlibConstants.Z_OK && _z.AvailableBytesIn == 0)
            //    {
            //        // deferred
            //    }
            //
            //    // are we completely done reading?
            //    if (nomoreinput)
            //    {
            //        // and in compression?
            //        if (_wantCompress)
            //        {
            //            // no more input data available; therefore we flush to
            //            // try to complete the read
            //            rc = _z.Deflate(FlushType.Finish);
            //
            //            if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
            //                throw new ZlibException(String.Format("Deflating:  rc={0}  msg={1}", rc, _z.Message));
            //        }
            //    }
            //}

            rc = (count - _z.AvailableBytesOut);

            // calculate CRC after reading
            if (crc != null)
                crc.SlurpBlock(buffer, offset, rc);

            return rc;
        }
Esempio n. 2
0
        public override System.Int32 Read(System.Byte[] buffer, System.Int32 offset, System.Int32 count)
        {
            // According to MS documentation, any implementation of the IO.Stream.Read function must:
            // (a) throw an exception if offset & count reference an invalid part of the buffer,
            //     or if count < 0, or if buffer is null
            // (b) return 0 only upon EOF, or if count = 0
            // (c) if not EOF, then return at least 1 byte, up to <count> bytes

            if (_streamMode == StreamMode.Undefined)
            {
                if (!this._stream.CanRead) throw new ZlibException("The stream is not readable.");
                // for the first read, set up some controls.
                _streamMode = StreamMode.Reader;
                // (The first reference to _z goes through the private accessor which
                // may initialize it.)
                z.AvailableBytesIn = 0;
                if (_flavor == ZlibStreamFlavor.GZIP)
                {
                    _gzipHeaderByteCount = _ReadAndValidateGzipHeader();
                    // workitem 8501: handle edge case (decompress empty stream)
                    if (_gzipHeaderByteCount == 0)
                        return 0;
                }
            }

            if (_streamMode != StreamMode.Reader)
                throw new ZlibException("Cannot Read after Writing.");

            if (count == 0) return 0;
            if (nomoreinput && _wantCompress) return 0;  // workitem 8557
            if (buffer == null) throw new ArgumentNullException("buffer");
            if (count < 0) throw new ArgumentOutOfRangeException("count");
            if (offset < buffer.GetLowerBound(0)) throw new ArgumentOutOfRangeException("offset");
            if ((offset + count) > buffer.GetLength(0)) throw new ArgumentOutOfRangeException("count");

            int rc = 0;

            // set up the output of the deflate/inflate codec:
            _z.OutputBuffer = buffer;
            _z.NextOut = offset;
            _z.AvailableBytesOut = count;

            // This is necessary in case _workingBuffer has been resized. (new byte[])
            // (The first reference to _workingBuffer goes through the private accessor which
            // may initialize it.)
            _z.InputBuffer = workingBuffer;

            do
            {
                // need data in _workingBuffer in order to deflate/inflate.  Here, we check if we have any.
                if ((_z.AvailableBytesIn == 0) && (!nomoreinput))
                {
                    // No data available, so try to Read data from the captive stream.
                    _z.NextIn = 0;
                    _z.AvailableBytesIn = _stream.Read(_workingBuffer, 0, _workingBuffer.Length);
                    if (_z.AvailableBytesIn == 0)
                        nomoreinput = true;

                }
                // we have data in InputBuffer; now compress or decompress as appropriate
                rc = (_wantCompress)
                    ? _z.Deflate(_flushMode)
                    : _z.Inflate(_flushMode);

                if (nomoreinput && (rc == ZlibConstants.Z_BUF_ERROR))
                    return 0;

                if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
                    throw new ZlibException(String.Format("{0}flating:  rc={1}  msg={2}", (_wantCompress ? "de" : "in"), rc, _z.Message));

                if ((nomoreinput || rc == ZlibConstants.Z_STREAM_END) && (_z.AvailableBytesOut == count))
                    break; // nothing more to read
            }
            //while (_z.AvailableBytesOut == count && rc == ZlibConstants.Z_OK);
            while (_z.AvailableBytesOut > 0 && !nomoreinput && rc == ZlibConstants.Z_OK);


            // workitem 8557
            // is there more room in output?
            if (_z.AvailableBytesOut > 0)
            {
                if (rc == ZlibConstants.Z_OK && _z.AvailableBytesIn == 0)
                {
                    // deferred
                }

                // are we completely done reading?
                if (nomoreinput)
                {
                    // and in compression?
                    if (_wantCompress)
                    {
                        // no more input data available; therefore we flush to
                        // try to complete the read
                        rc = _z.Deflate(FlushType.Finish);

                        if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
                            throw new ZlibException(String.Format("Deflating:  rc={0}  msg={1}", rc, _z.Message));
                    }
                }
            }


            rc = (count - _z.AvailableBytesOut);

            // calculate CRC after reading
            if (crc != null)
                crc.SlurpBlock(buffer, offset, rc);

            return rc;
        }
        //Identifies the size of each dimension in the array
        private string GetArraySize(System.Array arr)
        {
            string _Size = System.String.Empty;

            for (int _Index = 0; _Index < arr.Rank; _Index++)
            {
                _Size += "," + System.Convert.ToString(arr.GetUpperBound(_Index) - arr.GetLowerBound(_Index) + 1);
            }

            if (_Size.Length == 0)
            {
                _Size = ",";
            }

            return _Size.Substring(1);
        }
        private void WriteArray(System.Array arr, System.Xml.XmlWriter writer, int rank, int[] Indices)
        {
            int _Index;

            //C# Is 0 Based While VB Is 1 Based By Default
            int[] _Indexes = new int[arr.Rank];

            if (Indices != null)
            {
                System.Array.Copy(Indices, _Indexes, Indices.Length - 1);
            }

            for (_Index = arr.GetLowerBound(rank); _Index <= arr.GetUpperBound(rank); _Index++)
            {
                //C# Is 0 Based While VB Is 1 Based By Default
                _Indexes.SetValue(_Index, rank);

                if (arr.GetValue(_Indexes) != null)
                {
                    writer.WriteStartElement("System.Array.Item");
                    writer.WriteAttributeString("point", null, GetArrayPoint(_Indexes));
                    WriteXml(arr.GetValue(_Indexes), writer);
                    writer.WriteEndElement();
                }

                if (arr.Rank - 1 > rank)
                {
                    WriteArray(arr, writer, rank + 1, _Indexes);
                }
            }
        }
 /// <summary>
 /// Converting the data values retrieved from the rangecells to string values.
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 private string[][] ToStringArray(System.Array data)
 {
     try
     {
         string[][] sArray = new string[data.GetUpperBound(0)][];
         for (int i = data.GetLowerBound(0); i <= data.GetUpperBound(0); ++i)
         {
             sArray[i - 1] = new string[data.GetUpperBound(1)];
             for (int j = data.GetLowerBound(1); j <= data.GetUpperBound(1); ++j)
             {
                 if (data.GetValue(i, j) == null)
                 {
                     sArray[i - 1][j - 1] = "-----";
                 }
                 else
                 {
                     sArray[i - 1][j - 1] = data.GetValue(i, j).ToString();
                 }
             }
         }
         return sArray;
     }
     catch 
     {
         throw (new Exception("Exception: Conversion to string array"));
     }
 }
Esempio n. 6
0
        private string[] ConvertToStringArray(System.Array values)
        {
            string[] newArray = new string[values.Length];

            int index = 0;
            for (int i = values.GetLowerBound(0);
                  i <= values.GetUpperBound(0); i++)
            {
                for (int j = values.GetLowerBound(1);
                          j <= values.GetUpperBound(1); j++)
                {
                    if (values.GetValue(i, j) == null)
                    {
                        newArray[index] = "";
                    }
                    else
                    {
                        newArray[index] = (string)values.GetValue(i, j).ToString();
                    }
                    index++;
                }
            }
            return newArray;
        }
Esempio n. 7
0
 private bool LoadArray(ref System.Array AnArray, short CanonDT, ref string wrTxt)
 {
     int ii;
     int loc;
     int Wlen;
     int start;
     try
     {
         start = 1;
         Wlen = wrTxt.Length;
         for (ii = AnArray.GetLowerBound(0); (ii <= AnArray.GetUpperBound(0)); ii++)
         {
             loc = (wrTxt.IndexOf(",", (start - 1)) + 1);
             if ((ii < AnArray.GetUpperBound(0)))
             {
                 if ((loc == 0))
                 {
                     MessageBox.Show("Write Value: Incorrect Number of Items for Array Size?", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                     return false;
                 }
             }
             else
             {
                 loc = (Wlen + 1);
             }
             switch ((CanonicalDataTypes)CanonDT)
             {
                 case CanonicalDataTypes.CanonDtByte:
                     AnArray.SetValue(Convert.ToByte(wrTxt.Substring((start - 1), (loc - start))), ii);
                     //  End case
                     break;
                 case CanonicalDataTypes.CanonDtChar:
                     AnArray.SetValue(Convert.ToSByte(wrTxt.Substring((start - 1), (loc - start))), ii);
                     //  End case
                     break;
                 case CanonicalDataTypes.CanonDtWord:
                     AnArray.SetValue(Convert.ToUInt16(wrTxt.Substring((start - 1), (loc - start))), ii);
                     //  End case
                     break;
                 case CanonicalDataTypes.CanonDtShort:
                     AnArray.SetValue(Convert.ToInt16(wrTxt.Substring((start - 1), (loc - start))), ii);
                     //  End case
                     break;
                 case CanonicalDataTypes.CanonDtDWord:
                     AnArray.SetValue(Convert.ToUInt32(wrTxt.Substring((start - 1), (loc - start))), ii);
                     //  End case
                     break;
                 case CanonicalDataTypes.CanonDtLong:
                     AnArray.SetValue(Convert.ToInt32(wrTxt.Substring((start - 1), (loc - start))), ii);
                     //  End case
                     break;
                 case CanonicalDataTypes.CanonDtFloat:
                     AnArray.SetValue(Convert.ToSingle(wrTxt.Substring((start - 1), (loc - start))), ii);
                     //  End case
                     break;
                 case CanonicalDataTypes.CanonDtDouble:
                     AnArray.SetValue(Convert.ToDouble(wrTxt.Substring((start - 1), (loc - start))), ii);
                     //  End case
                     break;
                 case CanonicalDataTypes.CanonDtBool:
                     AnArray.SetValue(Convert.ToBoolean(wrTxt.Substring((start - 1), (loc - start))), ii);
                     //  End case
                     break;
                 case CanonicalDataTypes.CanonDtString:
                     AnArray.SetValue(Convert.ToString(wrTxt.Substring((start - 1), (loc - start))), ii);
                     //  End case
                     break;
                 default:
                     MessageBox.Show("Write Value Unknown data type", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                     return false;
                     break;
             }
             start = (loc + 1);
         }
         return true;
     }
     catch (Exception ex)
     {
         MessageBox.Show(("Write Value generated Exception: " + ex.Message), "SimpleOPCInterface Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
         return false;
     }
 }