| Index: webrtc/base/bytebuffer.cc
|
| diff --git a/webrtc/base/bytebuffer.cc b/webrtc/base/bytebuffer.cc
|
| index 8bc1f23670e7917736ee1e79fa304b57ca1187ff..cf4ce42574e959e8df76c23b6b6b37e45b1b94fc 100644
|
| --- a/webrtc/base/bytebuffer.cc
|
| +++ b/webrtc/base/bytebuffer.cc
|
| @@ -22,36 +22,30 @@ namespace rtc {
|
|
|
| static const int DEFAULT_SIZE = 4096;
|
|
|
| -ByteBuffer::ByteBuffer() {
|
| - Construct(NULL, DEFAULT_SIZE, ORDER_NETWORK);
|
| +ByteBufferWriter::ByteBufferWriter()
|
| + : ByteBuffer(ORDER_NETWORK) {
|
| + Construct(NULL, DEFAULT_SIZE);
|
| }
|
|
|
| -ByteBuffer::ByteBuffer(ByteOrder byte_order) {
|
| - Construct(NULL, DEFAULT_SIZE, byte_order);
|
| +ByteBufferWriter::ByteBufferWriter(ByteOrder byte_order)
|
| + : ByteBuffer(byte_order) {
|
| + Construct(NULL, DEFAULT_SIZE);
|
| }
|
|
|
| -ByteBuffer::ByteBuffer(const char* bytes, size_t len) {
|
| - Construct(bytes, len, ORDER_NETWORK);
|
| +ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len)
|
| + : ByteBuffer(ORDER_NETWORK) {
|
| + Construct(bytes, len);
|
| }
|
|
|
| -ByteBuffer::ByteBuffer(const char* bytes, size_t len, ByteOrder byte_order) {
|
| - Construct(bytes, len, byte_order);
|
| +ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len,
|
| + ByteOrder byte_order)
|
| + : ByteBuffer(byte_order) {
|
| + Construct(bytes, len);
|
| }
|
|
|
| -ByteBuffer::ByteBuffer(const char* bytes) {
|
| - Construct(bytes, strlen(bytes), ORDER_NETWORK);
|
| -}
|
| -
|
| -ByteBuffer::ByteBuffer(const Buffer& buf) {
|
| - Construct(buf.data<char>(), buf.size(), ORDER_NETWORK);
|
| -}
|
| -
|
| -void ByteBuffer::Construct(const char* bytes, size_t len,
|
| - ByteOrder byte_order) {
|
| - version_ = 0;
|
| +void ByteBufferWriter::Construct(const char* bytes, size_t len) {
|
| start_ = 0;
|
| size_ = len;
|
| - byte_order_ = byte_order;
|
| bytes_ = new char[size_];
|
|
|
| if (bytes) {
|
| @@ -62,70 +56,171 @@ void ByteBuffer::Construct(const char* bytes, size_t len,
|
| }
|
| }
|
|
|
| -ByteBuffer::~ByteBuffer() {
|
| +ByteBufferWriter::~ByteBufferWriter() {
|
| delete[] bytes_;
|
| }
|
|
|
| -bool ByteBuffer::ReadUInt8(uint8_t* val) {
|
| +void ByteBufferWriter::WriteUInt8(uint8_t val) {
|
| + WriteBytes(reinterpret_cast<const char*>(&val), 1);
|
| +}
|
| +
|
| +void ByteBufferWriter::WriteUInt16(uint16_t val) {
|
| + uint16_t v = (Order() == ORDER_NETWORK) ? HostToNetwork16(val) : val;
|
| + WriteBytes(reinterpret_cast<const char*>(&v), 2);
|
| +}
|
| +
|
| +void ByteBufferWriter::WriteUInt24(uint32_t val) {
|
| + uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
|
| + char* start = reinterpret_cast<char*>(&v);
|
| + if (Order() == ORDER_NETWORK || IsHostBigEndian()) {
|
| + ++start;
|
| + }
|
| + WriteBytes(start, 3);
|
| +}
|
| +
|
| +void ByteBufferWriter::WriteUInt32(uint32_t val) {
|
| + uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
|
| + WriteBytes(reinterpret_cast<const char*>(&v), 4);
|
| +}
|
| +
|
| +void ByteBufferWriter::WriteUInt64(uint64_t val) {
|
| + uint64_t v = (Order() == ORDER_NETWORK) ? HostToNetwork64(val) : val;
|
| + WriteBytes(reinterpret_cast<const char*>(&v), 8);
|
| +}
|
| +
|
| +void ByteBufferWriter::WriteString(const std::string& val) {
|
| + WriteBytes(val.c_str(), val.size());
|
| +}
|
| +
|
| +void ByteBufferWriter::WriteBytes(const char* val, size_t len) {
|
| + memcpy(ReserveWriteBuffer(len), val, len);
|
| +}
|
| +
|
| +char* ByteBufferWriter::ReserveWriteBuffer(size_t len) {
|
| + if (Length() + len > Capacity())
|
| + Resize(Length() + len);
|
| +
|
| + char* start = bytes_ + end_;
|
| + end_ += len;
|
| + return start;
|
| +}
|
| +
|
| +void ByteBufferWriter::Resize(size_t size) {
|
| + size_t len = std::min(end_ - start_, size);
|
| + if (size <= size_) {
|
| + // Don't reallocate, just move data backwards
|
| + memmove(bytes_, bytes_ + start_, len);
|
| + } else {
|
| + // Reallocate a larger buffer.
|
| + size_ = std::max(size, 3 * size_ / 2);
|
| + char* new_bytes = new char[size_];
|
| + memcpy(new_bytes, bytes_ + start_, len);
|
| + delete [] bytes_;
|
| + bytes_ = new_bytes;
|
| + }
|
| + start_ = 0;
|
| + end_ = len;
|
| +}
|
| +
|
| +void ByteBufferWriter::Clear() {
|
| + memset(bytes_, 0, size_);
|
| + start_ = end_ = 0;
|
| +}
|
| +
|
| +
|
| +ByteBufferReader::ByteBufferReader(const char* bytes, size_t len)
|
| + : ByteBuffer(ORDER_NETWORK) {
|
| + Construct(bytes, len);
|
| +}
|
| +
|
| +ByteBufferReader::ByteBufferReader(const char* bytes, size_t len,
|
| + ByteOrder byte_order)
|
| + : ByteBuffer(byte_order) {
|
| + Construct(bytes, len);
|
| +}
|
| +
|
| +ByteBufferReader::ByteBufferReader(const char* bytes)
|
| + : ByteBuffer(ORDER_NETWORK) {
|
| + Construct(bytes, strlen(bytes));
|
| +}
|
| +
|
| +ByteBufferReader::ByteBufferReader(const Buffer& buf)
|
| + : ByteBuffer(ORDER_NETWORK) {
|
| + Construct(buf.data<char>(), buf.size());
|
| +}
|
| +
|
| +ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf)
|
| + : ByteBuffer(buf.Order()) {
|
| + Construct(buf.Data(), buf.Length());
|
| +}
|
| +
|
| +void ByteBufferReader::Construct(const char* bytes, size_t len) {
|
| + bytes_ = bytes;
|
| + size_ = len;
|
| + start_ = 0;
|
| + end_ = len;
|
| +}
|
| +
|
| +bool ByteBufferReader::ReadUInt8(uint8_t* val) {
|
| if (!val) return false;
|
|
|
| return ReadBytes(reinterpret_cast<char*>(val), 1);
|
| }
|
|
|
| -bool ByteBuffer::ReadUInt16(uint16_t* val) {
|
| +bool ByteBufferReader::ReadUInt16(uint16_t* val) {
|
| if (!val) return false;
|
|
|
| uint16_t v;
|
| if (!ReadBytes(reinterpret_cast<char*>(&v), 2)) {
|
| return false;
|
| } else {
|
| - *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost16(v) : v;
|
| + *val = (Order() == ORDER_NETWORK) ? NetworkToHost16(v) : v;
|
| return true;
|
| }
|
| }
|
|
|
| -bool ByteBuffer::ReadUInt24(uint32_t* val) {
|
| +bool ByteBufferReader::ReadUInt24(uint32_t* val) {
|
| if (!val) return false;
|
|
|
| uint32_t v = 0;
|
| char* read_into = reinterpret_cast<char*>(&v);
|
| - if (byte_order_ == ORDER_NETWORK || IsHostBigEndian()) {
|
| + if (Order() == ORDER_NETWORK || IsHostBigEndian()) {
|
| ++read_into;
|
| }
|
|
|
| if (!ReadBytes(read_into, 3)) {
|
| return false;
|
| } else {
|
| - *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost32(v) : v;
|
| + *val = (Order() == ORDER_NETWORK) ? NetworkToHost32(v) : v;
|
| return true;
|
| }
|
| }
|
|
|
| -bool ByteBuffer::ReadUInt32(uint32_t* val) {
|
| +bool ByteBufferReader::ReadUInt32(uint32_t* val) {
|
| if (!val) return false;
|
|
|
| uint32_t v;
|
| if (!ReadBytes(reinterpret_cast<char*>(&v), 4)) {
|
| return false;
|
| } else {
|
| - *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost32(v) : v;
|
| + *val = (Order() == ORDER_NETWORK) ? NetworkToHost32(v) : v;
|
| return true;
|
| }
|
| }
|
|
|
| -bool ByteBuffer::ReadUInt64(uint64_t* val) {
|
| +bool ByteBufferReader::ReadUInt64(uint64_t* val) {
|
| if (!val) return false;
|
|
|
| uint64_t v;
|
| if (!ReadBytes(reinterpret_cast<char*>(&v), 8)) {
|
| return false;
|
| } else {
|
| - *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost64(v) : v;
|
| + *val = (Order() == ORDER_NETWORK) ? NetworkToHost64(v) : v;
|
| return true;
|
| }
|
| }
|
|
|
| -bool ByteBuffer::ReadString(std::string* val, size_t len) {
|
| +bool ByteBufferReader::ReadString(std::string* val, size_t len) {
|
| if (!val) return false;
|
|
|
| if (len > Length()) {
|
| @@ -137,7 +232,7 @@ bool ByteBuffer::ReadString(std::string* val, size_t len) {
|
| }
|
| }
|
|
|
| -bool ByteBuffer::ReadBytes(char* val, size_t len) {
|
| +bool ByteBufferReader::ReadBytes(char* val, size_t len) {
|
| if (len > Length()) {
|
| return false;
|
| } else {
|
| @@ -147,92 +242,11 @@ bool ByteBuffer::ReadBytes(char* val, size_t len) {
|
| }
|
| }
|
|
|
| -void ByteBuffer::WriteUInt8(uint8_t val) {
|
| - WriteBytes(reinterpret_cast<const char*>(&val), 1);
|
| -}
|
| -
|
| -void ByteBuffer::WriteUInt16(uint16_t val) {
|
| - uint16_t v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork16(val) : val;
|
| - WriteBytes(reinterpret_cast<const char*>(&v), 2);
|
| -}
|
| -
|
| -void ByteBuffer::WriteUInt24(uint32_t val) {
|
| - uint32_t v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork32(val) : val;
|
| - char* start = reinterpret_cast<char*>(&v);
|
| - if (byte_order_ == ORDER_NETWORK || IsHostBigEndian()) {
|
| - ++start;
|
| - }
|
| - WriteBytes(start, 3);
|
| -}
|
| -
|
| -void ByteBuffer::WriteUInt32(uint32_t val) {
|
| - uint32_t v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork32(val) : val;
|
| - WriteBytes(reinterpret_cast<const char*>(&v), 4);
|
| -}
|
| -
|
| -void ByteBuffer::WriteUInt64(uint64_t val) {
|
| - uint64_t v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork64(val) : val;
|
| - WriteBytes(reinterpret_cast<const char*>(&v), 8);
|
| -}
|
| -
|
| -void ByteBuffer::WriteString(const std::string& val) {
|
| - WriteBytes(val.c_str(), val.size());
|
| -}
|
| -
|
| -void ByteBuffer::WriteBytes(const char* val, size_t len) {
|
| - memcpy(ReserveWriteBuffer(len), val, len);
|
| -}
|
| -
|
| -char* ByteBuffer::ReserveWriteBuffer(size_t len) {
|
| - if (Length() + len > Capacity())
|
| - Resize(Length() + len);
|
| -
|
| - char* start = bytes_ + end_;
|
| - end_ += len;
|
| - return start;
|
| -}
|
| -
|
| -void ByteBuffer::Resize(size_t size) {
|
| - size_t len = std::min(end_ - start_, size);
|
| - if (size <= size_) {
|
| - // Don't reallocate, just move data backwards
|
| - memmove(bytes_, bytes_ + start_, len);
|
| - } else {
|
| - // Reallocate a larger buffer.
|
| - size_ = std::max(size, 3 * size_ / 2);
|
| - char* new_bytes = new char[size_];
|
| - memcpy(new_bytes, bytes_ + start_, len);
|
| - delete [] bytes_;
|
| - bytes_ = new_bytes;
|
| - }
|
| - start_ = 0;
|
| - end_ = len;
|
| - ++version_;
|
| -}
|
| -
|
| -bool ByteBuffer::Consume(size_t size) {
|
| +bool ByteBufferReader::Consume(size_t size) {
|
| if (size > Length())
|
| return false;
|
| start_ += size;
|
| return true;
|
| }
|
|
|
| -ByteBuffer::ReadPosition ByteBuffer::GetReadPosition() const {
|
| - return ReadPosition(start_, version_);
|
| -}
|
| -
|
| -bool ByteBuffer::SetReadPosition(const ReadPosition &position) {
|
| - if (position.version_ != version_) {
|
| - return false;
|
| - }
|
| - start_ = position.start_;
|
| - return true;
|
| -}
|
| -
|
| -void ByteBuffer::Clear() {
|
| - memset(bytes_, 0, size_);
|
| - start_ = end_ = 0;
|
| - ++version_;
|
| -}
|
| -
|
| } // namespace rtc
|
|
|