/** * Remove bytes from the stack */ void IOStack::Pop(unsigned int bytes_to_remove) { unsigned int bytes_removed = 0; BlockVector::iterator iter = m_blocks.begin(); while (iter != m_blocks.end() && bytes_removed != bytes_to_remove) { MemoryBlock *block = *iter; bytes_removed += block->PopFront(bytes_to_remove - bytes_removed); if (block->Empty()) { m_pool->Release(block); iter = m_blocks.erase(iter); } else { iter++; } } }
/** * Remove the first n bytes from the buffer */ void IOQueue::Pop(unsigned int n) { unsigned int bytes_popped = 0; BlockVector::iterator iter = m_blocks.begin(); while (iter != m_blocks.end() && bytes_popped != n) { MemoryBlock *block = *iter; bytes_popped += block->PopFront(n - bytes_popped); if (block->Empty()) { m_pool->Release(block); iter = m_blocks.erase(iter); } else { iter++; } } }
/** * Read up to n bytes into the string output. */ unsigned int IOStack::Read(string *output, unsigned int length) { unsigned int bytes_remaining = length; BlockVector::iterator iter = m_blocks.begin(); while (iter != m_blocks.end() && bytes_remaining) { MemoryBlock *block = *iter; unsigned int bytes_to_copy = std::min(block->Size(), bytes_remaining); output->append(reinterpret_cast<char*>(block->Data()), bytes_to_copy); bytes_remaining -= bytes_to_copy; if (block->Empty()) { m_pool->Release(block); iter = m_blocks.erase(iter); } else { iter++; } } return length - bytes_remaining; }
/** * Read up to n bytes into the memory location data and shrink the IOQueue by * the amount read. */ unsigned int IOQueue::Read(uint8_t *data, unsigned int n) { unsigned int bytes_read = 0; BlockVector::iterator iter = m_blocks.begin(); while (iter != m_blocks.end() && bytes_read != n) { MemoryBlock *block = *iter; unsigned int bytes_copied = block->Copy(data + bytes_read, n - bytes_read); block->PopFront(bytes_copied); bytes_read += bytes_copied; if (block->Empty()) { m_pool->Release(block); iter = m_blocks.erase(iter); } else { iter++; } } return bytes_read; }