Browse Source

Added a method MemoryBlock::replaceWith(). Changed FileInputStream to avoid cacheing the length of the file.

tags/2021-05-28
jules 13 years ago
parent
commit
0194486115
7 changed files with 26 additions and 12 deletions
  1. +9
    -6
      modules/juce_core/files/juce_FileInputStream.cpp
  2. +1
    -1
      modules/juce_core/files/juce_FileInputStream.h
  3. +10
    -0
      modules/juce_core/memory/juce_MemoryBlock.cpp
  4. +5
    -0
      modules/juce_core/memory/juce_MemoryBlock.h
  5. +0
    -2
      modules/juce_core/native/juce_posix_SharedCode.h
  6. +0
    -2
      modules/juce_core/native/juce_win32_Files.cpp
  7. +1
    -1
      modules/juce_core/streams/juce_OutputStream.cpp

+ 9
- 6
modules/juce_core/files/juce_FileInputStream.cpp View File

@@ -30,7 +30,6 @@ FileInputStream::FileInputStream (const File& f)
: file (f), : file (f),
fileHandle (nullptr), fileHandle (nullptr),
currentPosition (0), currentPosition (0),
totalSize (0),
status (Result::ok()), status (Result::ok()),
needToSeek (true) needToSeek (true)
{ {
@@ -45,7 +44,7 @@ FileInputStream::~FileInputStream()
//============================================================================== //==============================================================================
int64 FileInputStream::getTotalLength() int64 FileInputStream::getTotalLength()
{ {
return totalSize;
return file.getSize();
} }
int FileInputStream::read (void* buffer, int bytesToRead) int FileInputStream::read (void* buffer, int bytesToRead)
@@ -69,7 +68,7 @@ int FileInputStream::read (void* buffer, int bytesToRead)
bool FileInputStream::isExhausted() bool FileInputStream::isExhausted()
{ {
return currentPosition >= totalSize;
return currentPosition >= getTotalLength();
} }
int64 FileInputStream::getPosition() int64 FileInputStream::getPosition()
@@ -80,10 +79,14 @@ int64 FileInputStream::getPosition()
bool FileInputStream::setPosition (int64 pos) bool FileInputStream::setPosition (int64 pos)
{ {
jassert (openedOk()); 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; return true;
} }

+ 1
- 1
modules/juce_core/files/juce_FileInputStream.h View File

@@ -82,7 +82,7 @@ private:
//============================================================================== //==============================================================================
File file; File file;
void* fileHandle; void* fileHandle;
int64 currentPosition, totalSize;
int64 currentPosition;
Result status; Result status;
bool needToSeek; bool needToSeek;


+ 10
- 0
modules/juce_core/memory/juce_MemoryBlock.cpp View File

@@ -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) void MemoryBlock::insert (const void* const srcData, const size_t numBytes, size_t insertPosition)
{ {
if (numBytes > 0) if (numBytes > 0)


+ 5
- 0
modules/juce_core/memory/juce_MemoryBlock.h View File

@@ -150,6 +150,11 @@ public:
*/ */
void append (const void* data, size_t numBytes); 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. /** Inserts some data into the block.
The dataToInsert pointer must not be null. This block's size will be increased accordingly. 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 If the insert position lies outside the valid range of the block, it will be clipped to


+ 0
- 2
modules/juce_core/native/juce_posix_SharedCode.h View File

@@ -413,8 +413,6 @@ int64 juce_fileSetPosition (void* handle, int64 pos)
void FileInputStream::openHandle() void FileInputStream::openHandle()
{ {
totalSize = file.getSize();
const int f = open (file.getFullPathName().toUTF8(), O_RDONLY, 00644); const int f = open (file.getFullPathName().toUTF8(), O_RDONLY, 00644);
if (f != -1) if (f != -1)


+ 0
- 2
modules/juce_core/native/juce_win32_Files.cpp View File

@@ -222,8 +222,6 @@ int64 juce_fileSetPosition (void* handle, int64 pos)
void FileInputStream::openHandle() void FileInputStream::openHandle()
{ {
totalSize = file.getSize();
HANDLE h = CreateFile (file.getFullPathName().toWideCharPointer(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, 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); OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, 0);


+ 1
- 1
modules/juce_core/streams/juce_OutputStream.cpp View File

@@ -241,7 +241,7 @@ int OutputStream::writeFromInputStream (InputStream& source, int64 numBytesToWri
int numWritten = 0; int numWritten = 0;
while (numBytesToWrite > 0 && ! source.isExhausted())
while (numBytesToWrite > 0)
{ {
char buffer [8192]; char buffer [8192];
const int num = source.read (buffer, (int) jmin (numBytesToWrite, (int64) sizeof (buffer))); const int num = source.read (buffer, (int) jmin (numBytesToWrite, (int64) sizeof (buffer)));


Loading…
Cancel
Save