From 0194486115c1a52b5facdbc661de4f96b7eb7349 Mon Sep 17 00:00:00 2001 From: jules Date: Fri, 22 Jun 2012 20:20:06 +0100 Subject: [PATCH] Added a method MemoryBlock::replaceWith(). Changed FileInputStream to avoid cacheing the length of the file. --- modules/juce_core/files/juce_FileInputStream.cpp | 15 +++++++++------ modules/juce_core/files/juce_FileInputStream.h | 2 +- modules/juce_core/memory/juce_MemoryBlock.cpp | 10 ++++++++++ modules/juce_core/memory/juce_MemoryBlock.h | 5 +++++ modules/juce_core/native/juce_posix_SharedCode.h | 2 -- modules/juce_core/native/juce_win32_Files.cpp | 2 -- modules/juce_core/streams/juce_OutputStream.cpp | 2 +- 7 files changed, 26 insertions(+), 12 deletions(-) diff --git a/modules/juce_core/files/juce_FileInputStream.cpp b/modules/juce_core/files/juce_FileInputStream.cpp index c07fdd3639..2d8a64fb3e 100644 --- a/modules/juce_core/files/juce_FileInputStream.cpp +++ b/modules/juce_core/files/juce_FileInputStream.cpp @@ -30,7 +30,6 @@ FileInputStream::FileInputStream (const File& f) : file (f), fileHandle (nullptr), currentPosition (0), - totalSize (0), status (Result::ok()), needToSeek (true) { @@ -45,7 +44,7 @@ FileInputStream::~FileInputStream() //============================================================================== int64 FileInputStream::getTotalLength() { - return totalSize; + return file.getSize(); } int FileInputStream::read (void* buffer, int bytesToRead) @@ -69,7 +68,7 @@ int FileInputStream::read (void* buffer, int bytesToRead) bool FileInputStream::isExhausted() { - return currentPosition >= totalSize; + return currentPosition >= getTotalLength(); } int64 FileInputStream::getPosition() @@ -80,10 +79,14 @@ int64 FileInputStream::getPosition() bool FileInputStream::setPosition (int64 pos) { jassert (openedOk()); - pos = jlimit ((int64) 0, totalSize, pos); - needToSeek |= (currentPosition != pos); - currentPosition = pos; + if (pos != currentPosition) + { + pos = jlimit ((int64) 0, getTotalLength(), pos); + + needToSeek |= (currentPosition != pos); + currentPosition = pos; + } return true; } diff --git a/modules/juce_core/files/juce_FileInputStream.h b/modules/juce_core/files/juce_FileInputStream.h index 1e45ff14c6..8afbd8ad1a 100644 --- a/modules/juce_core/files/juce_FileInputStream.h +++ b/modules/juce_core/files/juce_FileInputStream.h @@ -82,7 +82,7 @@ private: //============================================================================== File file; void* fileHandle; - int64 currentPosition, totalSize; + int64 currentPosition; Result status; bool needToSeek; diff --git a/modules/juce_core/memory/juce_MemoryBlock.cpp b/modules/juce_core/memory/juce_MemoryBlock.cpp index 83bfd889fc..78c964122b 100644 --- a/modules/juce_core/memory/juce_MemoryBlock.cpp +++ b/modules/juce_core/memory/juce_MemoryBlock.cpp @@ -175,6 +175,16 @@ void MemoryBlock::append (const void* const srcData, const size_t numBytes) } } +void MemoryBlock::replaceWith (const void* const srcData, const size_t numBytes) +{ + if (numBytes > 0) + { + jassert (srcData != nullptr); // this must not be null! + setSize (numBytes); + memcpy (data, srcData, numBytes); + } +} + void MemoryBlock::insert (const void* const srcData, const size_t numBytes, size_t insertPosition) { if (numBytes > 0) diff --git a/modules/juce_core/memory/juce_MemoryBlock.h b/modules/juce_core/memory/juce_MemoryBlock.h index 5410c3d97b..0a8ec04a5a 100644 --- a/modules/juce_core/memory/juce_MemoryBlock.h +++ b/modules/juce_core/memory/juce_MemoryBlock.h @@ -150,6 +150,11 @@ public: */ void append (const void* data, size_t numBytes); + /** Resizes this block to the given size and fills its contents from the supplied buffer. + The data pointer must not be null. + */ + void replaceWith (const void* data, size_t numBytes); + /** Inserts some data into the block. The dataToInsert pointer must not be null. This block's size will be increased accordingly. If the insert position lies outside the valid range of the block, it will be clipped to diff --git a/modules/juce_core/native/juce_posix_SharedCode.h b/modules/juce_core/native/juce_posix_SharedCode.h index 9b2123e21c..fd683ae4e9 100644 --- a/modules/juce_core/native/juce_posix_SharedCode.h +++ b/modules/juce_core/native/juce_posix_SharedCode.h @@ -413,8 +413,6 @@ int64 juce_fileSetPosition (void* handle, int64 pos) void FileInputStream::openHandle() { - totalSize = file.getSize(); - const int f = open (file.getFullPathName().toUTF8(), O_RDONLY, 00644); if (f != -1) diff --git a/modules/juce_core/native/juce_win32_Files.cpp b/modules/juce_core/native/juce_win32_Files.cpp index 2052961e47..b14dfd8490 100644 --- a/modules/juce_core/native/juce_win32_Files.cpp +++ b/modules/juce_core/native/juce_win32_Files.cpp @@ -222,8 +222,6 @@ int64 juce_fileSetPosition (void* handle, int64 pos) void FileInputStream::openHandle() { - totalSize = file.getSize(); - HANDLE h = CreateFile (file.getFullPathName().toWideCharPointer(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, 0); diff --git a/modules/juce_core/streams/juce_OutputStream.cpp b/modules/juce_core/streams/juce_OutputStream.cpp index 45b41cb799..b1a3fb130d 100644 --- a/modules/juce_core/streams/juce_OutputStream.cpp +++ b/modules/juce_core/streams/juce_OutputStream.cpp @@ -241,7 +241,7 @@ int OutputStream::writeFromInputStream (InputStream& source, int64 numBytesToWri int numWritten = 0; - while (numBytesToWrite > 0 && ! source.isExhausted()) + while (numBytesToWrite > 0) { char buffer [8192]; const int num = source.read (buffer, (int) jmin (numBytesToWrite, (int64) sizeof (buffer)));