1f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch/* 2f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * libjingle 3f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * Copyright 2004--2005, Google Inc. 4f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * 5f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * Redistribution and use in source and binary forms, with or without 6f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * modification, are permitted provided that the following conditions are met: 7f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * 8f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * 1. Redistributions of source code must retain the above copyright notice, 9f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * this list of conditions and the following disclaimer. 10f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * 2. Redistributions in binary form must reproduce the above copyright notice, 11f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * this list of conditions and the following disclaimer in the documentation 12f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * and/or other materials provided with the distribution. 13f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * 3. The name of the author may not be used to endorse or promote products 14f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * derived from this software without specific prior written permission. 15f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * 16f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch */ 27f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 28f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#ifndef TALK_BASE_BYTEBUFFER_H_ 29f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#define TALK_BASE_BYTEBUFFER_H_ 30f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 31f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#include <string> 32f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 33f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#include "talk/base/basictypes.h" 34f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#include "talk/base/constructormagic.h" 35f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 36f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdochnamespace talk_base { 37f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 38f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdochclass ByteBuffer { 39f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch public: 40f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch ByteBuffer(); 41f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch ByteBuffer(const char* bytes, size_t len); 42f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch explicit ByteBuffer(const char* bytes); // uses strlen 43f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch ~ByteBuffer(); 44f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 45f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch const char* Data() const { return bytes_ + start_; } 46f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch size_t Length() const { return end_ - start_; } 47f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch size_t Capacity() const { return size_ - start_; } 48f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 493345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick bool ReadUInt8(uint8* val); 503345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick bool ReadUInt16(uint16* val); 513345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick bool ReadUInt24(uint32* val); 523345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick bool ReadUInt32(uint32* val); 533345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick bool ReadUInt64(uint64* val); 543345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick bool ReadString(std::string* val, size_t len); // append to val 55f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch bool ReadBytes(char* val, size_t len); 56f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 57f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch void WriteUInt8(uint8 val); 58f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch void WriteUInt16(uint16 val); 59f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch void WriteUInt24(uint32 val); 60f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch void WriteUInt32(uint32 val); 613345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick void WriteUInt64(uint64 val); 62f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch void WriteString(const std::string& val); 63f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch void WriteBytes(const char* val, size_t len); 64f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 65f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch void Resize(size_t size); 66f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch void Consume(size_t size); 67f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch void Shift(size_t size); 68f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 69f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch private: 70f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch char* bytes_; 71f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch size_t size_; 72f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch size_t start_; 73f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch size_t end_; 74f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 75f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch // There are sensible ways to define these, but they aren't needed in our code 76f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch // base. 77f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch DISALLOW_COPY_AND_ASSIGN(ByteBuffer); 78f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch}; 79f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 80f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch} // namespace talk_base 81f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 82f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#endif // TALK_BASE_BYTEBUFFER_H_ 83