Skip to content

Commit

Permalink
Add wrappers around readsome and peek methods in std istream
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyRyabinin authored and sbiscigl committed Aug 30, 2024
1 parent a2cb2c5 commit a4ef09f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
24 changes: 24 additions & 0 deletions include/aws/crt/io/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ namespace Aws
*/
virtual bool ReadImpl(ByteBuf &buffer) noexcept = 0;

/***
* Read up-to buffer::capacity - buffer::len immediately available bytes into buffer::buffer
* Increment buffer::len by the amount you read in.
*
* @return true if nothing went wrong.
* Return true even if you read 0 bytes because the end-of-file has been reached.
* Return true even if you read 0 bytes because data is not currently available.
*
* Return false if an actual failure condition occurs,
* you SHOULD also raise an error via aws_raise_error().
*/
virtual bool ReadSomeImpl(ByteBuf &buffer) noexcept = 0;

/**
* @return the current status of the stream.
*/
Expand All @@ -136,6 +149,15 @@ namespace Aws
*/
virtual bool SeekImpl(int64_t offset, StreamSeekBasis seekBasis) noexcept = 0;

/**
* Peeks the stream
*
* Essentially calls peek on the underlying istream
*
* @return return value of the underlying istream::peek
*/
virtual int64_t PeekImpl() const noexcept = 0;

private:
static int s_Seek(aws_input_stream *stream, int64_t offset, enum aws_stream_seek_basis basis);
static int s_Read(aws_input_stream *stream, aws_byte_buf *dest);
Expand All @@ -161,9 +183,11 @@ namespace Aws

protected:
bool ReadImpl(ByteBuf &buffer) noexcept override;
bool ReadSomeImpl(ByteBuf &buffer) noexcept override;
StreamStatus GetStatusImpl() const noexcept override;
int64_t GetLengthImpl() const noexcept override;
bool SeekImpl(OffsetType offsetType, StreamSeekBasis seekBasis) noexcept override;
int64_t PeekImpl() const noexcept override;

private:
std::shared_ptr<Aws::Crt::Io::IStream> m_stream;
Expand Down
26 changes: 26 additions & 0 deletions source/io/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <aws/crt/StlAllocator.h>
#include <aws/crt/io/Stream.h>
#include <iostream>

#include <aws/io/stream.h>

Expand Down Expand Up @@ -150,6 +151,26 @@ namespace Aws
return status.is_valid && !status.is_end_of_stream;
}

bool StdIOStreamInputStream::ReadSomeImpl(ByteBuf &buffer) noexcept
{
// I have no idea why "readsome() doesn't work at all" for the original dev. It works well for me
// Jokes aside, read will always block and try to read till eof
// readsome will return available bytes without waiting for eof and without closing the stream.
auto actuallyRead = m_stream->readsome(
reinterpret_cast<char *>(buffer.buffer + buffer.len), buffer.capacity - buffer.len);

buffer.len += static_cast<size_t>(actuallyRead);

if (actuallyRead > 0 || (actuallyRead == 0 && m_stream->eof()))
{
return true;
}

auto status = GetStatusImpl();

return status.is_valid && !status.is_end_of_stream;
}

StreamStatus StdIOStreamInputStream::GetStatusImpl() const noexcept
{
StreamStatus status;
Expand Down Expand Up @@ -206,6 +227,11 @@ namespace Aws

return true;
}

int64_t StdIOStreamInputStream::PeekImpl() const noexcept
{
return m_stream->peek();
}
} // namespace Io
} // namespace Crt
} // namespace Aws

0 comments on commit a4ef09f

Please sign in to comment.