Back to index

lightning-sunbird  0.9+nobinonly
nsByteArrayInputStream.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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 Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
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 "nsByteArrayInputStream.h"
00040 #include "nsMemory.h"
00041 
00042 NS_IMPL_THREADSAFE_ISUPPORTS2(nsByteArrayInputStream, nsIInputStream, nsIByteArrayInputStream)
00043 
00044 nsByteArrayInputStream::nsByteArrayInputStream (char *buffer, PRUint32 bytes)
00045     : _buffer (buffer), _nbytes (bytes), _pos (0)
00046 { 
00047 }
00048 
00049 nsByteArrayInputStream::~nsByteArrayInputStream ()
00050 {
00051     if (_buffer != NULL)
00052         nsMemory::Free (_buffer);
00053 }
00054 
00055 NS_IMETHODIMP
00056 nsByteArrayInputStream::Available (PRUint32* aResult)
00057 {
00058     if (aResult == NULL)
00059         return NS_ERROR_NULL_POINTER;
00060 
00061     if (_nbytes == 0 || _buffer == NULL)
00062         *aResult = 0;
00063     else
00064         *aResult = _nbytes - _pos;
00065 
00066     return NS_OK;
00067 }
00068 
00069 NS_IMETHODIMP
00070 nsByteArrayInputStream::Read (char* aBuffer, PRUint32 aCount, PRUint32 *aNumRead)
00071 {
00072     if (aBuffer == NULL || aNumRead == NULL)
00073         return NS_ERROR_NULL_POINTER;
00074 
00075     if (_nbytes == 0)
00076         return NS_ERROR_FAILURE;
00077 
00078     if (aCount == 0 || _pos == _nbytes)
00079         *aNumRead = 0;
00080     else
00081     {
00082         NS_ASSERTION (_buffer != NULL, "Stream buffer has been released - there's an ownership problem somewhere!");
00083         if (_buffer == NULL)
00084             *aNumRead = 0;
00085         else
00086         if (aCount > _nbytes - _pos)
00087         {        
00088             memcpy (aBuffer, &_buffer[_pos], *aNumRead = _nbytes - _pos);
00089             _pos = _nbytes;
00090         }
00091         else
00092         {
00093             memcpy (aBuffer, &_buffer[_pos], *aNumRead = aCount);
00094             _pos += aCount;
00095         }
00096     }
00097     return NS_OK;
00098 }
00099 
00100 NS_IMETHODIMP 
00101 nsByteArrayInputStream::ReadSegments(nsWriteSegmentFun writer, void * aClosure, PRUint32 aCount, PRUint32 *aNumRead)
00102 {
00103     if (aNumRead == NULL)
00104         return NS_ERROR_NULL_POINTER;
00105 
00106     if (_nbytes == 0)
00107         return NS_ERROR_FAILURE;
00108 
00109     if (aCount == 0 || _pos == _nbytes)
00110         *aNumRead = 0;
00111     else {
00112         NS_ASSERTION (_buffer != NULL, "Stream buffer has been released - there's an ownership problem somewhere!");
00113         PRUint32 readCount = PR_MIN(aCount, (_nbytes - _pos));
00114         if (_buffer == NULL)
00115             *aNumRead = 0;
00116         else {
00117             nsresult rv = writer (this, aClosure, &_buffer[_pos], 
00118                                   _pos, readCount, aNumRead);
00119             if (NS_SUCCEEDED(rv))
00120                 _pos += *aNumRead;
00121         }
00122     }
00123 
00124     // do not propogate errors returned from writer!
00125     return NS_OK;
00126 }
00127 
00128 NS_IMETHODIMP 
00129 nsByteArrayInputStream::IsNonBlocking(PRBool *aNonBlocking)
00130 {
00131     *aNonBlocking = PR_TRUE;
00132     return NS_OK;
00133 }
00134 
00135 NS_IMETHODIMP
00136 nsByteArrayInputStream::Close ()
00137 {
00138     if (_buffer != NULL)
00139     {
00140         nsMemory::Free (_buffer);
00141         _buffer = NULL;
00142         _nbytes = 0;
00143     }
00144     else
00145         return NS_ERROR_FAILURE;
00146 
00147     return NS_OK;
00148 }
00149 
00150 NS_COM nsresult
00151 NS_NewByteArrayInputStream (nsIByteArrayInputStream* *aResult, char * buffer, unsigned long bytes)
00152 {
00153     if (aResult == NULL)
00154         return NS_ERROR_NULL_POINTER;
00155 
00156     nsIByteArrayInputStream * stream = new nsByteArrayInputStream (buffer, bytes);
00157 
00158     if (!stream)
00159         return NS_ERROR_OUT_OF_MEMORY;
00160 
00161     NS_ADDREF (stream);
00162     *aResult = stream;
00163     return NS_OK;
00164 }