size_t CachingFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags) { Prepare(); if (absolutePos >= filesize_) { bytes = 0; } else if (absolutePos + (s64)bytes >= filesize_) { bytes = (size_t)(filesize_ - absolutePos); } size_t readSize = 0; if ((flags & Flags::HINT_UNCACHED) != 0) { readSize = backend_->ReadAt(absolutePos, bytes, data, flags); } else { readSize = ReadFromCache(absolutePos, bytes, data); // While in case the cache size is too small for the entire read. while (readSize < bytes) { SaveIntoCache(absolutePos + readSize, bytes - readSize, flags); size_t bytesFromCache = ReadFromCache(absolutePos + readSize, bytes - readSize, (u8 *)data + readSize); readSize += bytesFromCache; if (bytesFromCache == 0) { // We can't read any more. break; } } StartReadAhead(absolutePos + readSize); } return readSize; }
size_t CachingFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data) { size_t readSize = ReadFromCache(absolutePos, bytes, data); // While in case the cache size is too small for the entire read. while (readSize < bytes) { SaveIntoCache(absolutePos + readSize, bytes - readSize); readSize += ReadFromCache(absolutePos + readSize, bytes - readSize, (u8 *)data + readSize); } StartReadAhead(absolutePos + readSize); filepos_ = absolutePos + readSize; return readSize; }
ChEXPORT void SsrwOOCacheStream::read (void* out_buffer, unsigned long* io_pCount) { ChLOG_DEBUG_START_FN; EnsureCache(std::min(*io_pCount, (unsigned long)STREAM_CACHE_SIZE)); // first read what we have in the cache (which may or may not be enough) unsigned long ulCacheCount = std::min(*io_pCount, m_bytesCached); unsigned long rc = ReadFromCache(out_buffer, &ulCacheCount); // bail out if the cache couldn't do what it said (shouldn't happen) SSRWOO_INTERNAL_THROW_ON_ERROR(rc); // reduce the count for the raw_read to take account of the bytes read from cache *io_pCount -= ulCacheCount; out_buffer = ((char*) out_buffer + ulCacheCount); // only try the raw read if we need more bytes if (*io_pCount>0) { if (m_bytesCached > 0) ChTHROW(SSTG_ERROR_ILLEGAL_CALL); CALL_SSRDR3(streamRead, m_pStream, out_buffer, io_pCount); } // put the number of bytes we read from the cache back on, so the out parameter is correct on return *io_pCount += ulCacheCount; SSRWOO_INTERNAL_THROW_ON_ERROR(rc); }
////////////////////////////////////////////////////////////////////////////// // nsICachelistener ////////////////////////////////////////////////////////////////////////////// NS_IMETHODIMP nsWyciwygChannel::OnCacheEntryAvailable(nsICacheEntryDescriptor * aCacheEntry, nsCacheAccessMode aMode, nsresult aStatus) { LOG(("nsWyciwygChannel::OnCacheEntryAvailable [this=%x entry=%x " "access=%x status=%x]\n", this, aCacheEntry, aMode, aStatus)); // if the channel's already fired onStopRequest, // then we should ignore this event. if (!mIsPending) return NS_OK; // otherwise, we have to handle this event. if (NS_SUCCEEDED(aStatus)) mCacheEntry = aCacheEntry; else if (NS_SUCCEEDED(mStatus)) mStatus = aStatus; nsresult rv; if (NS_FAILED(mStatus)) { LOG(("channel was canceled [this=%x status=%x]\n", this, mStatus)); rv = mStatus; } else { // advance to the next state... rv = ReadFromCache(); } // a failure from Connect means that we have to abort the channel. if (NS_FAILED(rv)) { CloseCacheEntry(rv); NotifyListener(); } return NS_OK; }
bool MCStreamCache::Read(void *p_buffer, uint32_t p_offset, uint32_t p_length, uint32_t &r_read) { bool t_success = true; uint32_t t_to_read; t_to_read = 0; uint32_t t_read; t_read = 0; if (p_offset < m_cache_length) { t_to_read = MCMin(m_cache_length - p_offset, p_length); t_success = ReadFromCache(p_buffer, p_offset, t_to_read, t_read); } r_read = t_read; if (t_success && t_read != t_to_read) return true; if (t_success) { t_to_read = p_length - t_read; t_read = 0; if (t_to_read > 0) t_success = ReadFromStream((uint8_t*)p_buffer + r_read, t_to_read, t_read); r_read += t_read; } return t_success; }
size_t RamCachingFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data) { size_t readSize = 0; if (cache_ == nullptr) { lock_guard guard(backendMutex_); readSize = backend_->ReadAt(absolutePos, bytes, data); } else { readSize = ReadFromCache(absolutePos, bytes, data); // While in case the cache size is too small for the entire read. while (readSize < bytes) { SaveIntoCache(absolutePos + readSize, bytes - readSize); readSize += ReadFromCache(absolutePos + readSize, bytes - readSize, (u8 *)data + readSize); } } StartReadAhead(absolutePos + readSize); filepos_ = absolutePos + readSize; return readSize; }
size_t RamCachingFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags) { size_t readSize = 0; if (cache_ == nullptr || (flags & Flags::HINT_UNCACHED) != 0) { lock_guard guard(backendMutex_); readSize = backend_->ReadAt(absolutePos, bytes, data, flags); } else { readSize = ReadFromCache(absolutePos, bytes, data); // While in case the cache size is too small for the entire read. while (readSize < bytes) { SaveIntoCache(absolutePos + readSize, bytes - readSize, flags); size_t bytesFromCache = ReadFromCache(absolutePos + readSize, bytes - readSize, (u8 *)data + readSize); readSize += bytesFromCache; if (bytesFromCache == 0) { // We can't read any more. break; } } StartReadAhead(absolutePos + readSize); } filepos_ = absolutePos + readSize; return readSize; }
////////////////////////////////////////////////////////////////////////////// // nsICachelistener ////////////////////////////////////////////////////////////////////////////// NS_IMETHODIMP nsWyciwygChannel::OnCacheEntryAvailable(nsICacheEntryDescriptor * aCacheEntry, nsCacheAccessMode aMode, nsresult aStatus) { LOG(("nsWyciwygChannel::OnCacheEntryAvailable [this=%x entry=%x " "access=%x status=%x]\n", this, aCacheEntry, aMode, aStatus)); // if the channel's already fired onStopRequest, // then we should ignore this event. if (!mIsPending) return NS_OK; // otherwise, we have to handle this event. if (NS_SUCCEEDED(aStatus)) mCacheEntry = aCacheEntry; else if (NS_SUCCEEDED(mStatus)) mStatus = aStatus; nsresult rv; if (NS_FAILED(mStatus)) { LOG(("channel was canceled [this=%x status=%x]\n", this, mStatus)); rv = mStatus; } else { // advance to the next state... rv = ReadFromCache(); } // a failure from Connect means that we have to abort the channel. if (NS_FAILED(rv)) { CloseCacheEntry(rv); if (mListener) { mListener->OnStartRequest(this, mListenerContext); mListener->OnStopRequest(this, mListenerContext, mStatus); mListener = 0; mListenerContext = 0; } mIsPending = PR_FALSE; // Remove ourselves from the load group. if (mLoadGroup) mLoadGroup->RemoveRequest(this, nsnull, mStatus); } return NS_OK; }
NS_IMETHODIMP nsWyciwygChannel::OnCacheEntryAvailable(nsICacheEntry *aCacheEntry, bool aNew, nsIApplicationCache* aAppCache, nsresult aStatus) { LOG(("nsWyciwygChannel::OnCacheEntryAvailable [this=%p entry=%p " "new=%d status=%x]\n", this, aCacheEntry, aNew, aStatus)); // if the channel's already fired onStopRequest, // then we should ignore this event. if (!mIsPending && !aNew) return NS_OK; // otherwise, we have to handle this event. if (NS_SUCCEEDED(aStatus)) mCacheEntry = aCacheEntry; else if (NS_SUCCEEDED(mStatus)) mStatus = aStatus; nsresult rv = NS_OK; if (NS_FAILED(mStatus)) { LOG(("channel was canceled [this=%p status=%x]\n", this, mStatus)); rv = mStatus; } else if (!aNew) { // advance to the next state... rv = ReadFromCache(); } // a failure from Connect means that we have to abort the channel. if (NS_FAILED(rv)) { CloseCacheEntry(rv); if (!aNew) { // Since OnCacheEntryAvailable can be called directly from AsyncOpen // we must dispatch. NS_DispatchToCurrentThread(NS_NewRunnableMethod( this, &nsWyciwygChannel::NotifyListener)); } } return NS_OK; }
NS_IMETHODIMP nsWyciwygChannel::AsyncOpen(nsIStreamListener *listener, nsISupports *ctx) { LOG(("nsWyciwygChannel::AsyncOpen [this=%x]\n", this)); NS_ENSURE_TRUE(!mIsPending, NS_ERROR_IN_PROGRESS); NS_ENSURE_ARG_POINTER(listener); nsCAutoString spec; mURI->GetSpec(spec); // open a cache entry for this channel... PRBool delayed = PR_FALSE; nsresult rv = OpenCacheEntry(spec, nsICache::ACCESS_READ, &delayed); if (NS_FAILED(rv)) { LOG(("nsWyciwygChannel::OpenCacheEntry failed [rv=%x]\n", rv)); return rv; } if (!delayed) { rv = ReadFromCache(); if (NS_FAILED(rv)) { LOG(("nsWyciwygChannel::ReadFromCache failed [rv=%x]\n", rv)); return rv; } } mIsPending = PR_TRUE; mListener = listener; mListenerContext = ctx; if (mLoadGroup) mLoadGroup->AddRequest(this, nsnull); return NS_OK; }
wxString SjInterfaceBase::ReadFromCache(const wxString& file, unsigned long fileTimestamp) { wxArrayString a; ReadFromCache(file, a, fileTimestamp); return a.GetCount()? a.Item(0) : wxString(); }