Resize buffer if BytesRead is less than MaxSegmentSize
This commit is contained in:
parent
29c3265622
commit
450e9e7d34
@ -74,7 +74,7 @@
|
|||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#define SYSERROR() GetLastError()
|
#define SYSERROR() GetLastError()
|
||||||
#else
|
#else
|
||||||
#include <errno.h>
|
#include <cerrno>
|
||||||
#define SYSERROR() errno
|
#define SYSERROR() errno
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -180,6 +180,8 @@ FB_UDR_END_FUNCTION
|
|||||||
* external name 'MyFirstUDRKit!MFKLoadBlobFromFile!Load file and save to Blob'
|
* external name 'MyFirstUDRKit!MFKLoadBlobFromFile!Load file and save to Blob'
|
||||||
* engine udr;
|
* engine udr;
|
||||||
***/
|
***/
|
||||||
|
// NOTE This function would be better as a procedure as that would allow it
|
||||||
|
// to return the bytes written to the blob.
|
||||||
FB_UDR_BEGIN_FUNCTION (MFK_LoadBlobFromFile)
|
FB_UDR_BEGIN_FUNCTION (MFK_LoadBlobFromFile)
|
||||||
//BEGIN
|
//BEGIN
|
||||||
FB_UDR_MESSAGE(InMessage,
|
FB_UDR_MESSAGE(InMessage,
|
||||||
@ -205,8 +207,9 @@ FB_UDR_EXECUTE_FUNCTION
|
|||||||
|
|
||||||
|
|
||||||
std::ifstream FileReader;
|
std::ifstream FileReader;
|
||||||
FileReader.open( in->afilename.str , std::ifstream::binary );
|
FileReader.open( in->afilename.str , std::ifstream::binary | std::ifstream::in );
|
||||||
if (! FileReader.is_open()) {
|
if (! FileReader.is_open()) {
|
||||||
|
// if (! FileReader.good()) {
|
||||||
out->ablobNull = FB_TRUE;
|
out->ablobNull = FB_TRUE;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -238,7 +241,7 @@ FB_UDR_EXECUTE_FUNCTION
|
|||||||
// What do we do with the partially written blob? Is cancel enough?
|
// What do we do with the partially written blob? Is cancel enough?
|
||||||
blob->cancel( status );
|
blob->cancel( status );
|
||||||
}
|
}
|
||||||
// ****** IT IS VITAL TO SET THIS IF WRITING TO BLOB SUCCEEDED ********
|
// ****** IT IS VITAL TO SET THIS IF WRITING TO BLOB WAS SUCCESSFUL ******* //
|
||||||
out->ablobNull = FB_FALSE;
|
out->ablobNull = FB_FALSE;
|
||||||
|
|
||||||
blob->close( status );
|
blob->close( status );
|
||||||
@ -298,21 +301,32 @@ FB_UDR_EXECUTE_FUNCTION
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* DOC NOTE - We do not test for existence of the file here!
|
* DOC NOTE - We do not test for existence of the file here!
|
||||||
|
* This should be done prior to calling this function.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
std::ofstream FileWriter ( in->afilename.str , std::ofstream::binary );
|
std::ofstream FileWriter ( in->afilename.str , std::ofstream::binary | std::ofstream::out );
|
||||||
if (! FileWriter.is_open()) {
|
if (! FileWriter.is_open()) {
|
||||||
|
// if (! FileWriter.good()) {
|
||||||
out->resultNull = FB_TRUE;
|
out->resultNull = FB_TRUE;
|
||||||
|
|
||||||
// Convert system error to a negative value and assign to result
|
// Convert system error to a negative value and assign to result
|
||||||
out->result = ( SYSERROR() * -1 );
|
out->result = ( SYSERROR() * -1 );
|
||||||
// important to set this to false here otherwise the error code will
|
|
||||||
// be hidden and NULL returned to the firebird engine. This is because
|
// important to set out->resultNull to false here otherwise the error
|
||||||
// the test for out->resultNull has precedence.
|
// code willbe hidden and NULL returned to the firebird engine. This
|
||||||
|
// is because the test for out->resultNull has precedence.
|
||||||
if ( out->result < 0 ) {
|
if ( out->result < 0 ) {
|
||||||
out->resultNull = FB_FALSE;
|
out->resultNull = FB_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This might be useful but doesn't work because it requires libc++ and we compile with libstdc++ on linux
|
||||||
|
// else {
|
||||||
|
// std::filesystem::permissions ( in->afilename.str, std::filesystem::perms::owner_all | std::filesystem::perms::group_all,
|
||||||
|
// std::filesystem::perm_options::add );
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
att.reset(context->getAttachment(status));
|
att.reset(context->getAttachment(status));
|
||||||
@ -328,6 +342,7 @@ FB_UDR_EXECUTE_FUNCTION
|
|||||||
|
|
||||||
std::vector<std::uint8_t> Buffer (MaxSegmentSize, 0);
|
std::vector<std::uint8_t> Buffer (MaxSegmentSize, 0);
|
||||||
unsigned BytesRead = 0;
|
unsigned BytesRead = 0;
|
||||||
|
unsigned BufferSize = 0;
|
||||||
|
|
||||||
for (bool Eof = false; !Eof; )
|
for (bool Eof = false; !Eof; )
|
||||||
{
|
{
|
||||||
@ -336,6 +351,13 @@ FB_UDR_EXECUTE_FUNCTION
|
|||||||
case IStatus::RESULT_OK:
|
case IStatus::RESULT_OK:
|
||||||
case IStatus::RESULT_SEGMENT:
|
case IStatus::RESULT_SEGMENT:
|
||||||
{
|
{
|
||||||
|
BufferSize = Buffer.size();
|
||||||
|
if ( BufferSize < BytesRead) {
|
||||||
|
BytesRead = BufferSize;
|
||||||
|
}
|
||||||
|
if ( BytesRead < MaxSegmentSize) {
|
||||||
|
Buffer.resize( BytesRead);
|
||||||
|
}
|
||||||
FileWriter.write( (char *) Buffer.data(), Buffer.size() );
|
FileWriter.write( (char *) Buffer.data(), Buffer.size() );
|
||||||
out->result += BytesRead;
|
out->result += BytesRead;
|
||||||
continue;
|
continue;
|
||||||
|
Loading…
Reference in New Issue
Block a user