1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is mozilla.org code.
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.
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsByteBuffer.h"
40 #include "nsIInputStream.h"
43 #define MIN_BUFFER_SIZE 32
45 ByteBufferImpl::ByteBufferImpl(void)
46 : mBuffer(NULL
), mSpace(0), mLength(0)
51 ByteBufferImpl::Init(PRUint32 aBufferSize
)
53 if (aBufferSize
< MIN_BUFFER_SIZE
) {
54 aBufferSize
= MIN_BUFFER_SIZE
;
58 mBuffer
= new char[aBufferSize
];
59 return mBuffer
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
62 NS_IMPL_ISUPPORTS1(ByteBufferImpl
,nsIByteBuffer
)
64 ByteBufferImpl::~ByteBufferImpl()
66 if (nsnull
!= mBuffer
) {
74 ByteBufferImpl::Create(nsISupports
*aOuter
, REFNSIID aIID
, void **aResult
)
77 return NS_ERROR_NO_AGGREGATION
;
79 ByteBufferImpl
* it
= new ByteBufferImpl();
81 return NS_ERROR_OUT_OF_MEMORY
;
84 nsresult rv
= it
->QueryInterface(aIID
, (void**)aResult
);
89 NS_IMETHODIMP_(PRUint32
)
90 ByteBufferImpl::GetLength(void) const
95 NS_IMETHODIMP_(PRUint32
)
96 ByteBufferImpl::GetBufferSize(void) const
101 NS_IMETHODIMP_(char*)
102 ByteBufferImpl::GetBuffer(void) const
107 NS_IMETHODIMP_(PRBool
)
108 ByteBufferImpl::Grow(PRUint32 aNewSize
)
110 if (aNewSize
< MIN_BUFFER_SIZE
) {
111 aNewSize
= MIN_BUFFER_SIZE
;
113 char* newbuf
= new char[aNewSize
];
114 if (nsnull
!= newbuf
) {
116 memcpy(newbuf
, mBuffer
, mLength
);
125 NS_IMETHODIMP_(PRInt32
)
126 ByteBufferImpl::Fill(nsresult
* aErrorCode
, nsIInputStream
* aStream
,
129 NS_PRECONDITION(nsnull
!= aStream
, "null stream");
130 NS_PRECONDITION(aKeep
<= mLength
, "illegal keep count");
131 if ((nsnull
== aStream
) || (PRUint32(aKeep
) > PRUint32(mLength
))) {
133 *aErrorCode
= NS_BASE_STREAM_ILLEGAL_ARGS
;
138 // Slide over kept data
139 memmove(mBuffer
, mBuffer
+ (mLength
- aKeep
), aKeep
);
142 // Read in some new data
145 *aErrorCode
= aStream
->Read(mBuffer
+ aKeep
, mSpace
- aKeep
, &nb
);
146 if (NS_SUCCEEDED(*aErrorCode
)) {
154 NS_COM nsresult
NS_NewByteBuffer(nsIByteBuffer
** aInstancePtrResult
,
156 PRUint32 aBufferSize
)
160 rv
= ByteBufferImpl::Create(aOuter
, NS_GET_IID(nsIByteBuffer
), (void**)&buf
);
161 if (NS_FAILED(rv
)) return rv
;
163 rv
= buf
->Init(aBufferSize
);
168 *aInstancePtrResult
= buf
;