/src/logging-log4cxx/src/main/cpp/bytebuffer.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | #include <log4cxx/logstring.h> |
18 | | #include <log4cxx/helpers/bytebuffer.h> |
19 | | #include <log4cxx/helpers/exception.h> |
20 | | #include <log4cxx/helpers/pool.h> |
21 | | |
22 | | using namespace LOG4CXX_NS; |
23 | | using namespace LOG4CXX_NS::helpers; |
24 | | |
25 | | struct ByteBuffer::ByteBufferPriv |
26 | | { |
27 | | ByteBufferPriv(char* data1, size_t capacity) : |
28 | 0 | base(data1), pos(0), lim(capacity), cap(capacity) {} |
29 | | |
30 | | char* base; |
31 | | size_t pos; |
32 | | size_t lim; |
33 | | size_t cap; |
34 | | }; |
35 | | |
36 | | ByteBuffer::ByteBuffer(char* data1, size_t capacity) |
37 | 0 | : m_priv(std::make_unique<ByteBufferPriv>(data1, capacity)) |
38 | 0 | { |
39 | 0 | } |
40 | | |
41 | | ByteBuffer::~ByteBuffer() |
42 | 0 | { |
43 | 0 | } |
44 | | |
45 | | void ByteBuffer::clear() |
46 | 0 | { |
47 | 0 | m_priv->lim = m_priv->cap; |
48 | 0 | m_priv->pos = 0; |
49 | 0 | } |
50 | | |
51 | | void ByteBuffer::flip() |
52 | 0 | { |
53 | 0 | m_priv->lim = m_priv->pos; |
54 | 0 | m_priv->pos = 0; |
55 | 0 | } |
56 | | |
57 | | void ByteBuffer::position(size_t newPosition) |
58 | 0 | { |
59 | 0 | if (newPosition < m_priv->lim) |
60 | 0 | { |
61 | 0 | m_priv->pos = newPosition; |
62 | 0 | } |
63 | 0 | else |
64 | 0 | { |
65 | 0 | m_priv->pos = m_priv->lim; |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | | void ByteBuffer::limit(size_t newLimit) |
70 | 0 | { |
71 | 0 | if (newLimit > m_priv->cap) |
72 | 0 | { |
73 | 0 | throw IllegalArgumentException(LOG4CXX_STR("newLimit")); |
74 | 0 | } |
75 | | |
76 | 0 | m_priv->lim = newLimit; |
77 | 0 | } |
78 | | |
79 | | |
80 | | bool ByteBuffer::put(char byte) |
81 | 0 | { |
82 | 0 | if (m_priv->pos < m_priv->lim) |
83 | 0 | { |
84 | 0 | m_priv->base[m_priv->pos++] = byte; |
85 | 0 | return true; |
86 | 0 | } |
87 | | |
88 | 0 | return false; |
89 | 0 | } |
90 | | |
91 | | char* ByteBuffer::data() |
92 | 0 | { |
93 | 0 | return m_priv->base; |
94 | 0 | } |
95 | | |
96 | | const char* ByteBuffer::data() const |
97 | 0 | { |
98 | 0 | return m_priv->base; |
99 | 0 | } |
100 | | |
101 | | char* ByteBuffer::current() |
102 | 0 | { |
103 | 0 | return m_priv->base + m_priv->pos; |
104 | 0 | } |
105 | | |
106 | | const char* ByteBuffer::current() const |
107 | 0 | { |
108 | 0 | return m_priv->base + m_priv->pos; |
109 | 0 | } |
110 | | |
111 | | size_t ByteBuffer::limit() const |
112 | 0 | { |
113 | 0 | return m_priv->lim; |
114 | 0 | } |
115 | | |
116 | | size_t ByteBuffer::position() const |
117 | 0 | { |
118 | 0 | return m_priv->pos; |
119 | 0 | } |
120 | | |
121 | | size_t ByteBuffer::remaining() const |
122 | 0 | { |
123 | 0 | return m_priv->lim - m_priv->pos; |
124 | 0 | } |
125 | | |