mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-20 02:55:50 +00:00
Beast includes a lot of code for encapsulating cross-platform differences which are not used or needed by rippled. Additionally, a lot of that code implements functionality that is available from the standard library. This moves away from custom implementations of features that the standard library provides and reduces the number of platform-specific interfaces andfeatures that Beast makes available. Highlights include: * Use std:: instead of beast implementations when possible * Reduce the use of beast::String in public interfaces * Remove Windows-specific COM and Registry code * Reduce the public interface of beast::File * Reduce the public interface of beast::SystemStats * Remove unused sysctl/getsysinfo functions * Remove beast::Logger
138 lines
3.7 KiB
C++
138 lines
3.7 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
This file is part of Beast: https://github.com/vinniefalco/Beast
|
|
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
|
|
|
Portions of this file are from JUCE.
|
|
Copyright (c) 2013 - Raw Material Software Ltd.
|
|
Please visit http://www.juce.com
|
|
|
|
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 THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
|
|
*/
|
|
//==============================================================================
|
|
|
|
#include <algorithm>
|
|
|
|
namespace beast
|
|
{
|
|
|
|
std::int64_t beast_fileSetPosition (void* handle, std::int64_t pos);
|
|
|
|
//==============================================================================
|
|
FileOutputStream::FileOutputStream (const File& f, const size_t bufferSizeToUse)
|
|
: file (f),
|
|
fileHandle (nullptr),
|
|
status (Result::ok()),
|
|
currentPosition (0),
|
|
bufferSize (bufferSizeToUse),
|
|
bytesInBuffer (0),
|
|
buffer (std::max (bufferSizeToUse, (size_t) 16))
|
|
{
|
|
openHandle();
|
|
}
|
|
|
|
FileOutputStream::~FileOutputStream()
|
|
{
|
|
flushBuffer();
|
|
flushInternal();
|
|
closeHandle();
|
|
}
|
|
|
|
std::int64_t FileOutputStream::getPosition()
|
|
{
|
|
return currentPosition;
|
|
}
|
|
|
|
bool FileOutputStream::setPosition (std::int64_t newPosition)
|
|
{
|
|
if (newPosition != currentPosition)
|
|
{
|
|
flushBuffer();
|
|
currentPosition = beast_fileSetPosition (fileHandle, newPosition);
|
|
}
|
|
|
|
return newPosition == currentPosition;
|
|
}
|
|
|
|
bool FileOutputStream::flushBuffer()
|
|
{
|
|
bool ok = true;
|
|
|
|
if (bytesInBuffer > 0)
|
|
{
|
|
ok = (writeInternal (buffer, bytesInBuffer) == (std::ptrdiff_t) bytesInBuffer);
|
|
bytesInBuffer = 0;
|
|
}
|
|
|
|
return ok;
|
|
}
|
|
|
|
void FileOutputStream::flush()
|
|
{
|
|
flushBuffer();
|
|
flushInternal();
|
|
}
|
|
|
|
bool FileOutputStream::write (const void* const src, const size_t numBytes)
|
|
{
|
|
bassert (src != nullptr && ((std::ptrdiff_t) numBytes) >= 0);
|
|
|
|
if (bytesInBuffer + numBytes < bufferSize)
|
|
{
|
|
memcpy (buffer + bytesInBuffer, src, numBytes);
|
|
bytesInBuffer += numBytes;
|
|
currentPosition += numBytes;
|
|
}
|
|
else
|
|
{
|
|
if (! flushBuffer())
|
|
return false;
|
|
|
|
if (numBytes < bufferSize)
|
|
{
|
|
memcpy (buffer + bytesInBuffer, src, numBytes);
|
|
bytesInBuffer += numBytes;
|
|
currentPosition += numBytes;
|
|
}
|
|
else
|
|
{
|
|
const std::ptrdiff_t bytesWritten = writeInternal (src, numBytes);
|
|
|
|
if (bytesWritten < 0)
|
|
return false;
|
|
|
|
currentPosition += bytesWritten;
|
|
return bytesWritten == (std::ptrdiff_t) numBytes;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool FileOutputStream::writeRepeatedByte (std::uint8_t byte, size_t numBytes)
|
|
{
|
|
bassert (((std::ptrdiff_t) numBytes) >= 0);
|
|
|
|
if (bytesInBuffer + numBytes < bufferSize)
|
|
{
|
|
memset (buffer + bytesInBuffer, byte, numBytes);
|
|
bytesInBuffer += numBytes;
|
|
currentPosition += numBytes;
|
|
return true;
|
|
}
|
|
|
|
return OutputStream::writeRepeatedByte (byte, numBytes);
|
|
}
|
|
|
|
} // beast
|