LCOV - app.info - xpcom/ds/nsByteBuffer.cpp

LCOV - code coverage report
| Current view: | directory - xpcom/ds - nsByteBuffer.cpp (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 67 | 46 | 68.7 %
| Date: | 2012-04-21 | Functions: | 13 | 11 | 84.6 %

           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
           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                 :  *   Pierre Phaneuf <pp@ludusdesign.com>
          24                 :  *
          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.
          36                 :  *
          37                 :  * ***** END LICENSE BLOCK ***** */
          38                 :
          39                 : #include "nsByteBuffer.h"
          40                 : #include "nsIInputStream.h"
          41                 : #include "nsCRT.h"
          42                 :
          43                 : #define MIN_BUFFER_SIZE 32
          44                 :
          45            2738 : ByteBufferImpl::ByteBufferImpl(void)
          46            2738 :   : mBuffer(NULL), mSpace(0), mLength(0)
          47                 : {
          48            2738 : }
          49                 :
          50                 : NS_IMETHODIMP
          51            2738 : ByteBufferImpl::Init(PRUint32 aBufferSize)
          52                 : {
          53            2738 :   if (aBufferSize < MIN_BUFFER_SIZE) {
          54              24 :     aBufferSize = MIN_BUFFER_SIZE;
          55                 :   }
          56            2738 :   mSpace = aBufferSize;
          57            2738 :   mLength = 0;
          58            5476 :   mBuffer = new char[aBufferSize];
          59            2738 :   return mBuffer ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
          60                 : }
          61                 :
          62           24642 : NS_IMPL_ISUPPORTS1(ByteBufferImpl,nsIByteBuffer)
          63                 :
          64            2738 : ByteBufferImpl::~ByteBufferImpl()
          65                 : {
          66            2738 :   if (nsnull != mBuffer) {
          67            2738 :     delete[] mBuffer;
          68            2738 :     mBuffer = nsnull;
          69                 :   }
          70            2738 :   mLength = 0;
          71            2738 : }
          72                 :
          73                 : nsresult
          74            2738 : ByteBufferImpl::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
          75                 : {
          76            2738 :   if (aOuter)
          77               0 :     return NS_ERROR_NO_AGGREGATION;
          78                 :
          79            2738 :   ByteBufferImpl* it = new ByteBufferImpl();
          80            2738 :   if (nsnull == it)
          81               0 :     return NS_ERROR_OUT_OF_MEMORY;
          82                 :
          83            2738 :   NS_ADDREF(it);
          84            2738 :   nsresult rv = it->QueryInterface(aIID, (void**)aResult);
          85            2738 :   NS_RELEASE(it);
          86            2738 :   return rv;
          87                 : }
          88                 :
          89                 : NS_IMETHODIMP_(PRUint32)
          90           20288 : ByteBufferImpl::GetLength(void) const
          91                 : {
          92           20288 :   return mLength;
          93                 : }
          94                 :
          95                 : NS_IMETHODIMP_(PRUint32)
          96               0 : ByteBufferImpl::GetBufferSize(void) const
          97                 : {
          98               0 :   return mSpace;
          99                 : }
         100                 :
         101                 : NS_IMETHODIMP_(char*)
         102           13347 : ByteBufferImpl::GetBuffer(void) const
         103                 : {
         104           13347 :   return mBuffer;
         105                 : }
         106                 :
         107                 : NS_IMETHODIMP_(bool)
         108               0 : ByteBufferImpl::Grow(PRUint32 aNewSize)
         109                 : {
         110               0 :   if (aNewSize < MIN_BUFFER_SIZE) {
         111               0 :     aNewSize = MIN_BUFFER_SIZE;
         112                 :   }
         113               0 :   char* newbuf = new char[aNewSize];
         114               0 :   if (nsnull != newbuf) {
         115               0 :     if (0 != mLength) {
         116               0 :       memcpy(newbuf, mBuffer, mLength);
         117                 :     }
         118               0 :     delete[] mBuffer;
         119               0 :     mBuffer = newbuf;
         120               0 :     return true;
         121                 :   }
         122               0 :   return false;
         123                 : }
         124                 :
         125                 : NS_IMETHODIMP_(PRInt32)
         126            7689 : ByteBufferImpl::Fill(nsresult* aErrorCode, nsIInputStream* aStream,
         127                 :                      PRUint32 aKeep)
         128                 : {
         129            7689 :   NS_PRECONDITION(nsnull != aStream, "null stream");
         130            7689 :   NS_PRECONDITION(aKeep <= mLength, "illegal keep count");
         131            7689 :   if ((nsnull == aStream) || (PRUint32(aKeep) > PRUint32(mLength))) {
         132                 :     // whoops
         133               0 :     *aErrorCode = NS_BASE_STREAM_ILLEGAL_ARGS;
         134               0 :     return -1;
         135                 :   }
         136                 :
         137            7689 :   if (0 != aKeep) {
         138                 :     // Slide over kept data
         139               0 :     memmove(mBuffer, mBuffer + (mLength - aKeep), aKeep);
         140                 :   }
         141                 :
         142                 :   // Read in some new data
         143            7689 :   mLength = aKeep;
         144                 :   PRUint32 nb;
         145            7689 :   *aErrorCode = aStream->Read(mBuffer + aKeep, mSpace - aKeep, &nb);
         146            7689 :   if (NS_SUCCEEDED(*aErrorCode)) {
         147            7689 :     mLength += nb;
         148                 :   }
         149                 :   else
         150               0 :     nb = 0;
         151            7689 :   return nb;
         152                 : }
         153                 :
         154            2738 : nsresult NS_NewByteBuffer(nsIByteBuffer** aInstancePtrResult,
         155                 :                                   nsISupports* aOuter,
         156                 :                                   PRUint32 aBufferSize)
         157                 : {
         158                 :   nsresult rv;
         159                 :   nsIByteBuffer* buf;
         160            2738 :   rv = ByteBufferImpl::Create(aOuter, NS_GET_IID(nsIByteBuffer), (void**)&buf);
         161            2738 :   if (NS_FAILED(rv)) return rv;
         162                 :
         163            2738 :   rv = buf->Init(aBufferSize);
         164            2738 :   if (NS_FAILED(rv)) {
         165               0 :     NS_RELEASE(buf);
         166               0 :     return rv;
         167                 :   }
         168            2738 :   *aInstancePtrResult = buf;
         169            2738 :   return rv;
         170                 : }

Generated by: LCOV version 1.7