mirror of
https://github.com/oatpp/oatpp.git
synced 2025-04-18 19:00:23 +08:00
data: introduce data resources.
This commit is contained in:
parent
bc3d17750d
commit
b5ef1ba022
@ -100,13 +100,18 @@ add_library(oatpp
|
||||
oatpp/core/data/mapping/type/UnorderedSet.hpp
|
||||
oatpp/core/data/mapping/type/Vector.cpp
|
||||
oatpp/core/data/mapping/type/Vector.hpp
|
||||
oatpp/core/data/resource/File.cpp
|
||||
oatpp/core/data/resource/File.hpp
|
||||
oatpp/core/data/resource/InMemoryData.cpp
|
||||
oatpp/core/data/resource/InMemoryData.hpp
|
||||
oatpp/core/data/resource/Resource.hpp
|
||||
oatpp/core/data/resource/TemporaryFile.cpp
|
||||
oatpp/core/data/resource/TemporaryFile.hpp
|
||||
oatpp/core/data/share/LazyStringMap.hpp
|
||||
oatpp/core/data/share/MemoryLabel.cpp
|
||||
oatpp/core/data/share/MemoryLabel.hpp
|
||||
oatpp/core/data/share/StringTemplate.cpp
|
||||
oatpp/core/data/share/StringTemplate.hpp
|
||||
oatpp/core/data/share/TemporaryFile.cpp
|
||||
oatpp/core/data/share/TemporaryFile.hpp
|
||||
oatpp/core/data/stream/BufferStream.cpp
|
||||
oatpp/core/data/stream/BufferStream.hpp
|
||||
oatpp/core/data/stream/ChunkedBuffer.cpp
|
||||
|
79
src/oatpp/core/data/resource/File.cpp
Normal file
79
src/oatpp/core/data/resource/File.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
|
||||
*
|
||||
* Licensed 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 "File.hpp"
|
||||
|
||||
#include "oatpp/core/data/stream/FileStream.hpp"
|
||||
|
||||
namespace oatpp { namespace data { namespace resource {
|
||||
|
||||
oatpp::String File::concatDirAndName(const oatpp::String& dir, const oatpp::String& filename) {
|
||||
if(dir && dir->size() > 0) {
|
||||
auto lastChar = dir->data()[dir->size() - 1];
|
||||
if(lastChar != '/' && lastChar != '\\') {
|
||||
return dir + "/" + filename;
|
||||
}
|
||||
return dir + filename;
|
||||
}
|
||||
return filename;
|
||||
}
|
||||
|
||||
File::File(const oatpp::String& fullFileName)
|
||||
: m_handle(std::make_shared<FileHandle>(fullFileName))
|
||||
{}
|
||||
|
||||
File::File(const oatpp::String& tmpDirectory, const oatpp::String& tmpFileName)
|
||||
: m_handle(std::make_shared<FileHandle>(concatDirAndName(tmpDirectory, tmpFileName)))
|
||||
{}
|
||||
|
||||
std::shared_ptr<data::stream::OutputStream> File::openOutputStream() {
|
||||
if(m_handle) {
|
||||
return std::make_shared<data::stream::FileOutputStream>(m_handle->fileName->c_str(), "wb", m_handle);
|
||||
}
|
||||
throw std::runtime_error("[oatpp::data::resource::File::openOutputStream()]: Error. FileHandle is NOT initialized.");
|
||||
}
|
||||
|
||||
std::shared_ptr<data::stream::InputStream> File::openInputStream() {
|
||||
if(m_handle) {
|
||||
return std::make_shared<data::stream::FileInputStream>(m_handle->fileName->c_str(), m_handle);
|
||||
}
|
||||
throw std::runtime_error("[oatpp::data::resource::File::openInputStream()]: Error. FileHandle is NOT initialized.");
|
||||
}
|
||||
|
||||
oatpp::String File::getInMemoryData() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
v_int64 File::getKnownSize() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
oatpp::String File::getLocation() {
|
||||
if(m_handle) {
|
||||
return m_handle->fileName;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
}}}
|
110
src/oatpp/core/data/resource/File.hpp
Normal file
110
src/oatpp/core/data/resource/File.hpp
Normal file
@ -0,0 +1,110 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
|
||||
*
|
||||
* Licensed 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.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef oatpp_data_resource_File_hpp
|
||||
#define oatpp_data_resource_File_hpp
|
||||
|
||||
#include "./Resource.hpp"
|
||||
#include "oatpp/core/data/stream/Stream.hpp"
|
||||
|
||||
namespace oatpp { namespace data { namespace resource {
|
||||
|
||||
/**
|
||||
* File.
|
||||
* @extends - &id:oatpp::data::Resource;.
|
||||
*/
|
||||
class File : public Resource {
|
||||
private:
|
||||
|
||||
struct FileHandle {
|
||||
|
||||
oatpp::String fileName;
|
||||
|
||||
FileHandle(const oatpp::String& fullFileName)
|
||||
: fileName(fullFileName)
|
||||
{}
|
||||
|
||||
};
|
||||
|
||||
public:
|
||||
static oatpp::String concatDirAndName(const oatpp::String& dir, const oatpp::String& filename);
|
||||
private:
|
||||
std::shared_ptr<FileHandle> m_handle;
|
||||
public:
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
File() = default;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param fullFilename
|
||||
*/
|
||||
File(const oatpp::String& fullFilename);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param directory
|
||||
* @param filename
|
||||
*/
|
||||
File(const oatpp::String& directory, const oatpp::String& filename);
|
||||
|
||||
/**
|
||||
* Open output stream to a file. <br>
|
||||
* *Note: stream also captures file-handle. The file object won't be deleted until the stream is deleted.*
|
||||
* @return - `std::shared_ptr` to &id:oatpp::data::stream::OutputStream;.
|
||||
*/
|
||||
std::shared_ptr<data::stream::OutputStream> openOutputStream() override;
|
||||
|
||||
/**
|
||||
* Open input stream to a temporary file. <br>
|
||||
* *Note: stream also captures file-handle. The file won't be deleted until the stream is deleted.*
|
||||
* @return - `std::shared_ptr` &id:oatpp::data::stream::InputStream;.
|
||||
*/
|
||||
std::shared_ptr<data::stream::InputStream> openInputStream() override;
|
||||
|
||||
/**
|
||||
* Not applicable.
|
||||
* @return - always returns `nullptr`.
|
||||
*/
|
||||
oatpp::String getInMemoryData() override;
|
||||
|
||||
/**
|
||||
* Not applicable.
|
||||
* @return - always returns `-1`.
|
||||
*/
|
||||
v_int64 getKnownSize() override;
|
||||
|
||||
/**
|
||||
* Get location where temporary data is stored.
|
||||
* @return - `&id:oatpp::String;`.
|
||||
*/
|
||||
oatpp::String getLocation() override;
|
||||
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#endif //oatpp_data_resource_File_hpp
|
76
src/oatpp/core/data/resource/InMemoryData.cpp
Normal file
76
src/oatpp/core/data/resource/InMemoryData.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
|
||||
*
|
||||
* Licensed 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 "InMemoryData.hpp"
|
||||
|
||||
namespace oatpp { namespace data { namespace resource {
|
||||
|
||||
InMemoryData::OutputDataHandle::~OutputDataHandle() {
|
||||
if(data != nullptr) {
|
||||
dataHandle->data = data + stream->toString();
|
||||
} else {
|
||||
dataHandle->data = stream->toString();
|
||||
}
|
||||
}
|
||||
|
||||
InMemoryData::InMemoryData(const oatpp::String& data)
|
||||
: m_handle(std::make_shared<DataHandle>(data))
|
||||
{}
|
||||
|
||||
std::shared_ptr<data::stream::OutputStream> InMemoryData::openOutputStream() {
|
||||
auto outputDataHandle = std::make_shared<OutputDataHandle>();
|
||||
if(!m_handle) {
|
||||
m_handle = std::make_shared<DataHandle>(nullptr);
|
||||
}
|
||||
outputDataHandle->dataHandle = m_handle;
|
||||
outputDataHandle->data = m_handle->data;
|
||||
|
||||
auto stream = std::make_shared<data::stream::BufferOutputStream>(1024, outputDataHandle);
|
||||
outputDataHandle->stream = stream.get();
|
||||
return stream;
|
||||
}
|
||||
|
||||
std::shared_ptr<data::stream::InputStream> InMemoryData::openInputStream() {
|
||||
return std::make_shared<data::stream::BufferInputStream>(m_handle->data, m_handle);
|
||||
}
|
||||
|
||||
oatpp::String InMemoryData::getInMemoryData() {
|
||||
if(m_handle && m_handle->data) {
|
||||
return m_handle->data;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
v_int64 InMemoryData::getKnownSize() {
|
||||
if(m_handle && m_handle->data) {
|
||||
return m_handle->data->size();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
oatpp::String InMemoryData::getLocation() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
}}}
|
111
src/oatpp/core/data/resource/InMemoryData.hpp
Normal file
111
src/oatpp/core/data/resource/InMemoryData.hpp
Normal file
@ -0,0 +1,111 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
|
||||
*
|
||||
* Licensed 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.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef oatpp_data_resource_InMemoryData_hpp
|
||||
#define oatpp_data_resource_InMemoryData_hpp
|
||||
|
||||
#include "./Resource.hpp"
|
||||
#include "oatpp/core/data/stream/BufferStream.hpp"
|
||||
|
||||
namespace oatpp { namespace data { namespace resource {
|
||||
|
||||
class InMemoryData : public Resource {
|
||||
private:
|
||||
|
||||
struct DataHandle {
|
||||
|
||||
oatpp::String data;
|
||||
|
||||
DataHandle(const oatpp::String& pData)
|
||||
: data(pData)
|
||||
{}
|
||||
|
||||
};
|
||||
|
||||
struct OutputDataHandle {
|
||||
|
||||
std::shared_ptr<DataHandle> dataHandle;
|
||||
oatpp::String data;
|
||||
data::stream::BufferOutputStream* stream;
|
||||
|
||||
~OutputDataHandle();
|
||||
|
||||
};
|
||||
|
||||
private:
|
||||
std::shared_ptr<DataHandle> m_handle;
|
||||
public:
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
InMemoryData() = default;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param fullInMemoryDataname
|
||||
*/
|
||||
InMemoryData(const oatpp::String& data);
|
||||
|
||||
/**
|
||||
* Open output stream to an InMemoryData. <br>
|
||||
* NOT thread-safe. <br>
|
||||
* *Note: data is committed once stream is closed.* <br>
|
||||
* *Note: stream also captures data-handle. The data won't be deleted until the stream is deleted.*
|
||||
* @return - `std::shared_ptr` to &id:oatpp::data::stream::OutputStream;.
|
||||
*/
|
||||
std::shared_ptr<data::stream::OutputStream> openOutputStream() override;
|
||||
|
||||
/**
|
||||
* Open input stream to an InMemoryData. <br>
|
||||
* NOT thread-safe. <br>
|
||||
* *Note: once the stream is open no subsequent writes through the output stream won't affect currently opened input streams.* <br>
|
||||
* *Note: stream also captures file-handle. The data won't be deleted until the stream is deleted.*
|
||||
* @return - `std::shared_ptr` &id:oatpp::data::stream::InputStream;.
|
||||
*/
|
||||
std::shared_ptr<data::stream::InputStream> openInputStream() override;
|
||||
|
||||
/**
|
||||
* Get in-memory-data.
|
||||
* @return - always returns `nullptr`.
|
||||
*/
|
||||
oatpp::String getInMemoryData() override;
|
||||
|
||||
/**
|
||||
* Get size of an in-memory-data.
|
||||
* @return - size of the data in bytes.
|
||||
*/
|
||||
v_int64 getKnownSize() override;
|
||||
|
||||
/**
|
||||
* Not applicable.
|
||||
* @return - always returns `nullptr`.
|
||||
*/
|
||||
oatpp::String getLocation() override;
|
||||
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#endif //oatpp_data_resource_InMemoryData_hpp
|
78
src/oatpp/core/data/resource/Resource.hpp
Normal file
78
src/oatpp/core/data/resource/Resource.hpp
Normal file
@ -0,0 +1,78 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
|
||||
*
|
||||
* Licensed 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.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef oatpp_data_resource_Resource_hpp
|
||||
#define oatpp_data_resource_Resource_hpp
|
||||
|
||||
#include "oatpp/core/data/stream/Stream.hpp"
|
||||
|
||||
namespace oatpp { namespace data { namespace resource {
|
||||
|
||||
/**
|
||||
* Abstract data resource
|
||||
*/
|
||||
class Resource {
|
||||
public:
|
||||
|
||||
/**
|
||||
* virtual destructor.
|
||||
*/
|
||||
virtual ~Resource() = default;
|
||||
|
||||
/**
|
||||
* Open output stream.
|
||||
* @return
|
||||
*/
|
||||
virtual std::shared_ptr<data::stream::OutputStream> openOutputStream() = 0;
|
||||
|
||||
/**
|
||||
* Open input stream.
|
||||
* @return
|
||||
*/
|
||||
virtual std::shared_ptr<data::stream::InputStream> openInputStream() = 0;
|
||||
|
||||
/**
|
||||
* Get in-memory data if applicable.
|
||||
* @return - `&id:oatpp::String;` or `nullptr` if not applicable.
|
||||
*/
|
||||
virtual oatpp::String getInMemoryData() = 0;
|
||||
|
||||
/**
|
||||
* Get known data size if applicable.
|
||||
* @return - known size of the data. `-1` - if size is unknown.
|
||||
*/
|
||||
virtual v_int64 getKnownSize() = 0;
|
||||
|
||||
/**
|
||||
* Get resource location if applicable. <br>
|
||||
* location can be for example a file name.
|
||||
* @return - `&id:oatpp::String;` or `nullptr` if not applicable.
|
||||
*/
|
||||
virtual oatpp::String getLocation() = 0;
|
||||
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#endif //oatpp_data_resource_Resource_hpp
|
@ -24,11 +24,14 @@
|
||||
|
||||
#include "TemporaryFile.hpp"
|
||||
|
||||
#include "./File.hpp"
|
||||
|
||||
#include "oatpp/core/data/stream/FileStream.hpp"
|
||||
#include "oatpp/core/data/stream/BufferStream.hpp"
|
||||
#include "oatpp/encoding/Hex.hpp"
|
||||
#include "oatpp/core/utils/Random.hpp"
|
||||
|
||||
namespace oatpp { namespace data { namespace share {
|
||||
namespace oatpp { namespace data { namespace resource {
|
||||
|
||||
TemporaryFile::FileHandle::~FileHandle() {
|
||||
if(fileName) {
|
||||
@ -36,17 +39,6 @@ TemporaryFile::FileHandle::~FileHandle() {
|
||||
}
|
||||
}
|
||||
|
||||
oatpp::String TemporaryFile::concatDirAndName(const oatpp::String& dir, const oatpp::String& filename) {
|
||||
if(dir && dir->size() > 0) {
|
||||
auto lastChar = dir->data()[dir->size() - 1];
|
||||
if(lastChar != '/' && lastChar != '\\') {
|
||||
return dir + "/" + filename;
|
||||
}
|
||||
return dir + filename;
|
||||
}
|
||||
return filename;
|
||||
}
|
||||
|
||||
oatpp::String TemporaryFile::constructRandomFilename(const oatpp::String& dir, v_int32 randomWordSizeBytes) {
|
||||
|
||||
std::unique_ptr<v_char8[]> buff(new v_char8[randomWordSizeBytes]);
|
||||
@ -55,7 +47,7 @@ oatpp::String TemporaryFile::constructRandomFilename(const oatpp::String& dir, v
|
||||
encoding::Hex::encode(&s, buff.get(), randomWordSizeBytes, encoding::Hex::ALPHABET_LOWER);
|
||||
s << ".tmp";
|
||||
|
||||
return concatDirAndName(dir, s.toString());
|
||||
return File::concatDirAndName(dir, s.toString());
|
||||
|
||||
}
|
||||
|
||||
@ -64,44 +56,38 @@ TemporaryFile::TemporaryFile(const oatpp::String& tmpDirectory, v_int32 randomWo
|
||||
{}
|
||||
|
||||
TemporaryFile::TemporaryFile(const oatpp::String& tmpDirectory, const oatpp::String& tmpFileName)
|
||||
: m_handle(std::make_shared<FileHandle>(concatDirAndName(tmpDirectory, tmpFileName)))
|
||||
: m_handle(std::make_shared<FileHandle>(File::concatDirAndName(tmpDirectory, tmpFileName)))
|
||||
{}
|
||||
|
||||
oatpp::String TemporaryFile::getFullFileName() {
|
||||
std::shared_ptr<data::stream::OutputStream> TemporaryFile::openOutputStream() {
|
||||
if(m_handle) {
|
||||
return std::make_shared<data::stream::FileOutputStream>(m_handle->fileName->c_str(), "wb", m_handle);
|
||||
}
|
||||
throw std::runtime_error("[oatpp::data::resource::TemporaryFile::openOutputStream()]: Error. FileHandle is NOT initialized.");
|
||||
}
|
||||
|
||||
std::shared_ptr<data::stream::InputStream> TemporaryFile::openInputStream() {
|
||||
if(m_handle) {
|
||||
return std::make_shared<data::stream::FileInputStream>(m_handle->fileName->c_str(), m_handle);
|
||||
}
|
||||
throw std::runtime_error("[oatpp::data::resource::TemporaryFile::openInputStream()]: Error. FileHandle is NOT initialized.");
|
||||
}
|
||||
|
||||
oatpp::String TemporaryFile::getInMemoryData() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
v_int64 TemporaryFile::getKnownSize() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
oatpp::String TemporaryFile::getLocation() {
|
||||
if(m_handle) {
|
||||
return m_handle->fileName;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
data::stream::FileOutputStream TemporaryFile::openOutputStream() {
|
||||
if(m_handle) {
|
||||
return data::stream::FileOutputStream(m_handle->fileName->c_str(), "wb", m_handle);
|
||||
}
|
||||
throw std::runtime_error("[oatpp::data::share::TemporaryFile::openOutputStream()]: Error. FileHandle is NOT initialized.");
|
||||
}
|
||||
|
||||
data::stream::FileInputStream TemporaryFile::openInputStream() {
|
||||
if(m_handle) {
|
||||
return data::stream::FileInputStream(m_handle->fileName->c_str(), m_handle);
|
||||
}
|
||||
throw std::runtime_error("[oatpp::data::share::TemporaryFile::openInputStream()]: Error. FileHandle is NOT initialized.");
|
||||
}
|
||||
|
||||
std::shared_ptr<data::stream::FileOutputStream> TemporaryFile::openOutputStreamShared() {
|
||||
if(m_handle) {
|
||||
return std::make_shared<data::stream::FileOutputStream>(m_handle->fileName->c_str(), "wb", m_handle);
|
||||
}
|
||||
throw std::runtime_error("[oatpp::data::share::TemporaryFile::openOutputStreamShared()]: Error. FileHandle is NOT initialized.");
|
||||
}
|
||||
|
||||
std::shared_ptr<data::stream::FileInputStream> TemporaryFile::openInputStreamShared() {
|
||||
if(m_handle) {
|
||||
return std::make_shared<data::stream::FileInputStream>(m_handle->fileName->c_str(), m_handle);
|
||||
}
|
||||
throw std::runtime_error("[oatpp::data::share::TemporaryFile::openInputStreamShared()]: Error. FileHandle is NOT initialized.");
|
||||
}
|
||||
|
||||
bool TemporaryFile::moveFile(const oatpp::String& fullFileName) {
|
||||
if(m_handle) {
|
||||
return std::rename(m_handle->fileName->c_str(), fullFileName->c_str()) == 0;
|
@ -22,13 +22,13 @@
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef oatpp_data_share_TemporaryFile_hpp
|
||||
#define oatpp_data_share_TemporaryFile_hpp
|
||||
#ifndef oatpp_data_resource_TemporaryFile_hpp
|
||||
#define oatpp_data_resource_TemporaryFile_hpp
|
||||
|
||||
#include "oatpp/core/data/stream/FileStream.hpp"
|
||||
#include "./Resource.hpp"
|
||||
#include "oatpp/core/Types.hpp"
|
||||
|
||||
namespace oatpp { namespace data { namespace share {
|
||||
namespace oatpp { namespace data { namespace resource {
|
||||
|
||||
/**
|
||||
* Temporary file - the file which gets deleted when the destructor is called
|
||||
@ -36,9 +36,10 @@ namespace oatpp { namespace data { namespace share {
|
||||
* The `TemporaryFile` object internally stores a `shared_ptr` to a file handle.
|
||||
* When file handle deleted it also deletes the underlying file. <br>
|
||||
* Thus it's safe to copy `TemporaryFile` object and you may treat `TemporaryFile` object
|
||||
* as a shared_ptr to a temporary file.
|
||||
* as a shared_ptr to a temporary file. <br>
|
||||
* @extends - &id:oatpp::data::Resource;.
|
||||
*/
|
||||
class TemporaryFile {
|
||||
class TemporaryFile : public Resource {
|
||||
private:
|
||||
|
||||
/*
|
||||
@ -57,7 +58,6 @@ private:
|
||||
};
|
||||
|
||||
private:
|
||||
static oatpp::String concatDirAndName(const oatpp::String& dir, const oatpp::String& filename);
|
||||
static oatpp::String constructRandomFilename(const oatpp::String& dir, v_int32 randomWordSizeBytes);
|
||||
private:
|
||||
std::shared_ptr<FileHandle> m_handle;
|
||||
@ -86,39 +86,37 @@ public:
|
||||
*/
|
||||
TemporaryFile(const oatpp::String& tmpDirectory, const oatpp::String& tmpFileName);
|
||||
|
||||
/**
|
||||
* Get full name of a temporary file.
|
||||
* @return
|
||||
*/
|
||||
oatpp::String getFullFileName();
|
||||
|
||||
/**
|
||||
* Open output stream to a temporary file. <br>
|
||||
* *Note: stream also captures file-handle. The temporary file won't be deleted until the stream is deleted.*
|
||||
* @return - &id:oatpp::data::stream::FileOutputStream;.
|
||||
* @return - `std::shared_ptr` to &id:oatpp::data::stream::OutputStream;.
|
||||
*/
|
||||
data::stream::FileOutputStream openOutputStream();
|
||||
std::shared_ptr<data::stream::OutputStream> openOutputStream() override;
|
||||
|
||||
/**
|
||||
* Open input stream to a temporary file.
|
||||
* Open input stream to a temporary file. <br>
|
||||
* *Note: stream also captures file-handle. The temporary file won't be deleted until the stream is deleted.*
|
||||
* @return - &id:oatpp::data::stream::FileInputStream;.
|
||||
* @return - `std::shared_ptr` &id:oatpp::data::stream::InputStream;.
|
||||
*/
|
||||
data::stream::FileInputStream openInputStream();
|
||||
std::shared_ptr<data::stream::InputStream> openInputStream() override;
|
||||
|
||||
/**
|
||||
* Open output stream to a temporary file. <br>
|
||||
* *Note: stream also captures file-handle. The temporary file won't be deleted until the stream is deleted.*
|
||||
* @return - `std::shared_ptr` to &id:oatpp::data::stream::FileOutputStream;.
|
||||
* Not applicable.
|
||||
* @return - always returns `nullptr`.
|
||||
*/
|
||||
std::shared_ptr<data::stream::FileOutputStream> openOutputStreamShared();
|
||||
oatpp::String getInMemoryData() override;
|
||||
|
||||
/**
|
||||
* Open input stream to a temporary file.
|
||||
* *Note: stream also captures file-handle. The temporary file won't be deleted until the stream is deleted.*
|
||||
* @return - `std::shared_ptr` &id:oatpp::data::stream::FileInputStream;.
|
||||
* Not applicable.
|
||||
* @return - always returns `-1`.
|
||||
*/
|
||||
std::shared_ptr<data::stream::FileInputStream> openInputStreamShared();
|
||||
v_int64 getKnownSize() override;
|
||||
|
||||
/**
|
||||
* Get location where temporary data is stored.
|
||||
* @return - `&id:oatpp::String;`.
|
||||
*/
|
||||
oatpp::String getLocation() override;
|
||||
|
||||
/**
|
||||
* Move payload to a different file. <br>
|
||||
@ -131,4 +129,4 @@ public:
|
||||
|
||||
}}}
|
||||
|
||||
#endif //oatpp_data_share_TemporaryFile_hpp
|
||||
#endif //oatpp_data_resource_TemporaryFile_hpp
|
@ -33,15 +33,17 @@ namespace oatpp { namespace data{ namespace stream {
|
||||
|
||||
data::stream::DefaultInitializedContext BufferOutputStream::DEFAULT_CONTEXT(data::stream::StreamType::STREAM_INFINITE);
|
||||
|
||||
BufferOutputStream::BufferOutputStream(v_buff_size initialCapacity)
|
||||
BufferOutputStream::BufferOutputStream(v_buff_size initialCapacity, const std::shared_ptr<void>& captureData)
|
||||
: m_data(new v_char8[initialCapacity])
|
||||
, m_capacity(initialCapacity)
|
||||
, m_position(0)
|
||||
, m_maxCapacity(-1)
|
||||
, m_ioMode(IOMode::ASYNCHRONOUS)
|
||||
, m_capturedData(captureData)
|
||||
{}
|
||||
|
||||
BufferOutputStream::~BufferOutputStream() {
|
||||
m_capturedData.reset(); // reset capture data before deleting data.
|
||||
delete [] m_data;
|
||||
}
|
||||
|
||||
@ -167,23 +169,32 @@ oatpp::async::CoroutineStarter BufferOutputStream::flushToStreamAsync(const std:
|
||||
|
||||
data::stream::DefaultInitializedContext BufferInputStream::DEFAULT_CONTEXT(data::stream::StreamType::STREAM_FINITE);
|
||||
|
||||
BufferInputStream::BufferInputStream(const std::shared_ptr<std::string>& memoryHandle, const void* data, v_buff_size size)
|
||||
BufferInputStream::BufferInputStream(const std::shared_ptr<std::string>& memoryHandle,
|
||||
const void* data,
|
||||
v_buff_size size,
|
||||
const std::shared_ptr<void>& captureData)
|
||||
: m_memoryHandle(memoryHandle)
|
||||
, m_data((p_char8) data)
|
||||
, m_size(size)
|
||||
, m_position(0)
|
||||
, m_ioMode(IOMode::ASYNCHRONOUS)
|
||||
, m_capturedData(captureData)
|
||||
{}
|
||||
|
||||
BufferInputStream::BufferInputStream(const oatpp::String& data)
|
||||
: BufferInputStream(data.getPtr(), (p_char8) data->data(), data->size())
|
||||
BufferInputStream::BufferInputStream(const oatpp::String& data, const std::shared_ptr<void>& captureData)
|
||||
: BufferInputStream(data.getPtr(), (p_char8) data->data(), data->size(), captureData)
|
||||
{}
|
||||
|
||||
void BufferInputStream::reset(const std::shared_ptr<std::string>& memoryHandle, p_char8 data, v_buff_size size) {
|
||||
void BufferInputStream::reset(const std::shared_ptr<std::string>& memoryHandle,
|
||||
p_char8 data,
|
||||
v_buff_size size,
|
||||
const std::shared_ptr<void>& captureData)
|
||||
{
|
||||
m_memoryHandle = memoryHandle;
|
||||
m_data = data;
|
||||
m_size = size;
|
||||
m_position = 0;
|
||||
m_capturedData = captureData;
|
||||
}
|
||||
|
||||
void BufferInputStream::reset() {
|
||||
@ -191,6 +202,7 @@ void BufferInputStream::reset() {
|
||||
m_data = nullptr;
|
||||
m_size = 0;
|
||||
m_position = 0;
|
||||
m_capturedData.reset();
|
||||
}
|
||||
|
||||
v_io_size BufferInputStream::read(void *data, v_buff_size count, async::Action& action) {
|
||||
|
@ -41,13 +41,16 @@ private:
|
||||
v_buff_size m_position;
|
||||
v_buff_size m_maxCapacity;
|
||||
IOMode m_ioMode;
|
||||
private:
|
||||
std::shared_ptr<void> m_capturedData;
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param growBytes
|
||||
* @param captureData - capture auxiliary data to not get deleted until it's done with the stream.
|
||||
*/
|
||||
BufferOutputStream(v_buff_size initialCapacity = 2048);
|
||||
BufferOutputStream(v_buff_size initialCapacity = 2048, const std::shared_ptr<void>& captureData = nullptr);
|
||||
|
||||
/**
|
||||
* Virtual destructor.
|
||||
@ -155,6 +158,8 @@ private:
|
||||
v_buff_size m_size;
|
||||
v_buff_size m_position;
|
||||
IOMode m_ioMode;
|
||||
private:
|
||||
std::shared_ptr<void> m_capturedData;
|
||||
public:
|
||||
|
||||
/**
|
||||
@ -162,22 +167,31 @@ public:
|
||||
* @param memoryHandle - buffer memory handle. May be nullptr.
|
||||
* @param data - pointer to buffer data.
|
||||
* @param size - size of the buffer.
|
||||
* @param captureData - capture auxiliary data to not get deleted until it's done with the stream.
|
||||
*/
|
||||
BufferInputStream(const std::shared_ptr<std::string>& memoryHandle, const void* data, v_buff_size size);
|
||||
BufferInputStream(const std::shared_ptr<std::string>& memoryHandle,
|
||||
const void* data,
|
||||
v_buff_size size,
|
||||
const std::shared_ptr<void>& captureData = nullptr);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param data - buffer.
|
||||
* @param captureData - capture auxiliary data to not get deleted until it's done with the stream.
|
||||
*/
|
||||
BufferInputStream(const oatpp::String& data);
|
||||
BufferInputStream(const oatpp::String& data, const std::shared_ptr<void>& captureData = nullptr);
|
||||
|
||||
/**
|
||||
* Reset stream data and set position to `0`.
|
||||
* @param memoryHandle - buffer memory handle. May be nullptr.
|
||||
* @param data - pointer to buffer data.
|
||||
* @param size - size of the buffer.
|
||||
* @param captureData - capture auxiliary data to not get deleted until it's done with the stream.
|
||||
*/
|
||||
void reset(const std::shared_ptr<std::string>& memoryHandle, p_char8 data, v_buff_size size);
|
||||
void reset(const std::shared_ptr<std::string>& memoryHandle,
|
||||
p_char8 data,
|
||||
v_buff_size size,
|
||||
const std::shared_ptr<void>& captureData = nullptr);
|
||||
|
||||
|
||||
/**
|
||||
|
@ -505,17 +505,6 @@ ConsistentOutputStream& operator << (ConsistentOutputStream& s, T value) {
|
||||
s.writeAsString(value);
|
||||
return s;
|
||||
}
|
||||
//ConsistentOutputStream& operator << (ConsistentOutputStream& s, v_int8 value);
|
||||
//ConsistentOutputStream& operator << (ConsistentOutputStream& s, v_uint8 value);
|
||||
//ConsistentOutputStream& operator << (ConsistentOutputStream& s, v_int16 value);
|
||||
//ConsistentOutputStream& operator << (ConsistentOutputStream& s, v_uint16 value);
|
||||
//ConsistentOutputStream& operator << (ConsistentOutputStream& s, v_int32 value);
|
||||
//ConsistentOutputStream& operator << (ConsistentOutputStream& s, v_uint32 value);
|
||||
//ConsistentOutputStream& operator << (ConsistentOutputStream& s, v_int64 value);
|
||||
//ConsistentOutputStream& operator << (ConsistentOutputStream& s, v_uint64 value);
|
||||
//ConsistentOutputStream& operator << (ConsistentOutputStream& s, v_float32 value);
|
||||
//ConsistentOutputStream& operator << (ConsistentOutputStream& s, v_float64 value);
|
||||
//ConsistentOutputStream& operator << (ConsistentOutputStream& s, bool value);
|
||||
|
||||
/**
|
||||
* Error of Asynchronous stream transfer.
|
||||
@ -552,11 +541,11 @@ public:
|
||||
* @return - the actual amout of bytes read from the `readCallback`.
|
||||
*/
|
||||
v_io_size transfer(const base::ObjectHandle<ReadCallback>& readCallback,
|
||||
const base::ObjectHandle<WriteCallback>& writeCallback,
|
||||
v_io_size transferSize,
|
||||
void* buffer,
|
||||
v_buff_size bufferSize,
|
||||
const base::ObjectHandle<data::buffer::Processor>& processor = &StatelessDataTransferProcessor::INSTANCE);
|
||||
const base::ObjectHandle<WriteCallback>& writeCallback,
|
||||
v_io_size transferSize,
|
||||
void* buffer,
|
||||
v_buff_size bufferSize,
|
||||
const base::ObjectHandle<data::buffer::Processor>& processor = &StatelessDataTransferProcessor::INSTANCE);
|
||||
|
||||
/**
|
||||
* Transfer data from `readCallback` to `writeCallback` in Async manner.
|
||||
|
Loading…
x
Reference in New Issue
Block a user