diff --git a/Userland/Libraries/LibAudio/FlacLoader.cpp b/Userland/Libraries/LibAudio/FlacLoader.cpp index d7606d98ce5..b45238aa927 100644 --- a/Userland/Libraries/LibAudio/FlacLoader.cpp +++ b/Userland/Libraries/LibAudio/FlacLoader.cpp @@ -60,7 +60,7 @@ MaybeLoaderError FlacLoaderPlugin::initialize() // 11.5 STREAM MaybeLoaderError FlacLoaderPlugin::parse_header() { - auto bit_input = LOADER_TRY(BigEndianInputBitStream::construct(*m_stream)); + auto bit_input = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle(*m_stream))); // A mixture of VERIFY and the non-crashing TRY(). #define FLAC_VERIFY(check, category, msg) \ @@ -79,7 +79,7 @@ MaybeLoaderError FlacLoaderPlugin::parse_header() auto streaminfo = TRY(next_meta_block(*bit_input)); FLAC_VERIFY(streaminfo.type == FlacMetadataBlockType::STREAMINFO, LoaderError::Category::Format, "First block must be STREAMINFO"); auto streaminfo_data_memory = LOADER_TRY(Core::Stream::MemoryStream::construct(streaminfo.data.bytes())); - auto streaminfo_data = LOADER_TRY(BigEndianInputBitStream::construct(*streaminfo_data_memory)); + auto streaminfo_data = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle(*streaminfo_data_memory))); // 11.10 METADATA_BLOCK_STREAMINFO m_min_block_size = LOADER_TRY(streaminfo_data->read_bits(16)); @@ -150,7 +150,7 @@ MaybeLoaderError FlacLoaderPlugin::parse_header() MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block) { auto memory_stream = LOADER_TRY(Core::Stream::MemoryStream::construct(block.data.bytes())); - auto picture_block_bytes = LOADER_TRY(BigEndianInputBitStream::construct(*memory_stream)); + auto picture_block_bytes = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle(*memory_stream))); PictureData picture {}; @@ -187,7 +187,7 @@ MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block) MaybeLoaderError FlacLoaderPlugin::load_seektable(FlacRawMetadataBlock& block) { auto memory_stream = LOADER_TRY(Core::Stream::MemoryStream::construct(block.data.bytes())); - auto seektable_bytes = LOADER_TRY(BigEndianInputBitStream::construct(*memory_stream)); + auto seektable_bytes = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle(*memory_stream))); for (size_t i = 0; i < block.length / 18; ++i) { // 11.14. SEEKPOINT FlacSeekPoint seekpoint { @@ -333,7 +333,7 @@ MaybeLoaderError FlacLoaderPlugin::next_frame(Span target_vector) } \ } while (0) - auto bit_stream = LOADER_TRY(BigEndianInputBitStream::construct(*m_stream)); + auto bit_stream = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle(*m_stream))); // TODO: Check the CRC-16 checksum (and others) by keeping track of read data diff --git a/Userland/Libraries/LibAudio/MP3Loader.cpp b/Userland/Libraries/LibAudio/MP3Loader.cpp index 3a28074c0e6..229921653c7 100644 --- a/Userland/Libraries/LibAudio/MP3Loader.cpp +++ b/Userland/Libraries/LibAudio/MP3Loader.cpp @@ -41,7 +41,7 @@ Result, LoaderError> MP3LoaderPlugin::try_create( MaybeLoaderError MP3LoaderPlugin::initialize() { - m_bitstream = LOADER_TRY(Core::Stream::BigEndianInputBitStream::construct(*m_stream)); + m_bitstream = LOADER_TRY(Core::Stream::BigEndianInputBitStream::construct(Core::Stream::Handle(*m_stream))); TRY(synchronize()); diff --git a/Userland/Libraries/LibCompress/Brotli.cpp b/Userland/Libraries/LibCompress/Brotli.cpp index 16344f09469..00d2e7c3c03 100644 --- a/Userland/Libraries/LibCompress/Brotli.cpp +++ b/Userland/Libraries/LibCompress/Brotli.cpp @@ -29,7 +29,7 @@ ErrorOr BrotliDecompressionStream::CanonicalCode::read_symbol(LittleEndi } BrotliDecompressionStream::BrotliDecompressionStream(Stream& stream) - : m_input_stream(stream) + : m_input_stream(Core::Stream::Handle(stream)) { } diff --git a/Userland/Libraries/LibCore/InputBitStream.h b/Userland/Libraries/LibCore/InputBitStream.h index 3cd9615a68b..f78877508a5 100644 --- a/Userland/Libraries/LibCore/InputBitStream.h +++ b/Userland/Libraries/LibCore/InputBitStream.h @@ -21,12 +21,11 @@ namespace Core::Stream { /// A stream wrapper class that allows you to read arbitrary amounts of bits /// in big-endian order from another stream. -/// Note that this stream does not own its underlying stream, it merely takes a reference. class BigEndianInputBitStream : public Stream { public: - static ErrorOr> construct(Stream& stream) + static ErrorOr> construct(Handle stream) { - return adopt_nonnull_own_or_enomem(new BigEndianInputBitStream(stream)); + return adopt_nonnull_own_or_enomem(new BigEndianInputBitStream(move(stream))); } // ^Stream @@ -34,18 +33,18 @@ public: { if (m_current_byte.has_value() && is_aligned_to_byte_boundary()) { bytes[0] = m_current_byte.release_value(); - return m_stream.read(bytes.slice(1)); + return m_stream->read(bytes.slice(1)); } align_to_byte_boundary(); - return m_stream.read(bytes); + return m_stream->read(bytes); } - virtual ErrorOr write(ReadonlyBytes bytes) override { return m_stream.write(bytes); } - virtual ErrorOr write_entire_buffer(ReadonlyBytes bytes) override { return m_stream.write_entire_buffer(bytes); } - virtual bool is_eof() const override { return m_stream.is_eof() && !m_current_byte.has_value(); } - virtual bool is_open() const override { return m_stream.is_open(); } + virtual ErrorOr write(ReadonlyBytes bytes) override { return m_stream->write(bytes); } + virtual ErrorOr write_entire_buffer(ReadonlyBytes bytes) override { return m_stream->write_entire_buffer(bytes); } + virtual bool is_eof() const override { return m_stream->is_eof() && !m_current_byte.has_value(); } + virtual bool is_open() const override { return m_stream->is_open(); } virtual void close() override { - m_stream.close(); + m_stream->close(); align_to_byte_boundary(); } @@ -98,7 +97,7 @@ public: } } else { auto temp_buffer = TRY(ByteBuffer::create_uninitialized(1)); - TRY(m_stream.read(temp_buffer.bytes())); + TRY(m_stream->read(temp_buffer.bytes())); m_current_byte = temp_buffer[0]; m_bit_offset = 0; } @@ -119,28 +118,27 @@ public: ALWAYS_INLINE bool is_aligned_to_byte_boundary() const { return m_bit_offset == 0; } private: - BigEndianInputBitStream(Stream& stream) - : m_stream(stream) + BigEndianInputBitStream(Handle stream) + : m_stream(move(stream)) { } Optional m_current_byte; size_t m_bit_offset { 0 }; - Stream& m_stream; + Handle m_stream; }; /// A stream wrapper class that allows you to read arbitrary amounts of bits /// in little-endian order from another stream. -/// Note that this stream does not own its underlying stream, it merely takes a reference. class LittleEndianInputBitStream : public Stream { public: - static ErrorOr> construct(Stream& stream) + static ErrorOr> construct(Handle stream) { - return adopt_nonnull_own_or_enomem(new LittleEndianInputBitStream(stream)); + return adopt_nonnull_own_or_enomem(new LittleEndianInputBitStream(move(stream))); } - LittleEndianInputBitStream(Stream& stream) - : m_stream(stream) + LittleEndianInputBitStream(Handle stream) + : m_stream(move(stream)) { } @@ -149,18 +147,18 @@ public: { if (m_current_byte.has_value() && is_aligned_to_byte_boundary()) { bytes[0] = m_current_byte.release_value(); - return m_stream.read(bytes.slice(1)); + return m_stream->read(bytes.slice(1)); } align_to_byte_boundary(); - return m_stream.read(bytes); + return m_stream->read(bytes); } - virtual ErrorOr write(ReadonlyBytes bytes) override { return m_stream.write(bytes); } - virtual ErrorOr write_entire_buffer(ReadonlyBytes bytes) override { return m_stream.write_entire_buffer(bytes); } - virtual bool is_eof() const override { return m_stream.is_eof() && !m_current_byte.has_value(); } - virtual bool is_open() const override { return m_stream.is_open(); } + virtual ErrorOr write(ReadonlyBytes bytes) override { return m_stream->write(bytes); } + virtual ErrorOr write_entire_buffer(ReadonlyBytes bytes) override { return m_stream->write_entire_buffer(bytes); } + virtual bool is_eof() const override { return m_stream->is_eof() && !m_current_byte.has_value(); } + virtual bool is_open() const override { return m_stream->is_open(); } virtual void close() override { - m_stream.close(); + m_stream->close(); align_to_byte_boundary(); } @@ -209,7 +207,7 @@ public: } } else { auto temp_buffer = TRY(ByteBuffer::create_uninitialized(1)); - auto read_bytes = TRY(m_stream.read(temp_buffer.bytes())); + auto read_bytes = TRY(m_stream->read(temp_buffer.bytes())); if (read_bytes.is_empty()) return Error::from_string_literal("eof"); m_current_byte = temp_buffer[0]; @@ -236,7 +234,7 @@ public: private: Optional m_current_byte; size_t m_bit_offset { 0 }; - Stream& m_stream; + Handle m_stream; }; }