1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsScriptableInputStream.h"
39 : #include "nsMemory.h"
40 : #include "nsString.h"
41 :
42 48068 : NS_IMPL_ISUPPORTS1(nsScriptableInputStream, nsIScriptableInputStream)
43 :
44 : // nsIScriptableInputStream methods
45 : NS_IMETHODIMP
46 74 : nsScriptableInputStream::Close(void) {
47 74 : if (!mInputStream) return NS_ERROR_NOT_INITIALIZED;
48 74 : return mInputStream->Close();
49 : }
50 :
51 : NS_IMETHODIMP
52 2614 : nsScriptableInputStream::Init(nsIInputStream *aInputStream) {
53 2614 : if (!aInputStream) return NS_ERROR_NULL_POINTER;
54 2614 : mInputStream = aInputStream;
55 2614 : return NS_OK;
56 : }
57 :
58 : NS_IMETHODIMP
59 103 : nsScriptableInputStream::Available(PRUint32 *_retval) {
60 103 : if (!mInputStream) return NS_ERROR_NOT_INITIALIZED;
61 103 : return mInputStream->Available(_retval);
62 : }
63 :
64 : NS_IMETHODIMP
65 1893 : nsScriptableInputStream::Read(PRUint32 aCount, char **_retval) {
66 1893 : nsresult rv = NS_OK;
67 1893 : PRUint32 count = 0;
68 1893 : char *buffer = nsnull;
69 :
70 1893 : if (!mInputStream) return NS_ERROR_NOT_INITIALIZED;
71 :
72 1893 : rv = mInputStream->Available(&count);
73 1893 : if (NS_FAILED(rv)) return rv;
74 :
75 : // bug716556 - Ensure count+1 doesn't overflow
76 1891 : count = NS_MIN(NS_MIN(count, aCount), PR_UINT32_MAX - 1);
77 1891 : buffer = (char*)nsMemory::Alloc(count+1); // make room for '\0'
78 1891 : if (!buffer) return NS_ERROR_OUT_OF_MEMORY;
79 :
80 1891 : PRUint32 amtRead = 0;
81 1891 : rv = mInputStream->Read(buffer, count, &amtRead);
82 1891 : if (NS_FAILED(rv)) {
83 1 : nsMemory::Free(buffer);
84 1 : return rv;
85 : }
86 :
87 1890 : buffer[amtRead] = '\0';
88 1890 : *_retval = buffer;
89 1890 : return NS_OK;
90 : }
91 :
92 : NS_IMETHODIMP
93 774 : nsScriptableInputStream::ReadBytes(PRUint32 aCount, nsACString &_retval) {
94 774 : if (!mInputStream) {
95 0 : return NS_ERROR_NOT_INITIALIZED;
96 : }
97 :
98 774 : _retval.SetLength(aCount);
99 774 : if (_retval.Length() != aCount) {
100 0 : return NS_ERROR_OUT_OF_MEMORY;
101 : }
102 :
103 774 : char *ptr = _retval.BeginWriting();
104 774 : PRUint32 totalBytesRead = 0;
105 1 : while (1) {
106 : PRUint32 bytesRead;
107 775 : nsresult rv = mInputStream->Read(ptr + totalBytesRead,
108 : aCount - totalBytesRead,
109 775 : &bytesRead);
110 775 : if (NS_FAILED(rv)) {
111 1 : return rv;
112 : }
113 :
114 774 : totalBytesRead += bytesRead;
115 774 : if (totalBytesRead == aCount) {
116 : break;
117 : }
118 :
119 : // If we have read zero bytes, we have hit EOF.
120 2 : if (bytesRead == 0) {
121 1 : _retval.Truncate();
122 1 : return NS_ERROR_FAILURE;
123 : }
124 :
125 : }
126 772 : return NS_OK;
127 : }
128 :
129 : nsresult
130 2670 : nsScriptableInputStream::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) {
131 2670 : if (aOuter) return NS_ERROR_NO_AGGREGATION;
132 :
133 2670 : nsScriptableInputStream *sis = new nsScriptableInputStream();
134 2670 : if (!sis) return NS_ERROR_OUT_OF_MEMORY;
135 :
136 2670 : NS_ADDREF(sis);
137 2670 : nsresult rv = sis->QueryInterface(aIID, aResult);
138 2670 : NS_RELEASE(sis);
139 2670 : return rv;
140 : }
|