VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/io/nsByteArrayInputStream.cpp@ 44770

最後變更 在這個檔案從44770是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 4.8 KB
 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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 Communicator client code, released
16 * March 31, 1998.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998-1999
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
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 "nsByteArrayInputStream.h"
40#include "nsMemory.h"
41
42NS_IMPL_THREADSAFE_ISUPPORTS2(nsByteArrayInputStream, nsIInputStream, nsIByteArrayInputStream)
43
44nsByteArrayInputStream::nsByteArrayInputStream (char *buffer, PRUint32 bytes)
45 : _buffer (buffer), _nbytes (bytes), _pos (0)
46{
47}
48
49nsByteArrayInputStream::~nsByteArrayInputStream ()
50{
51 if (_buffer != NULL)
52 nsMemory::Free (_buffer);
53}
54
55NS_IMETHODIMP
56nsByteArrayInputStream::Available (PRUint32* aResult)
57{
58 if (aResult == NULL)
59 return NS_ERROR_NULL_POINTER;
60
61 if (_nbytes == 0 || _buffer == NULL)
62 *aResult = 0;
63 else
64 *aResult = _nbytes - _pos;
65
66 return NS_OK;
67}
68
69NS_IMETHODIMP
70nsByteArrayInputStream::Read (char* aBuffer, PRUint32 aCount, PRUint32 *aNumRead)
71{
72 if (aBuffer == NULL || aNumRead == NULL)
73 return NS_ERROR_NULL_POINTER;
74
75 if (_nbytes == 0)
76 return NS_ERROR_FAILURE;
77
78 if (aCount == 0 || _pos == _nbytes)
79 *aNumRead = 0;
80 else
81 {
82 NS_ASSERTION (_buffer != NULL, "Stream buffer has been released - there's an ownership problem somewhere!");
83 if (_buffer == NULL)
84 *aNumRead = 0;
85 else
86 if (aCount > _nbytes - _pos)
87 {
88 memcpy (aBuffer, &_buffer[_pos], *aNumRead = _nbytes - _pos);
89 _pos = _nbytes;
90 }
91 else
92 {
93 memcpy (aBuffer, &_buffer[_pos], *aNumRead = aCount);
94 _pos += aCount;
95 }
96 }
97 return NS_OK;
98}
99
100NS_IMETHODIMP
101nsByteArrayInputStream::ReadSegments(nsWriteSegmentFun writer, void * aClosure, PRUint32 aCount, PRUint32 *aNumRead)
102{
103 if (aNumRead == NULL)
104 return NS_ERROR_NULL_POINTER;
105
106 if (_nbytes == 0)
107 return NS_ERROR_FAILURE;
108
109 if (aCount == 0 || _pos == _nbytes)
110 *aNumRead = 0;
111 else {
112 NS_ASSERTION (_buffer != NULL, "Stream buffer has been released - there's an ownership problem somewhere!");
113 PRUint32 readCount = PR_MIN(aCount, (_nbytes - _pos));
114 if (_buffer == NULL)
115 *aNumRead = 0;
116 else {
117 nsresult rv = writer (this, aClosure, &_buffer[_pos],
118 _pos, readCount, aNumRead);
119 if (NS_SUCCEEDED(rv))
120 _pos += *aNumRead;
121 }
122 }
123
124 // do not propogate errors returned from writer!
125 return NS_OK;
126}
127
128NS_IMETHODIMP
129nsByteArrayInputStream::IsNonBlocking(PRBool *aNonBlocking)
130{
131 *aNonBlocking = PR_TRUE;
132 return NS_OK;
133}
134
135NS_IMETHODIMP
136nsByteArrayInputStream::Close ()
137{
138 if (_buffer != NULL)
139 {
140 nsMemory::Free (_buffer);
141 _buffer = NULL;
142 _nbytes = 0;
143 }
144 else
145 return NS_ERROR_FAILURE;
146
147 return NS_OK;
148}
149
150NS_COM nsresult
151NS_NewByteArrayInputStream (nsIByteArrayInputStream* *aResult, char * buffer, unsigned long bytes)
152{
153 if (aResult == NULL)
154 return NS_ERROR_NULL_POINTER;
155
156 nsIByteArrayInputStream * stream = new nsByteArrayInputStream (buffer, bytes);
157
158 if (!stream)
159 return NS_ERROR_OUT_OF_MEMORY;
160
161 NS_ADDREF (stream);
162 *aResult = stream;
163 return NS_OK;
164}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette