LibreOffice Module io (master) 1
streamhelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <limits>
21#include <string.h>
22
23#include <com/sun/star/uno/Sequence.hxx>
24
25#include <com/sun/star/io/BufferSizeExceededException.hpp>
26
27using namespace ::com::sun::star::uno;
28
29#include "streamhelper.hxx"
30
31namespace io_stm {
32
34{
35 writeAt(getSize(), seq);
36}
37
38void MemFIFO::read( Sequence<sal_Int8> &seq , sal_Int32 nBufferLen )
39{
40 readAt(0, seq , nBufferLen);
41 forgetFromStart( nBufferLen );
42}
43
44void MemFIFO::skip( sal_Int32 nBytesToSkip )
45{
46 forgetFromStart( nBytesToSkip );
47}
48
49MemRingBuffer::MemRingBuffer() : m_p(nullptr), m_nBufferLen(0), m_nStart(0), m_nOccupiedBuffer(0)
50{
51}
52
54{
55 std::free( m_p );
56}
57
58void MemRingBuffer::resizeBuffer( sal_Int32 nMinSize )
59{
60 sal_Int32 nNewLen = 1;
61
62 while( nMinSize > nNewLen ) {
63 nNewLen = nNewLen << 1;
64 }
65
66 // buffer never shrinks !
67 if( nNewLen < m_nBufferLen ) {
68 nNewLen = m_nBufferLen;
69 }
70
71 if( nNewLen == m_nBufferLen )
72 return;
73
74 auto p = static_cast<sal_Int8*>(std::realloc(m_p, nNewLen));
75 if (!p)
76 throw css::io::BufferSizeExceededException(
77 "MemRingBuffer::resizeBuffer BufferSizeExceededException");
78
79 m_p = p;
80
81
83 memmove( &( m_p[m_nStart+(nNewLen-m_nBufferLen)]) , &(m_p[m_nStart]) , m_nBufferLen - m_nStart );
84 m_nStart += nNewLen - m_nBufferLen;
85 }
86 m_nBufferLen = nNewLen;
87}
88
89
90void MemRingBuffer::readAt( sal_Int32 nPos, Sequence<sal_Int8> &seq , sal_Int32 nBytesToRead ) const
91{
92 if( nPos + nBytesToRead > m_nOccupiedBuffer ) {
93 throw css::io::BufferSizeExceededException(
94 "MemRingBuffer::readAt BufferSizeExceededException");
95 }
96
97 sal_Int32 nStartReadingPos = nPos + m_nStart;
98 if( nStartReadingPos >= m_nBufferLen ) {
99 nStartReadingPos -= m_nBufferLen;
100 }
101
102 seq.realloc( nBytesToRead );
103
104 if( nStartReadingPos + nBytesToRead > m_nBufferLen ) {
105 sal_Int32 nDeltaLen = m_nBufferLen - nStartReadingPos;
106 memcpy( seq.getArray() , &(m_p[nStartReadingPos]) , nDeltaLen );
107 memcpy( &(seq.getArray()[nDeltaLen]), m_p , nBytesToRead - nDeltaLen );
108 }
109 else {
110 memcpy( seq.getArray() , &(m_p[nStartReadingPos]) , nBytesToRead );
111 }
112}
113
114
115void MemRingBuffer::writeAt( sal_Int32 nPos, const Sequence<sal_Int8> &seq )
116{
118 const sal_Int32 nLen = seq.getLength();
119
120 if( nPos < 0 || nPos > std::numeric_limits< sal_Int32 >::max() - nLen )
121 {
122 throw css::io::BufferSizeExceededException(
123 "MemRingBuffer::writeAt BufferSizeExceededException");
124 }
125
126 if( nPos + nLen - m_nOccupiedBuffer > 0 ) {
127 resizeBuffer( nPos + nLen );
128 m_nOccupiedBuffer = nPos + nLen;
129 }
130
131 sal_Int32 nStartWritingIndex = m_nStart + nPos;
132 if( nStartWritingIndex >= m_nBufferLen ) {
133 nStartWritingIndex -= m_nBufferLen;
134 }
135
136 if( const sal_Int32 nBufferRestLen = m_nBufferLen-nStartWritingIndex; nLen > nBufferRestLen ) {
137 // two area copy
138 memcpy( &(m_p[nStartWritingIndex]) , seq.getConstArray(), nBufferRestLen );
139 memcpy( m_p , &( seq.getConstArray()[nBufferRestLen] ), nLen - nBufferRestLen );
140
141 }
142 else {
143 // one area copy
144 memcpy( &( m_p[nStartWritingIndex]), seq.getConstArray() , nLen );
145 }
147}
148
149
150sal_Int32 MemRingBuffer::getSize() const noexcept
151{
152 return m_nOccupiedBuffer;
153}
154
155void MemRingBuffer::forgetFromStart( sal_Int32 nBytesToForget )
156{
158 if( nBytesToForget > m_nOccupiedBuffer ) {
159 throw css::io::BufferSizeExceededException(
160 "MemRingBuffer::forgetFromStart BufferSizeExceededException");
161 }
162 m_nStart += nBytesToForget;
163 if( m_nStart >= m_nBufferLen ) {
165 }
166 m_nOccupiedBuffer -= nBytesToForget;
168}
169
170
171}
172
173/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void read(Sequence< sal_Int8 > &, sal_Int32 nBytesToRead)
sal_Int32 getSize() const noexcept
void skip(sal_Int32 nBytesToSkip)
void write(const Sequence< sal_Int8 > &)
sal_Int32 getSize() const noexcept
void checkInvariants() const
void forgetFromStart(sal_Int32 nBytesToForget)
void readAt(sal_Int32 nPos, Sequence< sal_Int8 > &, sal_Int32 nBytesToRead) const
void writeAt(sal_Int32 nPos, const Sequence< sal_Int8 > &)
void resizeBuffer(sal_Int32 nMinSize)
void * p
sal_uInt16 nPos
Definition: odata.cxx:47
signed char sal_Int8