/src/logging-log4cxx/src/main/cpp/bytebuffer.cpp
Line | Count | Source |
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 | | #include <cstring> |
22 | | |
23 | | using namespace LOG4CXX_NS; |
24 | | using namespace LOG4CXX_NS::helpers; |
25 | | |
26 | | struct ByteBuffer::ByteBufferPriv |
27 | | { |
28 | | ByteBufferPriv(char* data1, size_t capacity) : |
29 | 4.09k | base(data1), pos(0), lim(capacity), cap(capacity) {} |
30 | | |
31 | | char* base; |
32 | | size_t pos; |
33 | | size_t lim; |
34 | | size_t cap; |
35 | | }; |
36 | | |
37 | | ByteBuffer::ByteBuffer(char* data1, size_t capacity) |
38 | 4.09k | : m_priv(std::make_unique<ByteBufferPriv>(data1, capacity)) |
39 | 4.09k | { |
40 | 4.09k | } |
41 | | |
42 | | ByteBuffer::~ByteBuffer() |
43 | 4.09k | { |
44 | 4.09k | } |
45 | | |
46 | | void ByteBuffer::clear() |
47 | 0 | { |
48 | 0 | m_priv->lim = m_priv->cap; |
49 | 0 | m_priv->pos = 0; |
50 | 0 | } |
51 | | |
52 | | void ByteBuffer::carry() |
53 | 0 | { |
54 | 0 | auto available = remaining(); |
55 | 0 | memmove(m_priv->base, current(), available); |
56 | 0 | m_priv->lim = m_priv->cap; |
57 | 0 | m_priv->pos = available; |
58 | 0 | } |
59 | | |
60 | | void ByteBuffer::flip() |
61 | 0 | { |
62 | 0 | m_priv->lim = m_priv->pos; |
63 | 0 | m_priv->pos = 0; |
64 | 0 | } |
65 | | |
66 | | #if LOG4CXX_ABI_VERSION <= 15 |
67 | | void ByteBuffer::position(size_t newPosition) |
68 | 0 | { |
69 | 0 | if (newPosition < m_priv->lim) |
70 | 0 | { |
71 | 0 | m_priv->pos = newPosition; |
72 | 0 | } |
73 | 0 | else |
74 | 0 | { |
75 | 0 | m_priv->pos = m_priv->lim; |
76 | 0 | } |
77 | 0 | } |
78 | | |
79 | | void ByteBuffer::limit(size_t newLimit) |
80 | 0 | { |
81 | 0 | if (newLimit > m_priv->cap) |
82 | 0 | { |
83 | 0 | throw IllegalArgumentException(LOG4CXX_STR("newLimit")); |
84 | 0 | } |
85 | | |
86 | 0 | m_priv->lim = newLimit; |
87 | |
|
88 | 0 | if (m_priv->pos > m_priv->lim) |
89 | 0 | { |
90 | 0 | m_priv->pos = m_priv->lim; |
91 | 0 | } |
92 | 0 | } |
93 | | #endif |
94 | | |
95 | | bool ByteBuffer::put(char byte) |
96 | 0 | { |
97 | 0 | if (m_priv->pos < m_priv->lim) |
98 | 0 | { |
99 | 0 | m_priv->base[m_priv->pos++] = byte; |
100 | 0 | return true; |
101 | 0 | } |
102 | | |
103 | 0 | return false; |
104 | 0 | } |
105 | | |
106 | | char* ByteBuffer::data() |
107 | 0 | { |
108 | 0 | return m_priv->base; |
109 | 0 | } |
110 | | |
111 | | const char* ByteBuffer::data() const |
112 | 0 | { |
113 | 0 | return m_priv->base; |
114 | 0 | } |
115 | | |
116 | | char* ByteBuffer::current() |
117 | 752k | { |
118 | 752k | return m_priv->base + m_priv->pos; |
119 | 752k | } |
120 | | |
121 | | const char* ByteBuffer::current() const |
122 | 0 | { |
123 | 0 | return m_priv->base + m_priv->pos; |
124 | 0 | } |
125 | | |
126 | | size_t ByteBuffer::limit() const |
127 | 0 | { |
128 | 0 | return m_priv->lim; |
129 | 0 | } |
130 | | |
131 | | size_t ByteBuffer::position() const |
132 | 0 | { |
133 | 0 | return m_priv->pos; |
134 | 0 | } |
135 | | |
136 | | size_t ByteBuffer::remaining() const |
137 | 4.50M | { |
138 | 4.50M | return m_priv->lim - m_priv->pos; |
139 | 4.50M | } |
140 | | |
141 | | size_t ByteBuffer::increment_position(size_t byteCount) |
142 | 1.49M | { |
143 | 1.49M | auto available = remaining(); |
144 | 1.49M | m_priv->pos += byteCount < available ? byteCount : available; |
145 | 1.49M | return remaining(); |
146 | 1.49M | } |