Coverage Report

Created: 2026-06-15 06:22

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