Back to index

lightning-sunbird  0.9+nobinonly
nsByteBuffer.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsByteBuffer.h"
00040 #include "nsIInputStream.h"
00041 #include "nsCRT.h"
00042 
00043 #define MIN_BUFFER_SIZE 32
00044 
00045 ByteBufferImpl::ByteBufferImpl(void)
00046   : mBuffer(NULL), mSpace(0), mLength(0)
00047 {
00048 }
00049 
00050 NS_IMETHODIMP
00051 ByteBufferImpl::Init(PRUint32 aBufferSize)
00052 {
00053   if (aBufferSize < MIN_BUFFER_SIZE) {
00054     aBufferSize = MIN_BUFFER_SIZE;
00055   }
00056   mSpace = aBufferSize;
00057   mLength = 0;
00058   mBuffer = new char[aBufferSize];
00059   return mBuffer ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00060 }
00061 
00062 NS_IMPL_ISUPPORTS1(ByteBufferImpl,nsIByteBuffer)
00063 
00064 ByteBufferImpl::~ByteBufferImpl()
00065 {
00066   if (nsnull != mBuffer) {
00067     delete[] mBuffer;
00068     mBuffer = nsnull;
00069   }
00070   mLength = 0;
00071 }
00072 
00073 NS_METHOD
00074 ByteBufferImpl::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00075 {
00076   if (aOuter)
00077     return NS_ERROR_NO_AGGREGATION;
00078 
00079   ByteBufferImpl* it = new ByteBufferImpl();
00080   if (nsnull == it) 
00081     return NS_ERROR_OUT_OF_MEMORY;
00082 
00083   NS_ADDREF(it);
00084   nsresult rv = it->QueryInterface(aIID, (void**)aResult);
00085   NS_RELEASE(it);
00086   return rv;
00087 }
00088 
00089 NS_IMETHODIMP_(PRUint32)
00090 ByteBufferImpl::GetLength(void) const
00091 {
00092   return mLength;
00093 }
00094 
00095 NS_IMETHODIMP_(PRUint32)
00096 ByteBufferImpl::GetBufferSize(void) const
00097 {
00098   return mSpace;
00099 }
00100 
00101 NS_IMETHODIMP_(char*)
00102 ByteBufferImpl::GetBuffer(void) const
00103 {
00104   return mBuffer;
00105 }
00106 
00107 NS_IMETHODIMP_(PRBool)
00108 ByteBufferImpl::Grow(PRUint32 aNewSize)
00109 {
00110   if (aNewSize < MIN_BUFFER_SIZE) {
00111     aNewSize = MIN_BUFFER_SIZE;
00112   }
00113   char* newbuf = new char[aNewSize];
00114   if (nsnull != newbuf) {
00115     if (0 != mLength) {
00116       memcpy(newbuf, mBuffer, mLength);
00117     }
00118     delete[] mBuffer;
00119     mBuffer = newbuf;
00120     return PR_TRUE;
00121   }
00122   return PR_FALSE;
00123 }
00124 
00125 NS_IMETHODIMP_(PRInt32)
00126 ByteBufferImpl::Fill(nsresult* aErrorCode, nsIInputStream* aStream,
00127                      PRUint32 aKeep)
00128 {
00129   NS_PRECONDITION(nsnull != aStream, "null stream");
00130   NS_PRECONDITION(aKeep <= mLength, "illegal keep count");
00131   if ((nsnull == aStream) || (PRUint32(aKeep) > PRUint32(mLength))) {
00132     // whoops
00133     *aErrorCode = NS_BASE_STREAM_ILLEGAL_ARGS;
00134     return -1;
00135   }
00136 
00137   if (0 != aKeep) {
00138     // Slide over kept data
00139     memmove(mBuffer, mBuffer + (mLength - aKeep), aKeep);
00140   }
00141 
00142   // Read in some new data
00143   mLength = aKeep;
00144   PRUint32 nb;
00145   *aErrorCode = aStream->Read(mBuffer + aKeep, mSpace - aKeep, &nb);
00146   if (NS_SUCCEEDED(*aErrorCode)) {
00147     mLength += nb;
00148   }
00149   else
00150     nb = 0;
00151   return nb;
00152 }
00153 
00154 NS_COM nsresult NS_NewByteBuffer(nsIByteBuffer** aInstancePtrResult,
00155                                   nsISupports* aOuter,
00156                                   PRUint32 aBufferSize)
00157 {
00158   nsresult rv;
00159   nsIByteBuffer* buf;
00160   rv = ByteBufferImpl::Create(aOuter, NS_GET_IID(nsIByteBuffer), (void**)&buf);
00161   if (NS_FAILED(rv)) return rv;
00162     
00163   rv = buf->Init(aBufferSize);
00164   if (NS_FAILED(rv)) {
00165     NS_RELEASE(buf);
00166     return rv;
00167   }
00168   *aInstancePtrResult = buf;
00169   return rv;
00170 }