sp<Buffer> Buffer::position(size_t newPos) { if (newPos > mLimit) { throw BufferOverflowException("Cannot position outside of the buffer"); } mPosition = newPos; return this; }
sp<Buffer> Buffer::limit(size_t newLimit) { if (newLimit > mCapacity) { throw BufferOverflowException("Given limit exceeds capacity"); } mLimit = newLimit; return this; }
ptrdiff_t BufferedStream::find(const std::string &str, size_t sanitySize, bool throwIfNotFound) { if (supportsSeek()) flush(false); if (sanitySize == (size_t)~0) sanitySize = 2 * m_bufferSize; sanitySize += str.size(); while (true) { size_t readAvailable = m_readBuffer.readAvailable(); if (readAvailable > 0) { ptrdiff_t result = m_readBuffer.find(str, std::min(sanitySize, readAvailable)); if (result != -1) { return result; } } if (readAvailable >= sanitySize) { if (throwIfNotFound) MORDOR_THROW_EXCEPTION(BufferOverflowException()); return -(ptrdiff_t)m_readBuffer.readAvailable() - 1; } MORDOR_LOG_TRACE(g_log) << this << " parent()->read(" << m_bufferSize << ")"; size_t result = parent()->read(m_readBuffer, m_bufferSize); MORDOR_LOG_DEBUG(g_log) << this << " parent()->read(" << m_bufferSize << "): " << result; if (result == 0) { // EOF if (throwIfNotFound) MORDOR_THROW_EXCEPTION(UnexpectedEofException()); return -(ptrdiff_t)m_readBuffer.readAvailable() - 1; } } }
Buffer File::readLine(bool keepEol, size_t maxLen) { if (!m_fd) { throw IOException("readLine", EINVAL); } uint32_t bytesRead = 0; Buffer buf; for (;;) { const size_t lineChunk = 2048; char * tmpBuf = buf.lockBuffer(bytesRead + lineChunk); tmpBuf += bytesRead; char *r = fgets(tmpBuf, lineChunk, m_fd); if (r == NULL && isEof() == false) { buf.unlockBuffer(bytesRead); throw IOException("fgets", errno); } if (isEof()) { break; } size_t len = strlen(r); bytesRead += len; if (tmpBuf[len-1] != '\n') { buf.unlockBuffer(bytesRead); if (maxLen && len > maxLen) throw BufferOverflowException(); } else { if (!keepEol) { tmpBuf[len-1] = '\0'; bytesRead--; } break; } } buf.unlockBuffer(bytesRead); return buf; }
void ByteBuffer::checkBufferOverflow(size_t index, size_t amount) { if (mOffset + index + amount > mLimit) { throw BufferOverflowException(); } }