예제 #1
0
        public override void Flush()
        {
            if (_hasFlushed)
            {
                return;
            }

            _hasFlushed = true;
            byte[] inputBuffer       = _inputBuffer ?? Array.Empty <byte>();
            int    inputBufferOffset = 0;

            ConvertStatus result = default(ConvertStatus);

            do
            {
                result = _encoder.Convert(inputBuffer, inputBufferOffset, _inputBufferEnd,
                                          _outputBuffer, 0, _outputBuffer.Length,
                                          true,
                                          out var inputUsed,
                                          out var outputUsed);

                if (outputUsed > 0)
                {
                    _outputWriter.Write(_outputBuffer, 0, outputUsed);
                }
            } while (!result);
        }
예제 #2
0
        public BinaryDecoderStream(BinaryCodec codec, TextReader inputReader, int?bufferLength = null, bool closeReader = true)
        {
            if (codec == null)
            {
                throw new ArgumentNullException(nameof(codec));
            }

            if (inputReader == null)
            {
                throw new ArgumentNullException(nameof(inputReader));
            }

            int inputBufferLength = bufferLength ?? DefaultInputBufferLength;

            if (inputBufferLength <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(bufferLength));
            }

            inputBufferLength = Math.Max(inputBufferLength, codec.MinimumInputBuffer);

            // all of the codecs currently have a minimumOutputBuffer of 1, this code relies
            // on that knowledge.. so let's check that it's true..
            System.Diagnostics.Debug.Assert(codec.MinimumOutputBuffer == 1);

            _convertStatus = ConvertStatus.InputRequired;
            _decoder       = codec.GetDecoder();
            _inputReader   = inputReader;
            _inputBuffer   = new char[inputBufferLength];
            _closeReader   = closeReader;
        }
예제 #3
0
 protected void StatusChanged(ConvertStatus oldValue)
 {
     if (Status == ConvertStatus.InProgress)
     {
         DeleteEnabled = false;
     }
     else
     {
         DeleteEnabled = true;
     }
 }
예제 #4
0
        /// <summary>
        /// HEX转换为bin
        /// </summary>
        /// <param name="strHexFile"></hex所有字符串>
        /// <param name="binFile"></存放的bin数组>
        /// <param name="binSize"></存放的bin文件的大小>
        /// <returns></returns>
        public ConvertStatus HexFileToBinFile(string strHexFile, ref byte[] binFile, ref UInt32 binSize)
        {
            UInt32 addrStart = 0;
            UInt32 addrBase  = 0;


            for (int i = 0; i < binFile.Length; i++)
            {
                binFile[i] = 0xFF;
            }

            //将其分割为一行一行的数据。
            string[] strLineData = strHexFile.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < strLineData.Length; i++)
            {
                ConvertStatus res = CovertLineHexToBin(strLineData[i]);
                if (res != ConvertStatus.ConvertOk)
                {
                    return(res);
                }
                else
                {
                    switch (this.LineType)
                    {
                    //数据记录
                    case 0:
                        if (this.FirstAddr == 0xFFFF)
                        {
                            this.FirstAddr = this.LineStartAddr;
                        }
                        addrStart = (UInt32)(addrBase + this.LineStartAddr - this.FirstAddr);        //每行的起始地址 需要加上基地址
                        Console.WriteLine(addrStart);
                        for (int j = 0; j < this.LineLength; j++)
                        {
                            binFile[j + addrStart] = this.LineBinData[j];
                        }
                        binSize = (UInt32)(this.LineLength + addrStart);
                        Console.WriteLine("binSize" + binSize);
                        break;

                    //数据结束
                    case 1:
                        Console.WriteLine("binSizeOKOK" + binSize);
                        return(ConvertStatus.ConvertOk);

                    //扩展段地址记录
                    case 2:
                        if (this.LineLength != 2)                                       //扩展地址只有两个字节
                        {
                            return(ConvertStatus.DataTypeError);
                        }
                        if (i == 0)      //首次仅仅记录基地址
                        {
                            this.BaseAddr = ((UInt32)(((UInt32)this.LineBinData[0] << 8) + (UInt32)this.LineBinData[1]) << 4);
                        }
                        else
                        {
                            if (((((UInt32)this.LineBinData[0] << 8) + (UInt32)this.LineBinData[1]) << 4) < this.BaseAddr)
                            {
                                return(ConvertStatus.DataTypeError);
                            }
                            addrBase = (((((UInt32)this.LineBinData[0] << 8) + (UInt32)this.LineBinData[1]) << 4) - this.BaseAddr);
                        }
                        break;

                    //开始段地址记录
                    case 3:
                        break;

                    //扩展线性地址记录
                    case 4:
                        if (this.LineLength != 2)
                        {
                            return(ConvertStatus.DataTypeError);
                        }
                        if (i == 0)      //首次仅仅记录基地址
                        {
                            this.BaseAddr = ((UInt32)(((UInt32)this.LineBinData[0] << 8) + (UInt32)this.LineBinData[1]) << 16);
                        }
                        else
                        {
                            if (((UInt32)(((UInt32)this.LineBinData[0] << 8) + (UInt32)this.LineBinData[1]) << 16) < this.BaseAddr)
                            {
                                return(ConvertStatus.DataTypeError);
                            }
                            addrBase = (((((UInt32)this.LineBinData[0] << 8) + (UInt32)this.LineBinData[1]) << 16) - this.BaseAddr);
                        }
                        break;

                    //开始线性地址记录
                    case 5:
                        break;

                    default:
                        return(ConvertStatus.DataTypeError);
                    }
                }
            }

            return(ConvertStatus.HexNoEnd);
        }
