Exemple #1
0
        //////          //---------------------------------------------------------------------------
        //////          // читает блок из потока каталога stream_from, собирая его по страницам
        //////          TStream* read_block(TStream* stream_from, int start, TStream* stream_to = nullptr)
        //////          {
        //////              stBlockHeader block_header;
        //////
        //////              if (!stream_to)
        //////              {
        //////                  stream_to = new TMemoryStream;
        //////              }
        //////
        //////              stream_to->Seek(0, soFromBeginning);
        //////              stream_to->SetSize(0);
        //////
        //////              if (start < 0
        //////                      || start == LAST_BLOCK
        //////                      || start > stream_from->GetSize())
        //////              {
        //////                  return stream_to;
        //////              }
        //////
        //////              stream_from->Seek(start, soFromBeginning);
        //////              stream_from->Read(&block_header, stBlockHeader::size());
        //////
        //////              int32_t len = block_header.get_data_size();
        //////
        //////              if (len == 0)
        //////              {
        //////                  return stream_to;
        //////              }
        //////
        //////              int32_t curlen = block_header.get_page_size();
        //////              start = block_header.get_next_page_addr();
        //////
        //////              int32_t readlen = std::min(len, curlen);
        //////              stream_to->CopyFrom(stream_from, readlen);
        //////
        //////              int32_t pos = readlen;
        //////
        //////              while (start != LAST_BLOCK)
        //////              {
        //////                  stream_from->Seek(start, soFromBeginning);
        //////                  stream_from->Read(&block_header, block_header.size());
        //////
        //////                  curlen = block_header.get_page_size();
        //////
        //////                  start = block_header.get_next_page_addr();
        //////
        //////                  readlen = std::min(len - pos, curlen);
        //////                  stream_to->CopyFrom(stream_from, readlen);
        //////                  pos += readlen;
        //////              }
        //////
        //////              return stream_to;
        //////
        //////          }
        #endregion

        // читает блок из потока каталога stream_from, собирая его по страницам
        public static Stream Read_Block(Stream stream_from, int start, ref int rlen, Stream stream_to = null)
        {
            //char temp_buf[32]; - оригинал
            byte[] temp_buf = new byte[32];

            stBlockHeader block_header = new stBlockHeader();

            int len, curlen, pos, readlen;

            if (stream_to == null)
            {
                stream_to = new MemoryStream();
            }

            stream_to.Seek(0, SeekOrigin.Begin);
            stream_to.SetLength(0);

            if (start < 0 || start == 0x7fffffff || start > stream_from.Length)
            {
                return(stream_to);
            }


            // спозиционироваться надо на start

            stream_from.Seek(start, SeekOrigin.Begin);
            block_header = ReadBlockHeaderFromData(stream_from);

            len = block_header.get_data_size();

            if (len == 0)
            {
                return(stream_to);
            }

            curlen = block_header.get_page_size();

            start = block_header.get_next_page_addr();

            readlen = Math.Min(len, curlen);
            byte[] tmp_buf = new byte[readlen];

            //((MemoryStream)stream_to).Capacity = readlen;
            rlen = readlen;
            stream_from.CopyTo(stream_to, readlen);

            pos = readlen;
            while (start != 0x7fffffff)
            {
                stream_from.Seek(start, SeekOrigin.Begin);

                block_header = ReadBlockHeaderFromData(stream_from);
                curlen       = block_header.get_page_size();
                start        = block_header.get_next_page_addr();

                readlen = Math.Min(len - pos, curlen);
                if (readlen != 0)
                {
                    stream_from.CopyTo(stream_to, readlen);
                }

                pos += readlen;
            }
            return(stream_to);
        }