Where to test if buffer contains image or xml?

asked 2014-08-07 08:41:53 -0600

thdrksdfthmn gravatar image

I get a buffer from a function. Normally it is an image buffer, but if the image has been deleted meanwhile, it is a buffer that contains a .xml. I am verifying if the buffer is a .xml and then I throw an exception that will be handled. Right now I am doing something like this:

std::vector<uchar> imgBuf;
imgBuf = s3Service.getObject(bucketNameIn, objectNameIn);

if (imgBuf.empty())
{
    throw EmptyImageBufferException(bucketNameIn, objectNameIn);
}
imgOut = cv::imdecode(imgBuf, CV_LOAD_IMAGE_COLOR);
if (imgOut.empty())
{
    std::string xmlStart("<?xml");
    std::string bufStart(imgBuf.begin(), imgBuf.begin() + 5);
    if (bufStart == xmlStart)
    {
        throw ImageNotAvailableException(bucketNameIn, objectNameIn);
    }
    throw ImageDecodeException(bucketNameIn, objectNameIn);
}

I am not sure how is better: to verify if the buffer is a .xml before imdecode or after?

std::vector<uchar> imgBuf;
imgBuf = s3Service.getObject(bucketNameIn, objectNameIn);

if (imgBuf.empty())
{
    throw EmptyImageBufferException(bucketNameIn, objectNameIn);
}
std::string xmlStart("<?xml");
std::string bufStart(imgBuf.begin(), imgBuf.begin() + 5);
if (bufStart == xmlStart)
{
    throw ImageNotAvailableException(bucketNameIn, objectNameIn);
}
imgOut = cv::imdecode(imgBuf, CV_LOAD_IMAGE_COLOR);
if (imgOut.empty())
{
    throw ImageDecodeException(bucketNameIn, objectNameIn);
}

Which one is faster? I have chosen the first version because normally the .xml is not as often as the image case.

edit retag flag offensive close merge delete