• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "ReadBuffer.h"
17 #include <string.h>
18 #include <assert.h>
19 
ReadBuffer(SocketStream * stream,size_t bufsize)20 ReadBuffer::ReadBuffer(SocketStream *stream, size_t bufsize)
21 {
22     m_size = bufsize;
23     m_stream = stream;
24     m_buf = new unsigned char[m_size];
25     m_validData = 0;
26     m_readPtr = m_buf;
27 }
28 
~ReadBuffer()29 ReadBuffer::~ReadBuffer()
30 {
31     delete m_buf;
32 }
33 
getData()34 int ReadBuffer::getData()
35 {
36     if (m_validData > 0) {
37         memcpy(m_buf, m_readPtr, m_validData);
38     }
39     m_readPtr = m_buf;
40     // get fresh data into the buffer;
41     int stat = m_stream->recv(m_buf + m_validData, m_size - m_validData);
42     if (stat > 0) {
43         m_validData += (size_t) stat;
44     }
45     return stat;
46 }
47 
consume(size_t amount)48 void ReadBuffer::consume(size_t amount)
49 {
50     assert(amount <= m_validData);
51     m_validData -= amount;
52     m_readPtr += amount;
53 }
54