1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4  */
5 
6 #include "nscore.h"
7 #include "nsIInputStream.h"
8 #include "nsIOutputStream.h"
9 
10 /*
11  * Fully reads the required amount of data. Keeps reading until all the
12  * data is retrieved or an error is hit.
13  */
ZW_ReadData(nsIInputStream * aStream,char * aBuffer,uint32_t aCount)14 nsresult ZW_ReadData(nsIInputStream *aStream, char *aBuffer, uint32_t aCount)
15 {
16     while (aCount > 0) {
17         uint32_t read;
18         nsresult rv = aStream->Read(aBuffer, aCount, &read);
19         NS_ENSURE_SUCCESS(rv, rv);
20         aCount -= read;
21         aBuffer += read;
22         // If we hit EOF before reading the data we need then throw.
23         if (read == 0 && aCount > 0)
24             return NS_ERROR_FAILURE;
25     }
26 
27     return NS_OK;
28 }
29 
30 /*
31  * Fully writes the required amount of data. Keeps writing until all the
32  * data is written or an error is hit.
33  */
ZW_WriteData(nsIOutputStream * aStream,const char * aBuffer,uint32_t aCount)34 nsresult ZW_WriteData(nsIOutputStream *aStream, const char *aBuffer,
35                                   uint32_t aCount)
36 {
37     while (aCount > 0) {
38         uint32_t written;
39         nsresult rv = aStream->Write(aBuffer, aCount, &written);
40         NS_ENSURE_SUCCESS(rv, rv);
41         if (written <= 0)
42             return NS_ERROR_FAILURE;
43         aCount -= written;
44         aBuffer += written;
45     }
46 
47     return NS_OK;
48 }
49