|
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2016 - ROLI Ltd.
-
- Permission is granted to use this software under the terms of the ISC license
- http://www.isc.org/downloads/software-support-policy/isc-license/
-
- Permission to use, copy, modify, and/or distribute this software for any
- purpose with or without fee is hereby granted, provided that the above
- copyright notice and this permission notice appear in all copies.
-
- THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
- TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
- FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
- OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
- USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
- TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
- OF THIS SOFTWARE.
-
- -----------------------------------------------------------------------------
-
- To release a closed-source product which uses other parts of JUCE not
- licensed under the ISC terms, commercial licenses are available: visit
- www.juce.com for more information.
-
- ==============================================================================
- */
-
- int64 juce_fileSetPosition (void* handle, int64 pos);
-
- //==============================================================================
- FileOutputStream::FileOutputStream (const File& f, const size_t bufferSizeToUse)
- : file (f),
- fileHandle (nullptr),
- status (Result::ok()),
- currentPosition (0),
- bufferSize (bufferSizeToUse),
- bytesInBuffer (0),
- buffer (jmax (bufferSizeToUse, (size_t) 16))
- {
- openHandle();
- }
-
- FileOutputStream::~FileOutputStream()
- {
- flushBuffer();
- closeHandle();
- }
-
- int64 FileOutputStream::getPosition()
- {
- return currentPosition;
- }
-
- bool FileOutputStream::setPosition (int64 newPosition)
- {
- if (newPosition != currentPosition)
- {
- flushBuffer();
- currentPosition = juce_fileSetPosition (fileHandle, newPosition);
- }
-
- return newPosition == currentPosition;
- }
-
- bool FileOutputStream::flushBuffer()
- {
- bool ok = true;
-
- if (bytesInBuffer > 0)
- {
- ok = (writeInternal (buffer, bytesInBuffer) == (ssize_t) bytesInBuffer);
- bytesInBuffer = 0;
- }
-
- return ok;
- }
-
- void FileOutputStream::flush()
- {
- flushBuffer();
- flushInternal();
- }
-
- bool FileOutputStream::write (const void* const src, const size_t numBytes)
- {
- jassert (src != nullptr && ((ssize_t) numBytes) >= 0);
-
- if (bytesInBuffer + numBytes < bufferSize)
- {
- memcpy (buffer + bytesInBuffer, src, numBytes);
- bytesInBuffer += numBytes;
- currentPosition += (int64) numBytes;
- }
- else
- {
- if (! flushBuffer())
- return false;
-
- if (numBytes < bufferSize)
- {
- memcpy (buffer + bytesInBuffer, src, numBytes);
- bytesInBuffer += numBytes;
- currentPosition += (int64) numBytes;
- }
- else
- {
- const ssize_t bytesWritten = writeInternal (src, numBytes);
-
- if (bytesWritten < 0)
- return false;
-
- currentPosition += (int64) bytesWritten;
- return bytesWritten == (ssize_t) numBytes;
- }
- }
-
- return true;
- }
-
- bool FileOutputStream::writeRepeatedByte (uint8 byte, size_t numBytes)
- {
- jassert (((ssize_t) numBytes) >= 0);
-
- if (bytesInBuffer + numBytes < bufferSize)
- {
- memset (buffer + bytesInBuffer, byte, numBytes);
- bytesInBuffer += numBytes;
- currentPosition += (int64) numBytes;
- return true;
- }
-
- return OutputStream::writeRepeatedByte (byte, numBytes);
- }
|