summaryrefslogtreecommitdiffstats
path: root/src/Stream.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2019-01-27 13:01:35 +0100
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2019-01-27 15:06:36 +0100
commita2345d1f45870eb00a2a3d5d6fd21a386efcade3 (patch)
tree895dbf8f78d5b0117fb0daaab43423775ac4c304 /src/Stream.cpp
parentMerge pull request #31 from uis246/pluginSystem (diff)
downloadAltCraft-a2345d1f45870eb00a2a3d5d6fd21a386efcade3.tar
AltCraft-a2345d1f45870eb00a2a3d5d6fd21a386efcade3.tar.gz
AltCraft-a2345d1f45870eb00a2a3d5d6fd21a386efcade3.tar.bz2
AltCraft-a2345d1f45870eb00a2a3d5d6fd21a386efcade3.tar.lz
AltCraft-a2345d1f45870eb00a2a3d5d6fd21a386efcade3.tar.xz
AltCraft-a2345d1f45870eb00a2a3d5d6fd21a386efcade3.tar.zst
AltCraft-a2345d1f45870eb00a2a3d5d6fd21a386efcade3.zip
Diffstat (limited to 'src/Stream.cpp')
-rw-r--r--src/Stream.cpp44
1 files changed, 16 insertions, 28 deletions
diff --git a/src/Stream.cpp b/src/Stream.cpp
index d66335e..efe37d2 100644
--- a/src/Stream.cpp
+++ b/src/Stream.cpp
@@ -71,11 +71,10 @@ double StreamInput::ReadDouble() {
std::string StreamInput::ReadString() {
int strLength = ReadVarInt();
- unsigned char *buff = new unsigned char[strLength + 1];
- ReadData(buff, strLength);
+ std::vector<unsigned char> buff(strLength + 1);
+ ReadData(buff.data(), strLength);
buff[strLength] = 0;
- std::string str((char *) buff);
- delete[] buff;
+ std::string str((char *) buff.data());
return str;
}
@@ -178,11 +177,9 @@ Uuid StreamInput::ReadUuid() {
}
std::vector<unsigned char> StreamInput::ReadByteArray(size_t arrLength) {
- unsigned char *buffer = new unsigned char[arrLength];
- ReadData(buffer, arrLength);
- std::vector<unsigned char> ret(buffer, buffer + arrLength);
- delete[] buffer;
- return ret;
+ std::vector<unsigned char> buffer(arrLength);
+ ReadData(buffer.data(), arrLength);
+ return buffer;
}
@@ -307,7 +304,7 @@ void StreamOutput::WriteByteArray(const std::vector<unsigned char> &value) {
}
void StreamBuffer::ReadData(unsigned char *buffPtr, size_t buffLen) {
- size_t bufferLengthLeft = buffer + bufferLength - bufferPtr;
+ size_t bufferLengthLeft = buffer.data() + buffer.size() - bufferPtr;
if (bufferLengthLeft < buffLen)
throw std::runtime_error("Internal error: StreamBuffer reader out of data");
@@ -316,38 +313,29 @@ void StreamBuffer::ReadData(unsigned char *buffPtr, size_t buffLen) {
}
void StreamBuffer::WriteData(unsigned char *buffPtr, size_t buffLen) {
- size_t bufferLengthLeft = buffer + bufferLength - bufferPtr;
+ size_t bufferLengthLeft = buffer.data() + buffer.size() - bufferPtr;
if (bufferLengthLeft < buffLen)
throw std::runtime_error("Internal error: StreamBuffer writer out of data");
std::memcpy(bufferPtr, buffPtr, buffLen);
bufferPtr += buffLen;
}
-StreamBuffer::StreamBuffer(unsigned char *data, size_t dataLen) {
- buffer = new unsigned char[dataLen];
- bufferPtr = buffer;
- bufferLength = dataLen;
- std::memcpy(buffer, data, dataLen);
+StreamBuffer::StreamBuffer(unsigned char *data, size_t dataLen) : buffer(data,data+dataLen) {
+ bufferPtr = buffer.data();
}
-StreamBuffer::StreamBuffer(size_t bufferLen) {
- buffer = new unsigned char[bufferLen];
- bufferPtr = buffer;
- bufferLength = bufferLen;
- for (unsigned char *p = buffer; p != buffer + bufferLength; ++p)
- *p = 0;
-}
-
-StreamBuffer::~StreamBuffer() {
- delete[] buffer;
+StreamBuffer::StreamBuffer(size_t bufferLen) : buffer(bufferLen) {
+ bufferPtr = buffer.data();
+ for (auto &it : buffer)
+ it = 0;
}
std::vector<unsigned char> StreamBuffer::GetBuffer() {
- return std::vector<unsigned char>(buffer, buffer + bufferLength);
+ return buffer;
}
size_t StreamBuffer::GetReadedLength() {
- return bufferPtr - buffer;
+ return bufferPtr - buffer.data();
}
void StreamCounter::WriteData(unsigned char *buffPtr, size_t buffLen) {