[haiku-commits] Re: haiku: hrev46860 - in src/kits: shared package/hpkg package/hpkg/v1

  • From: Stephan Aßmus <superstippi@xxxxxx>
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Tue, 18 Feb 2014 14:57:23 +0100

Hi guys, mostly Adrien,

I completely fail to see what the problem is.

As far as I understand, you want to use a class which implements BDataOutput in 
a context where you need a BDataIO. As Ingo pointed out, instead of changing 
all the places where BDataOutput was previously used, you could have simply 
written a BDataIO which wraps a BDataOutput. The fact that BDataIO can in 
theory return less bytes from Read() then requested, is completely irrelevant 
in this context, since when the underlying BDataOutput (the deflate output) 
only returns all or nothing, then that's what happens in your wrapper. It 
simply has the behavior of always returning the requested number of bytes. Why 
would that be any sort of problem?

This is how the BDataOutputAdapterIO could look like:

ssize_t
BDataOutputAdapterIO::Read(const void* buffer, size_t size)
{
        status_t error = fWrappedDataOutput->Read(buffer, size);
        if (error == B_OK)
                return size;
        else
                return (ssize_t)error;
}

The other direction would include the loop: Your BDataIOAdapterOutput:

status_t
BDataIOAdapterOutput::Read(const void* buffer, size_t size)
{
        while (size > 0) {
                ssize_t bytesRead = fWrappedDataIO->Read(buffer, size);
                if (bytesRead < 0)
                        return (status_t) bytesRead;
                ASSERT(size >= bytesRead);
                size -= bytesRead;
                buffer += bytesRead;
        }
        return B_OK;
}

With both adapters, you can build mixed chains that contain classes of both 
types.


Best regards,
-Stephan


Other related posts: