modules/libjar/zipwriter/src/StreamFunctions.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/modules/libjar/zipwriter/src/StreamFunctions.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,48 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 + */
     1.8 +
     1.9 +#include "nscore.h"
    1.10 +#include "nsIInputStream.h"
    1.11 +#include "nsIOutputStream.h"
    1.12 +
    1.13 +/*
    1.14 + * Fully reads the required amount of data. Keeps reading until all the
    1.15 + * data is retrieved or an error is hit.
    1.16 + */
    1.17 +NS_HIDDEN_(nsresult) ZW_ReadData(nsIInputStream *aStream, char *aBuffer, uint32_t aCount)
    1.18 +{
    1.19 +    while (aCount > 0) {
    1.20 +        uint32_t read;
    1.21 +        nsresult rv = aStream->Read(aBuffer, aCount, &read);
    1.22 +        NS_ENSURE_SUCCESS(rv, rv);
    1.23 +        aCount -= read;
    1.24 +        aBuffer += read;
    1.25 +        // If we hit EOF before reading the data we need then throw.
    1.26 +        if (read == 0 && aCount > 0)
    1.27 +            return NS_ERROR_FAILURE;
    1.28 +    }
    1.29 +
    1.30 +    return NS_OK;
    1.31 +}
    1.32 +
    1.33 +/*
    1.34 + * Fully writes the required amount of data. Keeps writing until all the
    1.35 + * data is written or an error is hit.
    1.36 + */
    1.37 +NS_HIDDEN_(nsresult) ZW_WriteData(nsIOutputStream *aStream, const char *aBuffer,
    1.38 +                                  uint32_t aCount)
    1.39 +{
    1.40 +    while (aCount > 0) {
    1.41 +        uint32_t written;
    1.42 +        nsresult rv = aStream->Write(aBuffer, aCount, &written);
    1.43 +        NS_ENSURE_SUCCESS(rv, rv);
    1.44 +        if (written <= 0)
    1.45 +            return NS_ERROR_FAILURE;
    1.46 +        aCount -= written;
    1.47 +        aBuffer += written;
    1.48 +    }
    1.49 +
    1.50 +    return NS_OK;
    1.51 +}

mercurial