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 while (aCount > 0) {
16 uint32_t read;
17 nsresult rv = aStream->Read(aBuffer, aCount, &read);
18 NS_ENSURE_SUCCESS(rv, rv);
19 aCount -= read;
20 aBuffer += read;
21 // If we hit EOF before reading the data we need then throw.
22 if (read == 0 && aCount > 0) return NS_ERROR_FAILURE;
23 }
24
25 return NS_OK;
26 }
27
28 /*
29 * Fully writes the required amount of data. Keeps writing until all the
30 * data is written or an error is hit.
31 */
ZW_WriteData(nsIOutputStream * aStream,const char * aBuffer,uint32_t aCount)32 nsresult ZW_WriteData(nsIOutputStream *aStream, const char *aBuffer,
33 uint32_t aCount) {
34 while (aCount > 0) {
35 uint32_t written;
36 nsresult rv = aStream->Write(aBuffer, aCount, &written);
37 NS_ENSURE_SUCCESS(rv, rv);
38 if (written <= 0) return NS_ERROR_FAILURE;
39 aCount -= written;
40 aBuffer += written;
41 }
42
43 return NS_OK;
44 }
45