From e0881f2139195cfcb90251b5595f22e2faa5e5e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Sat, 13 Sep 2014 23:01:01 +0200 Subject: [PATCH] HaikuDepot: Fixed reading bigger screenshots * Increase the RAM limit to 128K per screenshot * Reduce retrieved size to 320 pixel wide * Don't expect to be able to read the stream in one call, read it in 4K chunks. * Print some errors in this code-path to stderr. --- src/apps/haikudepot/Model.cpp | 6 +++++- src/apps/haikudepot/PackageInfo.cpp | 32 +++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/apps/haikudepot/Model.cpp b/src/apps/haikudepot/Model.cpp index 8307f6c8cd..ae81f13af5 100644 --- a/src/apps/haikudepot/Model.cpp +++ b/src/apps/haikudepot/Model.cpp @@ -598,7 +598,7 @@ Model::PopulatePackage(const PackageInfoRef& package, uint32 flags) } for (int i = 0; i < screenshotInfos.CountItems(); i++) { const ScreenshotInfo& info = screenshotInfos.ItemAtFast(i); - _PopulatePackageScreenshot(package, info, 400, false); + _PopulatePackageScreenshot(package, info, 320, false); } } } @@ -1070,6 +1070,10 @@ Model::_PopulatePackageScreenshot(const PackageInfoRef& package, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE) == B_OK) { screenshotFile.Write(buffer.Buffer(), buffer.BufferLength()); } + } else { + fprintf(stderr, "Failed to retrieve screenshot for code '%s' " + "at %" B_PRIi32 "x%" B_PRIi32 ".\n", info.Code().String(), + scaledWidth, scaledHeight); } } diff --git a/src/apps/haikudepot/PackageInfo.cpp b/src/apps/haikudepot/PackageInfo.cpp index b63447d999..aaf67eaf47 100644 --- a/src/apps/haikudepot/PackageInfo.cpp +++ b/src/apps/haikudepot/PackageInfo.cpp @@ -74,16 +74,32 @@ SharedBitmap::SharedBitmap(BPositionIO& data) fMimeType() { status_t status = data.GetSize(&fSize); - if (status == B_OK && fSize > 0 && fSize <= 64 * 1024) { + const off_t kMaxSize = 128 * 1024; + if (status == B_OK && fSize > 0 && fSize <= kMaxSize) { fBuffer = new(std::nothrow) uint8[fSize]; - - data.Seek(0, SEEK_SET); - ssize_t read = data.Read(fBuffer, fSize); - if (read != fSize) { - delete[] fBuffer; - fBuffer = NULL; + if (fBuffer != NULL) { + data.Seek(0, SEEK_SET); + + size_t bytesRead = 0; + size_t chunkSize = std::min((off_t)4096, fSize); + while (bytesRead < fSize) { + ssize_t read = data.Read(fBuffer + bytesRead, chunkSize); + if (read > 0) + bytesRead += read; + else + break; + } + + if (bytesRead != fSize) { + delete[] fBuffer; + fBuffer = NULL; + fSize = 0; + } + } else fSize = 0; - } + } else { + fprintf(stderr, "SharedBitmap(): Stream too large: %" B_PRIi64 + ", max: %" B_PRIi64 "\n", fSize, kMaxSize); } fBitmap[0] = NULL;