当前位置: 首页>>代码示例>>C++>>正文


C++ CFile::GetChunkSize方法代码示例

本文整理汇总了C++中xfile::CFile::GetChunkSize方法的典型用法代码示例。如果您正苦于以下问题:C++ CFile::GetChunkSize方法的具体用法?C++ CFile::GetChunkSize怎么用?C++ CFile::GetChunkSize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在xfile::CFile的用法示例。


在下文中一共展示了CFile::GetChunkSize方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: LoadFile

unsigned int CFileUtils::LoadFile(const std::string &filename, void* &outputBuffer)
{
    static const unsigned int max_file_size = 0x7FFFFFFF;
    static const unsigned int min_chunk_size = 64*1024U;
    static const unsigned int max_chunk_size = 2048*1024U;

    outputBuffer = NULL;
    if (filename.empty())
        return 0;

    XFILE::CFile file;
    if (!file.Open(filename, READ_TRUNCATED))
        return 0;

    /*
     GetLength() will typically return values that fall into three cases:
     1. The real filesize. This is the typical case.
     2. Zero. This is the case for some http:// streams for example.
     3. Some value smaller than the real filesize. This is the case for an expanding file.

     In order to handle all three cases, we read the file in chunks, relying on Read()
     returning 0 at EOF.  To minimize (re)allocation of the buffer, the chunksize in
     cases 1 and 3 is set to one byte larger than the value returned by GetLength().
     The chunksize in case 2 is set to the lowest value larger than min_chunk_size aligned
     to GetChunkSize().

     We fill the buffer entirely before reallocation.  Thus, reallocation never occurs in case 1
     as the buffer is larger than the file, so we hit EOF before we hit the end of buffer.

     To minimize reallocation, we double the chunksize each read while chunksize is lower
     than max_chunk_size.
     */
    int64_t filesize = file.GetLength();
    if (filesize > max_file_size)
    {   /* file is too large for this function */
        file.Close();
        return 0;
    }
    unsigned int chunksize = (filesize > 0) ? (unsigned int)(filesize + 1) : CFile::GetChunkSize(file.GetChunkSize(), min_chunk_size);
    unsigned char *inputBuff = NULL;
    unsigned int inputBuffSize = 0;

    unsigned int total_read = 0, free_space = 0;
    while (true)
    {
        if (!free_space)
        {   // (re)alloc
            inputBuffSize += chunksize;
            unsigned char *tempinputBuff = NULL;
            if (inputBuffSize <= max_file_size)
                tempinputBuff = (unsigned char *)realloc(inputBuff, inputBuffSize);
            if (!tempinputBuff)
            {
                CLog::Log(LOGERROR, "%s unable to (re)allocate buffer of size %u for file \"%s\"", __FUNCTION__, inputBuffSize, filename.c_str());
                free(inputBuff);
                file.Close();
                return 0;
            }
            inputBuff = tempinputBuff;
            free_space = chunksize;
            if (chunksize < max_chunk_size)
                chunksize *= 2;
        }
        unsigned int read = file.Read(inputBuff + total_read, free_space);
        free_space -= read;
        total_read += read;
        if (!read)
            break;
    }

    file.Close();

    if (total_read == 0)
    {
        free(inputBuff);
        return 0;
    }

    if (total_read + 1 < inputBuffSize)
    {
        /* free extra memory if more than 1 byte (cases 1 and 3) */
        unsigned char *tempinputBuff = (unsigned char *)realloc(inputBuff, total_read);
        if (!tempinputBuff)
        {
            /* just a precaution, shouldn't really happen */
            CLog::Log(LOGERROR, "%s unable to reallocate buffer for file \"%s\"", __FUNCTION__, filename.c_str());
            free(inputBuff);
            return 0;
        }
        inputBuff = tempinputBuff;
    }

    outputBuffer = (void *) inputBuff;
    return total_read;
}
开发者ID:blacksocket,项目名称:xbmc,代码行数:95,代码来源:FileUtils.cpp

示例2: Open

bool CJpegIO::Open(const CStdString &texturePath, unsigned int minx, unsigned int miny, bool read)
{
  Close();

  m_texturePath = texturePath;

  XFILE::CFile file;
  if (file.Open(m_texturePath.c_str(), READ_TRUNCATED))
  {
    /*
     GetLength() will typically return values that fall into three cases:
       1. The real filesize. This is the typical case.
       2. Zero. This is the case for some http:// streams for example.
       3. Some value smaller than the real filesize. This is the case for an expanding file.

     In order to handle all three cases, we read the file in chunks, relying on Read()
     returning 0 at EOF.  To minimize (re)allocation of the buffer, the chunksize in
     cases 1 and 3 is set to one byte larger** than the value returned by GetLength().
     The chunksize in case 2 is set to the larger of 64k and GetChunkSize().

     We fill the buffer entirely before reallocation.  Thus, reallocation never occurs in case 1
     as the buffer is larger than the file, so we hit EOF before we hit the end of buffer.

     To minimize reallocation, we double the chunksize each time up to a maxchunksize of 2MB.
     */
    unsigned int filesize = (unsigned int)file.GetLength();
    unsigned int chunksize = filesize ? (filesize + 1) : std::max(65536U, (unsigned int)file.GetChunkSize());
    unsigned int maxchunksize = 2048*1024U; /* max 2MB chunksize */

    unsigned int total_read = 0, free_space = 0;
    while (true)
    {
      if (!free_space)
      { // (re)alloc
        m_inputBuffSize += chunksize;
        m_inputBuff = (unsigned char *)realloc(m_inputBuff, m_inputBuffSize);
        if (!m_inputBuff)
        {
          CLog::Log(LOGERROR, "%s unable to allocate buffer of size %u", __FUNCTION__, m_inputBuffSize);
          return false;
        }
        free_space = chunksize;
        chunksize = std::min(chunksize*2, maxchunksize);
      }
      unsigned int read = file.Read(m_inputBuff + total_read, free_space);
      free_space -= read;
      total_read += read;
      if (!read)
        break;
    }
    m_inputBuffSize = total_read;
    file.Close();

    if (m_inputBuffSize == 0)
      return false;
  }
  else
    return false;

  if (!read)
    return true;

  if (Read(m_inputBuff, m_inputBuffSize, minx, miny))
    return true;
  return false;
}
开发者ID:MrBishop,项目名称:xbmc,代码行数:66,代码来源:JpegIO.cpp


注:本文中的xfile::CFile::GetChunkSize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。