Delete StreamInterface methods GetPosition, SetPosition and Rewind

Keep methods on subclasses where they are used: FifoBuffer and
MemoryStream. Also FileStream gets to keep SetPosition, because it's
used by a downstream subclass.

Bug: webrtc:6424
Change-Id: If2a00855aba7c2c9dc0909cda7c8a8ef00e0b9af
Reviewed-on: https://webrtc-review.googlesource.com/c/116487
Commit-Queue: Niels Moller <nisse@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26237}
This commit is contained in:
Niels Möller 2019-01-14 12:48:53 +01:00 committed by Commit Bot
parent eb0de2284e
commit 1a86b78180
6 changed files with 22 additions and 92 deletions

View file

@ -40,8 +40,6 @@ class StringStream : public StreamInterface {
size_t* written,
int* error) override;
void Close() override;
bool SetPosition(size_t position) override;
bool GetPosition(size_t* position) const override;
private:
std::string& str_;
@ -92,19 +90,6 @@ StreamResult StringStream::Write(const void* data,
void StringStream::Close() {}
bool StringStream::SetPosition(size_t position) {
if (position > str_.size())
return false;
read_pos_ = position;
return true;
}
bool StringStream::GetPosition(size_t* position) const {
if (position)
*position = read_pos_;
return true;
}
} // namespace
template <typename Base>

View file

@ -91,6 +91,10 @@ bool MemoryStream::GetPosition(size_t* position) const {
return true;
}
void MemoryStream::Rewind() {
seek_position_ = 0;
}
bool MemoryStream::GetSize(size_t* size) const {
if (size)
*size = data_length_;

View file

@ -36,11 +36,13 @@ class MemoryStream final : public StreamInterface {
size_t* bytes_written,
int* error) override;
void Close() override;
bool SetPosition(size_t position) override;
bool GetPosition(size_t* position) const override;
bool GetSize(size_t* size) const;
bool ReserveSize(size_t size);
bool SetPosition(size_t position);
bool GetPosition(size_t* position) const;
void Rewind();
char* GetBuffer() { return buffer_; }
const char* GetBuffer() const { return buffer_; }

View file

@ -77,14 +77,6 @@ void StreamInterface::PostEvent(int events, int err) {
PostEvent(Thread::Current(), events, err);
}
bool StreamInterface::SetPosition(size_t position) {
return false;
}
bool StreamInterface::GetPosition(size_t* position) const {
return false;
}
bool StreamInterface::Flush() {
return false;
}
@ -129,14 +121,6 @@ void StreamAdapterInterface::Close() {
stream_->Close();
}
bool StreamAdapterInterface::SetPosition(size_t position) {
return stream_->SetPosition(position);
}
bool StreamAdapterInterface::GetPosition(size_t* position) const {
return stream_->GetPosition(position);
}
bool StreamAdapterInterface::Flush() {
return stream_->Flush();
}
@ -288,18 +272,6 @@ bool FileStream::SetPosition(size_t position) {
return (fseek(file_, static_cast<int>(position), SEEK_SET) == 0);
}
bool FileStream::GetPosition(size_t* position) const {
RTC_DCHECK(nullptr != position);
if (!file_)
return false;
long result = ftell(file_);
if (result < 0)
return false;
if (position)
*position = result;
return true;
}
bool FileStream::Flush() {
if (file_) {
return (0 == fflush(file_));

View file

@ -107,15 +107,6 @@ class StreamInterface : public MessageHandler {
// Like the aforementioned method, but posts to the current thread.
void PostEvent(int events, int err);
// Seek to a byte offset from the beginning of the stream. Returns false if
// the stream does not support seeking, or cannot seek to the specified
// position.
virtual bool SetPosition(size_t position);
// Get the byte offset of the current position from the start of the stream.
// Returns false if the position is not known.
virtual bool GetPosition(size_t* position) const;
// Return true if flush is successful.
virtual bool Flush();
@ -125,9 +116,6 @@ class StreamInterface : public MessageHandler {
// These methods are implemented in terms of other methods, for convenience.
//
// Seek to the start of the stream.
inline bool Rewind() { return SetPosition(0); }
// WriteAll is a helper function which repeatedly calls Write until all the
// data is written, or something other than SR_SUCCESS is returned. Note that
// unlike Write, the argument 'written' is always set, and may be non-zero
@ -180,8 +168,6 @@ class StreamAdapterInterface : public StreamInterface,
int* error) override;
void Close() override;
bool SetPosition(size_t position) override;
bool GetPosition(size_t* position) const override;
bool Flush() override;
void Attach(StreamInterface* stream, bool owned = true);
@ -232,8 +218,7 @@ class FileStream : public StreamInterface {
size_t* written,
int* error) override;
void Close() override;
bool SetPosition(size_t position) override;
bool GetPosition(size_t* position) const override;
virtual bool SetPosition(size_t position);
bool Flush() override;
@ -291,6 +276,19 @@ class FifoBuffer final : public StreamInterface {
size_t* bytes_written,
int* error) override;
void Close() override;
// Seek to a byte offset from the beginning of the stream. Returns false if
// the stream does not support seeking, or cannot seek to the specified
// position.
bool SetPosition(size_t position);
// Get the byte offset of the current position from the start of the stream.
// Returns false if the position is not known.
bool GetPosition(size_t* position) const;
// Seek to the start of the stream.
bool Rewind() { return SetPosition(0); }
// GetReadData returns a pointer to a buffer which is owned by the stream.
// The buffer contains data_len bytes. null is returned if no data is
// available, or if the method fails. If the caller processes the data, it

View file

@ -50,17 +50,6 @@ class TestStream : public StreamInterface {
void Close() override {}
bool SetPosition(size_t position) override {
pos_ = position;
return true;
}
bool GetPosition(size_t* position) const override {
if (position)
*position = pos_;
return true;
}
private:
size_t pos_;
};
@ -78,26 +67,6 @@ bool VerifyTestBuffer(unsigned char* buffer, size_t len, unsigned char value) {
return passed;
}
void SeekTest(StreamInterface* stream, const unsigned char value) {
size_t bytes;
unsigned char buffer[13] = {0};
const size_t kBufSize = sizeof(buffer);
EXPECT_EQ(stream->Read(buffer, kBufSize, &bytes, nullptr), SR_SUCCESS);
EXPECT_EQ(bytes, kBufSize);
EXPECT_TRUE(VerifyTestBuffer(buffer, kBufSize, value));
EXPECT_TRUE(stream->GetPosition(&bytes));
EXPECT_EQ(13U, bytes);
EXPECT_TRUE(stream->SetPosition(7));
EXPECT_EQ(stream->Read(buffer, kBufSize, &bytes, nullptr), SR_SUCCESS);
EXPECT_EQ(bytes, kBufSize);
EXPECT_TRUE(VerifyTestBuffer(buffer, kBufSize, value + 7));
EXPECT_TRUE(stream->GetPosition(&bytes));
EXPECT_EQ(20U, bytes);
}
TEST(FifoBufferTest, TestAll) {
const size_t kSize = 16;
const char in[kSize * 2 + 1] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";