1 /*
2 * Copyright 2019 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 <algorithm>
17 #include <string.h>
18
19 #include "MemInputStream.h"
20
21 namespace parselib {
22
read(void * buff,int32_t numBytes)23 int32_t MemInputStream::read(void *buff, int32_t numBytes) {
24 int32_t numAvail = mBufferLen - mPos;
25 numBytes = std::min(numBytes, numAvail);
26
27 peek(buff, numBytes);
28 mPos += numBytes;
29 return numBytes;
30 }
31
peek(void * buff,int32_t numBytes)32 int32_t MemInputStream::peek(void *buff, int32_t numBytes) {
33 int32_t numAvail = mBufferLen - mPos;
34 numBytes = std::min(numBytes, numAvail);
35 memcpy(buff, mBuffer + mPos, numBytes);
36 return numBytes;
37 }
38
advance(int32_t numBytes)39 void MemInputStream::advance(int32_t numBytes) {
40 if (numBytes > 0) {
41 int32_t numAvail = mBufferLen - mPos;
42 mPos += std::min(numAvail, numBytes);
43 }
44 }
45
getPos()46 int32_t MemInputStream::getPos() {
47 return mPos;
48 }
49
setPos(int32_t pos)50 void MemInputStream::setPos(int32_t pos) {
51 if (pos > 0) {
52 if (pos < mBufferLen) {
53 mPos = pos;
54 } else {
55 mPos = mBufferLen - 1;
56 }
57 }
58 }
59
60 } // namespace parselib
61