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-06-02 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            2750 : ByteBufferImpl::ByteBufferImpl(void)
      46            2750 :   : mBuffer(NULL), mSpace(0), mLength(0)
      47                 : {
      48            2750 : }
      49                 : 
      50                 : NS_IMETHODIMP
      51            2750 : ByteBufferImpl::Init(PRUint32 aBufferSize)
      52                 : {
      53            2750 :   if (aBufferSize < MIN_BUFFER_SIZE) {
      54              24 :     aBufferSize = MIN_BUFFER_SIZE;
      55                 :   }
      56            2750 :   mSpace = aBufferSize;
      57            2750 :   mLength = 0;
      58            5500 :   mBuffer = new char[aBufferSize];
      59            2750 :   return mBuffer ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
      60                 : }
      61                 : 
      62           24750 : NS_IMPL_ISUPPORTS1(ByteBufferImpl,nsIByteBuffer)
      63                 : 
      64            2750 : ByteBufferImpl::~ByteBufferImpl()
      65                 : {
      66            2750 :   if (nsnull != mBuffer) {
      67            2750 :     delete[] mBuffer;
      68            2750 :     mBuffer = nsnull;
      69                 :   }
      70            2750 :   mLength = 0;
      71            2750 : }
      72                 : 
      73                 : nsresult
      74            2750 : ByteBufferImpl::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
      75                 : {
      76            2750 :   if (aOuter)
      77               0 :     return NS_ERROR_NO_AGGREGATION;
      78                 : 
      79            2750 :   ByteBufferImpl* it = new ByteBufferImpl();
      80            2750 :   if (nsnull == it) 
      81               0 :     return NS_ERROR_OUT_OF_MEMORY;
      82                 : 
      83            2750 :   NS_ADDREF(it);
      84            2750 :   nsresult rv = it->QueryInterface(aIID, (void**)aResult);
      85            2750 :   NS_RELEASE(it);
      86            2750 :   return rv;
      87                 : }
      88                 : 
      89                 : NS_IMETHODIMP_(PRUint32)
      90           20366 : ByteBufferImpl::GetLength(void) const
      91                 : {
      92           20366 :   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           13401 : ByteBufferImpl::GetBuffer(void) const
     103                 : {
     104           13401 :   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            7719 : ByteBufferImpl::Fill(nsresult* aErrorCode, nsIInputStream* aStream,
     127                 :                      PRUint32 aKeep)
     128                 : {
     129            7719 :   NS_PRECONDITION(nsnull != aStream, "null stream");
     130            7719 :   NS_PRECONDITION(aKeep <= mLength, "illegal keep count");
     131            7719 :   if ((nsnull == aStream) || (PRUint32(aKeep) > PRUint32(mLength))) {
     132                 :     // whoops
     133               0 :     *aErrorCode = NS_BASE_STREAM_ILLEGAL_ARGS;
     134               0 :     return -1;
     135                 :   }
     136                 : 
     137            7719 :   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            7719 :   mLength = aKeep;
     144                 :   PRUint32 nb;
     145            7719 :   *aErrorCode = aStream->Read(mBuffer + aKeep, mSpace - aKeep, &nb);
     146            7719 :   if (NS_SUCCEEDED(*aErrorCode)) {
     147            7719 :     mLength += nb;
     148                 :   }
     149                 :   else
     150               0 :     nb = 0;
     151            7719 :   return nb;
     152                 : }
     153                 : 
     154            2750 : nsresult NS_NewByteBuffer(nsIByteBuffer** aInstancePtrResult,
     155                 :                                   nsISupports* aOuter,
     156                 :                                   PRUint32 aBufferSize)
     157                 : {
     158                 :   nsresult rv;
     159                 :   nsIByteBuffer* buf;
     160            2750 :   rv = ByteBufferImpl::Create(aOuter, NS_GET_IID(nsIByteBuffer), (void**)&buf);
     161            2750 :   if (NS_FAILED(rv)) return rv;
     162                 :     
     163            2750 :   rv = buf->Init(aBufferSize);
     164            2750 :   if (NS_FAILED(rv)) {
     165               0 :     NS_RELEASE(buf);
     166               0 :     return rv;
     167                 :   }
     168            2750 :   *aInstancePtrResult = buf;
     169            2750 :   return rv;
     170                 : }

Generated by: LCOV version 1.7