Repository navigation
Expand file tree
/
Copy pathMemoryOutputStream.cpp
More file actions
156 lines (123 loc) · 4.73 KB
/
Copy pathMemoryOutputStream.cpp
File metadata and controls
156 lines (123 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "MemoryOutputStream.h"
namespace rocketmq {
MemoryOutputStream::MemoryOutputStream(const size_t initialSize)
: blockToUse(&internalBlock), externalData(NULL), position(0), size(0), availableSize(0) {
internalBlock.setSize(initialSize, false);
}
MemoryOutputStream::MemoryOutputStream(MemoryBlock& memoryBlockToWriteTo, const bool appendToExistingBlockContent)
: blockToUse(&memoryBlockToWriteTo), externalData(NULL), position(0), size(0), availableSize(0) {
if (appendToExistingBlockContent)
position = size = memoryBlockToWriteTo.getSize();
}
MemoryOutputStream::MemoryOutputStream(void* destBuffer, size_t destBufferSize)
: blockToUse(NULL), externalData(destBuffer), position(0), size(0), availableSize(destBufferSize) {}
MemoryOutputStream::~MemoryOutputStream() {
trimExternalBlockSize();
}
void MemoryOutputStream::flush() {
trimExternalBlockSize();
}
void MemoryOutputStream::trimExternalBlockSize() {
if (blockToUse != &internalBlock && blockToUse != NULL)
blockToUse->setSize(size, false);
}
void MemoryOutputStream::preallocate(const size_t bytesToPreallocate) {
if (blockToUse != NULL)
blockToUse->ensureSize(bytesToPreallocate + 1);
}
void MemoryOutputStream::reset() {
position = 0;
size = 0;
}
char* MemoryOutputStream::prepareToWrite(size_t numBytes) {
size_t storageNeeded = position + numBytes;
char* data;
if (blockToUse != NULL) {
if (storageNeeded >= (unsigned int)(blockToUse->getSize()))
blockToUse->ensureSize((storageNeeded + std::min(storageNeeded / 2, (size_t)(1024 * 1024)) + 32) & ~31u);
data = static_cast<char*>(blockToUse->getData());
} else {
if (storageNeeded > availableSize)
return NULL;
data = static_cast<char*>(externalData);
}
char* const writePointer = data + position;
position += numBytes;
size = std::max(size, position);
return writePointer;
}
bool MemoryOutputStream::write(const void* const buffer, size_t howMany) {
if (howMany == 0)
return true;
if (char* dest = prepareToWrite(howMany)) {
memcpy(dest, buffer, howMany);
return true;
}
return false;
}
bool MemoryOutputStream::writeRepeatedByte(uint8 byte, size_t howMany) {
if (howMany == 0)
return true;
if (char* dest = prepareToWrite(howMany)) {
memset(dest, byte, howMany);
return true;
}
return false;
}
MemoryBlock MemoryOutputStream::getMemoryBlock() const {
return MemoryBlock(getData(), getDataSize());
}
const void* MemoryOutputStream::getData() const {
if (blockToUse == NULL)
return externalData;
if ((unsigned int)blockToUse->getSize() > size)
static_cast<char*>(blockToUse->getData())[size] = 0;
return blockToUse->getData();
}
bool MemoryOutputStream::setPosition(int64 newPosition) {
if (newPosition <= (int64)size) {
// ok to seek backwards
if (newPosition < 0)
position = 0;
else
position = (int64)size < newPosition ? size : newPosition;
return true;
}
// can't move beyond the end of the stream..
return false;
}
int64 MemoryOutputStream::writeFromInputStream(InputStream& source, int64 maxNumBytesToWrite) {
// before writing from an input, see if we can preallocate to make it more
// efficient..
int64 availableData = source.getTotalLength() - source.getPosition();
if (availableData > 0) {
if (maxNumBytesToWrite > availableData || maxNumBytesToWrite < 0)
maxNumBytesToWrite = availableData;
if (blockToUse != NULL)
preallocate(blockToUse->getSize() + (size_t)maxNumBytesToWrite);
}
return OutputStream::writeFromInputStream(source, maxNumBytesToWrite);
}
OutputStream& operator<<(OutputStream& stream, const MemoryOutputStream& streamToRead) {
const size_t dataSize = streamToRead.getDataSize();
if (dataSize > 0)
stream.write(streamToRead.getData(), dataSize);
return stream;
}
} // namespace rocketmq