VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/ds/nsByteBuffer.cpp@ 4837

Last change on this file since 4837 was 1, checked in by vboxsync, 54 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.5 KB
Line 
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
45ByteBufferImpl::ByteBufferImpl(void)
46 : mBuffer(NULL), mSpace(0), mLength(0)
47{
48}
49
50NS_IMETHODIMP
51ByteBufferImpl::Init(PRUint32 aBufferSize)
52{
53 if (aBufferSize < MIN_BUFFER_SIZE) {
54 aBufferSize = MIN_BUFFER_SIZE;
55 }
56 mSpace = aBufferSize;
57 mLength = 0;
58 mBuffer = new char[aBufferSize];
59 return mBuffer ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
60}
61
62NS_IMPL_ISUPPORTS1(ByteBufferImpl,nsIByteBuffer)
63
64ByteBufferImpl::~ByteBufferImpl()
65{
66 if (nsnull != mBuffer) {
67 delete[] mBuffer;
68 mBuffer = nsnull;
69 }
70 mLength = 0;
71}
72
73NS_METHOD
74ByteBufferImpl::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
75{
76 if (aOuter)
77 return NS_ERROR_NO_AGGREGATION;
78
79 ByteBufferImpl* it = new ByteBufferImpl();
80 if (nsnull == it)
81 return NS_ERROR_OUT_OF_MEMORY;
82
83 NS_ADDREF(it);
84 nsresult rv = it->QueryInterface(aIID, (void**)aResult);
85 NS_RELEASE(it);
86 return rv;
87}
88
89NS_IMETHODIMP_(PRUint32)
90ByteBufferImpl::GetLength(void) const
91{
92 return mLength;
93}
94
95NS_IMETHODIMP_(PRUint32)
96ByteBufferImpl::GetBufferSize(void) const
97{
98 return mSpace;
99}
100
101NS_IMETHODIMP_(char*)
102ByteBufferImpl::GetBuffer(void) const
103{
104 return mBuffer;
105}
106
107NS_IMETHODIMP_(PRBool)
108ByteBufferImpl::Grow(PRUint32 aNewSize)
109{
110 if (aNewSize < MIN_BUFFER_SIZE) {
111 aNewSize = MIN_BUFFER_SIZE;
112 }
113 char* newbuf = new char[aNewSize];
114 if (nsnull != newbuf) {
115 if (0 != mLength) {
116 memcpy(newbuf, mBuffer, mLength);
117 }
118 delete[] mBuffer;
119 mBuffer = newbuf;
120 return PR_TRUE;
121 }
122 return PR_FALSE;
123}
124
125NS_IMETHODIMP_(PRInt32)
126ByteBufferImpl::Fill(nsresult* aErrorCode, nsIInputStream* aStream,
127 PRUint32 aKeep)
128{
129 NS_PRECONDITION(nsnull != aStream, "null stream");
130 NS_PRECONDITION(aKeep <= mLength, "illegal keep count");
131 if ((nsnull == aStream) || (PRUint32(aKeep) > PRUint32(mLength))) {
132 // whoops
133 *aErrorCode = NS_BASE_STREAM_ILLEGAL_ARGS;
134 return -1;
135 }
136
137 if (0 != aKeep) {
138 // Slide over kept data
139 memmove(mBuffer, mBuffer + (mLength - aKeep), aKeep);
140 }
141
142 // Read in some new data
143 mLength = aKeep;
144 PRUint32 nb;
145 *aErrorCode = aStream->Read(mBuffer + aKeep, mSpace - aKeep, &nb);
146 if (NS_SUCCEEDED(*aErrorCode)) {
147 mLength += nb;
148 }
149 else
150 nb = 0;
151 return nb;
152}
153
154NS_COM nsresult NS_NewByteBuffer(nsIByteBuffer** aInstancePtrResult,
155 nsISupports* aOuter,
156 PRUint32 aBufferSize)
157{
158 nsresult rv;
159 nsIByteBuffer* buf;
160 rv = ByteBufferImpl::Create(aOuter, NS_GET_IID(nsIByteBuffer), (void**)&buf);
161 if (NS_FAILED(rv)) return rv;
162
163 rv = buf->Init(aBufferSize);
164 if (NS_FAILED(rv)) {
165 NS_RELEASE(buf);
166 return rv;
167 }
168 *aInstancePtrResult = buf;
169 return rv;
170}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use