2020-09-02 18:09:00 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-02 18:09:00 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-11-22 04:02:56 +03:00
|
|
|
#include <AK/Noncopyable.h>
|
|
|
|
#include <AK/Span.h>
|
|
|
|
#include <AK/StdLibExtras.h>
|
2020-09-02 18:09:00 +03:00
|
|
|
#include <AK/Stream.h>
|
2020-11-22 04:02:56 +03:00
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/kmalloc.h>
|
2020-09-02 18:09:00 +03:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
// FIXME: Implement Buffered<T> for DuplexStream.
|
|
|
|
|
2022-03-17 20:29:46 +03:00
|
|
|
template<typename StreamType, size_t Size = 4096>
|
2020-09-02 18:09:00 +03:00
|
|
|
class Buffered;
|
|
|
|
|
|
|
|
template<typename StreamType, size_t Size>
|
2022-03-17 20:29:46 +03:00
|
|
|
requires(IsBaseOf<InputStream, StreamType>) class Buffered<StreamType, Size> final : public InputStream {
|
2020-10-03 20:11:11 +03:00
|
|
|
AK_MAKE_NONCOPYABLE(Buffered);
|
|
|
|
|
2020-09-02 18:09:00 +03:00
|
|
|
public:
|
|
|
|
template<typename... Parameters>
|
|
|
|
explicit Buffered(Parameters&&... parameters)
|
|
|
|
: m_stream(forward<Parameters>(parameters)...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-03 20:11:11 +03:00
|
|
|
Buffered(Buffered&& other)
|
|
|
|
: m_stream(move(other.m_stream))
|
|
|
|
{
|
|
|
|
other.buffer().copy_to(buffer());
|
|
|
|
m_buffered = exchange(other.m_buffered, 0);
|
|
|
|
}
|
|
|
|
|
2020-09-02 18:09:00 +03:00
|
|
|
bool has_recoverable_error() const override { return m_stream.has_recoverable_error(); }
|
|
|
|
bool has_fatal_error() const override { return m_stream.has_fatal_error(); }
|
|
|
|
bool has_any_error() const override { return m_stream.has_any_error(); }
|
|
|
|
|
|
|
|
bool handle_recoverable_error() override { return m_stream.handle_recoverable_error(); }
|
|
|
|
bool handle_fatal_error() override { return m_stream.handle_fatal_error(); }
|
|
|
|
bool handle_any_error() override { return m_stream.handle_any_error(); }
|
|
|
|
|
|
|
|
void set_recoverable_error() const override { return m_stream.set_recoverable_error(); }
|
|
|
|
void set_fatal_error() const override { return m_stream.set_fatal_error(); }
|
|
|
|
|
|
|
|
size_t read(Bytes bytes) override
|
|
|
|
{
|
2020-09-05 17:39:56 +03:00
|
|
|
if (has_any_error())
|
|
|
|
return 0;
|
|
|
|
|
2020-10-03 20:11:11 +03:00
|
|
|
auto nread = buffer().trim(m_buffered).copy_trimmed_to(bytes);
|
2020-09-02 18:09:00 +03:00
|
|
|
|
2020-10-03 20:11:11 +03:00
|
|
|
m_buffered -= nread;
|
2021-12-17 03:02:16 +03:00
|
|
|
if (m_buffered > 0)
|
|
|
|
buffer().slice(nread, m_buffered).copy_to(buffer());
|
2020-09-02 18:09:00 +03:00
|
|
|
|
|
|
|
if (nread < bytes.size()) {
|
2021-12-17 03:02:16 +03:00
|
|
|
nread += m_stream.read(bytes.slice(nread));
|
2020-09-02 18:09:00 +03:00
|
|
|
|
2021-12-17 03:02:16 +03:00
|
|
|
m_buffered = m_stream.read(buffer());
|
2020-09-02 18:09:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2020-09-13 13:24:17 +03:00
|
|
|
bool read_or_error(Bytes bytes) override
|
2020-09-02 18:09:00 +03:00
|
|
|
{
|
|
|
|
if (read(bytes) < bytes.size()) {
|
|
|
|
set_fatal_error();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-03 20:11:11 +03:00
|
|
|
bool unreliable_eof() const override { return m_buffered == 0 && m_stream.unreliable_eof(); }
|
2020-09-13 13:24:17 +03:00
|
|
|
|
|
|
|
bool eof() const
|
2020-09-02 18:09:00 +03:00
|
|
|
{
|
2020-10-03 20:11:11 +03:00
|
|
|
if (m_buffered > 0)
|
2020-09-02 18:09:00 +03:00
|
|
|
return false;
|
|
|
|
|
2020-10-03 20:11:11 +03:00
|
|
|
m_buffered = m_stream.read(buffer());
|
2020-09-02 18:09:00 +03:00
|
|
|
|
2020-10-03 20:11:11 +03:00
|
|
|
return m_buffered == 0;
|
2020-09-02 18:09:00 +03:00
|
|
|
}
|
|
|
|
|
2020-09-13 13:24:17 +03:00
|
|
|
bool discard_or_error(size_t count) override
|
2020-09-02 18:09:00 +03:00
|
|
|
{
|
|
|
|
size_t ndiscarded = 0;
|
|
|
|
while (ndiscarded < count) {
|
|
|
|
u8 dummy[Size];
|
|
|
|
|
|
|
|
if (!read_or_error({ dummy, min(Size, count - ndiscarded) }))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ndiscarded += min(Size, count - ndiscarded);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-03 12:39:03 +03:00
|
|
|
size_t buffered() const { return m_buffered; }
|
|
|
|
// Reading from the stream returned here will most definitely brick the buffering behavior of Buffered.
|
|
|
|
StreamType& underlying_stream() { return m_stream; }
|
|
|
|
|
2020-09-02 18:09:00 +03:00
|
|
|
private:
|
|
|
|
Bytes buffer() const { return { m_buffer, Size }; }
|
|
|
|
|
|
|
|
mutable StreamType m_stream;
|
|
|
|
mutable u8 m_buffer[Size];
|
2020-10-03 20:11:11 +03:00
|
|
|
mutable size_t m_buffered { 0 };
|
2020-09-02 18:09:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename StreamType, size_t Size>
|
2022-03-17 20:29:46 +03:00
|
|
|
requires(IsBaseOf<OutputStream, StreamType>) class Buffered<StreamType, Size> : public OutputStream {
|
2020-10-03 20:11:11 +03:00
|
|
|
AK_MAKE_NONCOPYABLE(Buffered);
|
|
|
|
|
2020-09-02 18:09:00 +03:00
|
|
|
public:
|
|
|
|
template<typename... Parameters>
|
|
|
|
explicit Buffered(Parameters&&... parameters)
|
|
|
|
: m_stream(forward<Parameters>(parameters)...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-03 20:11:11 +03:00
|
|
|
Buffered(Buffered&& other)
|
|
|
|
: m_stream(move(other.m_stream))
|
|
|
|
{
|
|
|
|
other.buffer().copy_to(buffer());
|
|
|
|
m_buffered = exchange(other.m_buffered, 0);
|
|
|
|
}
|
|
|
|
|
2020-09-02 18:09:00 +03:00
|
|
|
~Buffered()
|
|
|
|
{
|
2020-10-03 20:11:11 +03:00
|
|
|
if (m_buffered > 0)
|
|
|
|
flush();
|
2020-09-02 18:09:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool has_recoverable_error() const override { return m_stream.has_recoverable_error(); }
|
|
|
|
bool has_fatal_error() const override { return m_stream.has_fatal_error(); }
|
|
|
|
bool has_any_error() const override { return m_stream.has_any_error(); }
|
|
|
|
|
|
|
|
bool handle_recoverable_error() override { return m_stream.handle_recoverable_error(); }
|
|
|
|
bool handle_fatal_error() override { return m_stream.handle_fatal_error(); }
|
|
|
|
bool handle_any_error() override { return m_stream.handle_any_error(); }
|
|
|
|
|
|
|
|
void set_recoverable_error() const override { return m_stream.set_recoverable_error(); }
|
|
|
|
void set_fatal_error() const override { return m_stream.set_fatal_error(); }
|
|
|
|
|
|
|
|
size_t write(ReadonlyBytes bytes) override
|
|
|
|
{
|
|
|
|
if (has_any_error())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
auto nwritten = bytes.copy_trimmed_to(buffer().slice(m_buffered));
|
|
|
|
m_buffered += nwritten;
|
|
|
|
|
|
|
|
if (m_buffered == Size) {
|
|
|
|
flush();
|
|
|
|
|
|
|
|
if (bytes.size() - nwritten >= Size)
|
2020-10-03 20:32:15 +03:00
|
|
|
nwritten += m_stream.write(bytes.slice(nwritten));
|
2020-09-02 18:09:00 +03:00
|
|
|
|
|
|
|
nwritten += write(bytes.slice(nwritten));
|
|
|
|
}
|
|
|
|
|
|
|
|
return nwritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write_or_error(ReadonlyBytes bytes) override
|
|
|
|
{
|
|
|
|
write(bytes);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush()
|
|
|
|
{
|
|
|
|
m_stream.write_or_error({ m_buffer, m_buffered });
|
|
|
|
m_buffered = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Bytes buffer() { return { m_buffer, Size }; }
|
|
|
|
|
|
|
|
StreamType m_stream;
|
|
|
|
u8 m_buffer[Size];
|
|
|
|
size_t m_buffered { 0 };
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-09-02 18:09:00 +03:00
|
|
|
using AK::Buffered;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|