void VP8LBitReaderSetBuffer(VP8LBitReader* const br, const uint8_t* const buf, size_t len) { assert(br != NULL); assert(buf != NULL); assert(len < 0xfffffff8u); // can't happen with a RIFF chunk. br->buf_ = buf; br->len_ = len; br->eos_ = IsEndOfStream(br); }
int CMemoryStream::Read(void *lpDest, DWORD dwBytes) { if (IsEndOfStream()) return 0; DWORD dwMaxBytes = static_cast<DWORD>(GetSize() - GetPos()); DWORD dwBytesToCopy = min(dwBytes, dwMaxBytes); memcpy(lpDest, &m_data[static_cast<int>(m_currentPosition)], dwBytesToCopy); m_currentPosition += dwBytesToCopy; return dwBytesToCopy; }
std::size_t InMemoryDataStream::Read(char* bytes, std::size_t numBytesToRead) { if (IsEndOfStream()) { return 0; } std::size_t bytesRemaining = BytesRemaining(); std::size_t bytesRead = (numBytesToRead > bytesRemaining) ? bytesRemaining : numBytesToRead; for (std::size_t readByteIndex = 0; readByteIndex < bytesRead; ++readByteIndex) { bytes[readByteIndex] = data[streamPosition]; ++streamPosition; } return bytesRead; }
// Returns TRUE if all data was written successfully, // FALSE if data was dropped. bool ByteQueue::WriteData( const char *data, size_t length ) { size_t bytesToCopy; bool noDrop = true, wasEmpty; int err; LOG_DEBUG( LOG_DEBUG_ARG, "%p->WriteData( %p, %d )", this, data, length ); assert( !IsEndOfStream() ); err = pthread_mutex_lock( &(mutex) ); assert( err == 0 ); wasEmpty = this->IsEmpty(); #if DEBUG fprintf( debugLogFile, "Before WriteData( %p, %p, %d ): head=%d, tail=%d, " "isEmpty=%d, isFull=%d.\n", this, data, (int) length, head, tail, wasEmpty, this->IsFull() ); #endif while( length > 0 ) { if( this->IsFull() ) { noDrop = false; break; // drop } // Copy source from TAIL to (HEAD - 1 or end of buffer) whichever comes first if( head > tail ) bytesToCopy = head - tail; else bytesToCopy = size - tail; if( bytesToCopy > length ) bytesToCopy = length; assert( bytesToCopy > 0 ); memcpy( &(buffer[ tail ]), data, bytesToCopy ); data += bytesToCopy; tail += bytesToCopy; assert( tail <= size ); if( tail == size ) tail = 0; length -= bytesToCopy; if( head == tail ) isFull = true; if( wasEmpty ) { assert( !IsEmpty() ); pthread_cond_broadcast( &condition ); wasEmpty = false; } } #if DEBUG fprintf( debugLogFile, "After WriteData( %p, %p, %d ): head=%d, tail=%d, " "isEmpty=%d, isFull=%d, noDrop=%d.\n", this, data, (int) length, head, tail, this->IsEmpty(), this->IsFull(), noDrop ); fflush( debugLogFile ); #endif err = pthread_mutex_unlock( &mutex ); assert( err == 0 ); return noDrop; }
// Behaviour: block on read size_t ByteQueue::ReadData( char *data, size_t readRequest ) { size_t bytesToCopy; int err; size_t result; size_t length; LOG_DEBUG( LOG_DEBUG_ARG, "%p->ReadData( %p, %d )", (void *) this, data, readRequest ); length = readRequest; err = pthread_mutex_lock( &mutex ); assert( err == 0 ); assert( length > 0 ); #if DEBUG fprintf( debugLogFile, "Before ReadData( %p, %p, %d ): head=%d, tail=%d, " "isEmpty=%d, isFull=%d.\n", this, data, (int) length, head, tail, IsEmpty(), IsFull() ); #endif result = 0; while( length > 0 ) { if( IsEmpty() ) { LOG_TRACE( LOG_TRACE_ARG, "%p->ReadData: IsEmpty, eos=%d, " "eosReported=%d", this, IsEndOfStream(), endOfStreamReported ); if( IsEndOfStream() && !endOfStreamReported ) break; else { LOG_TRACE( LOG_TRACE_ARG, "%p->ReadData: waiting for data", this ); pthread_cond_wait( &condition, &mutex ); LOG_TRACE( LOG_TRACE_ARG, "%p->ReadData: got condition change", this ); // the condition may have been signalled from the end of stream // function, in which case we should do the IsEmpty check again continue; } } if( tail <= head ) bytesToCopy = size - head; else bytesToCopy = tail - head; if( bytesToCopy > length ) bytesToCopy = length; assert( bytesToCopy > 0 ); memcpy( data, &(buffer[head]), bytesToCopy ); if( head == tail ) // then it was full, because it was not empty { assert( isFull ); isFull = false; } data += bytesToCopy; head += bytesToCopy; result += bytesToCopy; assert( head <= size ); if( head == size ) head = 0; length -= bytesToCopy; } #if DEBUG fprintf( debugLogFile, "After ReadData( %p, %p, %d ): head=%d, tail=%d, " "isEmpty=%d, isFull=%d.\n", this, data, (int) length, head, tail, IsEmpty(), IsFull() ); fflush( debugLogFile ); #endif if( result < readRequest ) { assert( IsEndOfStream() ); endOfStreamReported = true; // broadcast because eos is now reported pthread_cond_broadcast( &condition ); } pthread_mutex_unlock( &mutex ); return result; }