michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: */ michael@0: michael@0: #include "nscore.h" michael@0: #include "nsIInputStream.h" michael@0: #include "nsIOutputStream.h" michael@0: michael@0: /* michael@0: * Fully reads the required amount of data. Keeps reading until all the michael@0: * data is retrieved or an error is hit. michael@0: */ michael@0: NS_HIDDEN_(nsresult) ZW_ReadData(nsIInputStream *aStream, char *aBuffer, uint32_t aCount) michael@0: { michael@0: while (aCount > 0) { michael@0: uint32_t read; michael@0: nsresult rv = aStream->Read(aBuffer, aCount, &read); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: aCount -= read; michael@0: aBuffer += read; michael@0: // If we hit EOF before reading the data we need then throw. michael@0: if (read == 0 && aCount > 0) michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* michael@0: * Fully writes the required amount of data. Keeps writing until all the michael@0: * data is written or an error is hit. michael@0: */ michael@0: NS_HIDDEN_(nsresult) ZW_WriteData(nsIOutputStream *aStream, const char *aBuffer, michael@0: uint32_t aCount) michael@0: { michael@0: while (aCount > 0) { michael@0: uint32_t written; michael@0: nsresult rv = aStream->Write(aBuffer, aCount, &written); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (written <= 0) michael@0: return NS_ERROR_FAILURE; michael@0: aCount -= written; michael@0: aBuffer += written; michael@0: } michael@0: michael@0: return NS_OK; michael@0: }