예제 #5
0
        private void StartButtonClicked(object parameters)
        {
            string param = parameters.ToString();

            if (param == "START")
            {
                DataSourceAdjustCoord transferSource = new DataSourceAdjustCoord(DateToFetch, FileToCorrect);
                Datasource       = transferSource;
                ConvertingStatus = transferSource.ConvertingStatus;
                NotifyPropertyChanged(p => p.ConvertingStatus);
                (transferSource as DataSourceAdjustCoord).ConvertCompleted += (s1, a1) =>
                {
                    string str1 = App.Current.FindResource("msgAdjustComplete").ToString();
                    MessageBox.Show(str1, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                };

                BackgroundWorker bw = new BackgroundWorker();
                bw.DoWork += (s, a) =>
                {
                    transferSource.ConvertToMBTiles(FileToCorrect, "", "", "", new int[] { 18, 19 }, null, false);
                };
                bw.RunWorkerAsync();
            }
            else if (param == "MERGE")
            {
                IsMergeButtonEnable = false;
                BackgroundWorker bw = new BackgroundWorker();
                bw.RunWorkerCompleted += (s, a) =>
                {
                    MessageBox.Show("转换完成", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                };
                bw.DoWork += (s, a) =>
                {
                    Datasource       = new MockDataSource();
                    ConvertingStatus = new ConvertStatus();
                    ((MockDataSource)Datasource).ConvertingStatus = ConvertingStatus;
                    NotifyPropertyChanged(p => p.ConvertingStatus);

                    List <int>       levels = new List <int>();
                    int              recordCount = 0;
                    int              totalCount = 0;
                    string           boundA = "", boundB = "";
                    SQLiteConnection _outputconn = new SQLiteConnection("Data source = " + FileToCorrect);
                    _outputconn.Open();
                    SQLiteConnection _inputconn = new SQLiteConnection("Data source = " + DateToFetch);
                    _inputconn.Open();
                    ConvertingStatus.IsInProgress = true;
                    using (SQLiteTransaction writeTran = _outputconn.BeginTransaction())
                    {
                        SQLiteCommand readCommand  = new SQLiteCommand(_inputconn);
                        SQLiteCommand writeCommand = new SQLiteCommand(_outputconn);
                        readCommand.CommandText = "select value from metadata where name = 'bounds'";
                        SQLiteDataReader reader = readCommand.ExecuteReader();
                        if (reader.Read())
                        {
                            boundA = reader.GetString(0);
                        }
                        reader.Close();

                        writeCommand.CommandText = "select value from metadata where name = 'bounds'";
                        reader = writeCommand.ExecuteReader();
                        if (reader.Read())
                        {
                            boundB = reader.GetString(0);
                        }
                        reader.Close();



                        readCommand.CommandText = "select count(*) from tiles";
                        reader = readCommand.ExecuteReader();
                        if (reader.Read())
                        {
                            totalCount = reader.GetInt32(0);
                        }
                        ConvertingStatus.TotalCount = totalCount;
                        reader.Close();
                        readCommand.CommandText = "select distinct(zoom_level) from tiles order by zoom_level";
                        reader = readCommand.ExecuteReader();
                        while (reader.Read())
                        {
                            levels.Add(reader.GetInt32(0));
                        }
                        reader.Close();


                        foreach (int level in levels)
                        {
                            readCommand.CommandText = "select count(*) from tiles where zoom_level = " + level;
                            reader = readCommand.ExecuteReader();
                            reader.Read();
                            int levelCount     = reader.GetInt32(0);
                            int levelCompleted = 0;
                            ConvertingStatus.Level              = level;
                            ConvertingStatus.LevelTotalCount    = levelCount;
                            ConvertingStatus.LevelCompleteCount = ConvertingStatus.LevelErrorCount = 0;
                            reader.Close();
                            readCommand.CommandText = "select zoom_level, tile_column, tile_row, tile_data from tiles where zoom_level = " + level;
                            reader = readCommand.ExecuteReader();
                            while (reader.Read())
                            {
                                int    col  = reader.GetInt32(1);
                                int    row  = reader.GetInt32(2);
                                byte[] data = (byte[])reader.GetValue(3);
                                try
                                {
                                    WriteTilesToSqlite(level, col, row, data, writeCommand);
                                }
                                catch (Exception e)
                                {
                                }
                                finally
                                {
                                    levelCompleted++;
                                    recordCount++;
                                    ConvertingStatus.LevelCompleteCount = levelCompleted;
                                    ConvertingStatus.CompleteCount      = recordCount;
                                    TotalPercent = ConvertingStatus.CompletePercent;
                                }
                            }
                            reader.Close();
                        }
                        writeCommand.CommandText = "update metadata set value = @bound where name = 'bounds'";
                        writeCommand.Parameters.AddWithValue("bound", mergeBounds(boundA, boundB));
                        writeCommand.ExecuteNonQuery();

                        writeTran.Commit();
                        writeTran.Dispose();
                    }
                    ConvertingStatus.IsInProgress = false;
                    _inputconn.Close();
                    _inputconn.Dispose();
                    _outputconn.Close();
                    _outputconn.Dispose();
                };
                bw.RunWorkerAsync();
            }
        }
예제 #6
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            if ((offset < 0) || (count < 0))
            {
                throw new ArgumentOutOfRangeException((offset < 0) ? nameof(offset) : nameof(count));
            }

            if (buffer.Length - offset < count)
            {
                throw new ArgumentException(Resources.InvalidOffsetCountLength);
            }

            if (_inputReader == null)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            // nothing to read after ConverStatus becomes Complete..
            if (_convertStatus == ConvertStatus.Complete)
            {
                return(0);
            }

            int totalRead = 0;

            while (count > 0)
            {
                if (_convertStatus == ConvertStatus.InputRequired)
                {
                    FillInputBuffer();
                }

                _convertStatus = _decoder.Convert(_inputBuffer, _inputBufferOffset, _inputBufferEnd - _inputBufferOffset,
                                                  buffer, offset, count,
                                                  _isEos,
                                                  out var inputUsed,
                                                  out var outputUsed);

                _inputBufferOffset += inputUsed;
                offset             += outputUsed;
                count     -= outputUsed;
                totalRead += outputUsed;

                // if we've run out of output buffer or finished converting, then stop looping..
                if ((_convertStatus == ConvertStatus.OutputRequired) || (_convertStatus == ConvertStatus.Complete))
                {
                    break;
                }

                // if we've already had a partial read from the input, then we'll stop and wait
                // for the next call to Read to access the underlying input reader again (unless we
                // haven't produced any output yet, in which case we need to continue reading)

                if ((_isBlocked) && (totalRead > 0))
                {
                    break;
                }
            }

            _isBlocked    = false;
            _outputTotal += totalRead;
            return(totalRead);
        }