1 #include "Converters.h"
2 #include "nsIStringStream.h"
4 #include "nsComponentManagerUtils.h"
8 //////////////////////////////////////////////////
10 //////////////////////////////////////////////////
12 NS_IMPL_ISUPPORTS3(TestConverter
,
17 TestConverter::TestConverter() {
20 // Convert aFromStream (of type aFromType), to _retval (nsIInputStream of type aToType).
21 // This Convert method simply converts the stream byte-by-byte, to the first character
22 // in the aToType "string".
24 TestConverter::Convert(nsIInputStream
*aFromStream
,
25 const char *aFromType
,
28 nsIInputStream
**_retval
) {
31 nsresult rv
= aFromStream
->Read(buf
, 1024, &read
);
32 if (NS_FAILED(rv
) || read
== 0) return rv
;
34 // verify that the data we're converting matches the from type
35 // if it doesn't then we're being handed the wrong data.
36 char fromChar
= *aFromType
;
38 if (fromChar
!= buf
[0]) {
39 printf("We're receiving %c, but are supposed to have %c.\n", buf
[0], fromChar
);
40 return NS_ERROR_FAILURE
;
44 // Get the first character
45 char toChar
= *aToType
;
47 for (PRUint32 i
= 0; i
< read
; i
++)
52 nsCOMPtr
<nsIStringInputStream
> str
53 (do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv
));
54 NS_ENSURE_SUCCESS(rv
, rv
);
56 rv
= str
->SetData(buf
, read
);
57 NS_ENSURE_SUCCESS(rv
, rv
);
59 NS_ADDREF(*_retval
= str
);
63 /* This method initializes any internal state before the stream converter
64 * begins asynchronous conversion */
66 TestConverter::AsyncConvertData(const char *aFromType
,
68 nsIStreamListener
*aListener
,
70 NS_ASSERTION(aListener
, "null listener");
72 mListener
= aListener
;
74 // based on these types, setup internal state to handle the appropriate conversion.
81 // nsIStreamListener method
82 /* This method handles asyncronous conversion of data. */
84 TestConverter::OnDataAvailable(nsIRequest
* request
,
86 nsIInputStream
*inStr
,
87 PRUint32 sourceOffset
,
90 nsCOMPtr
<nsIInputStream
> convertedStream
;
91 // just make a syncronous call to the Convert() method.
92 // Anything can happen here, I just happen to be using the sync call to
93 // do the actual conversion.
94 rv
= Convert(inStr
, fromType
.get(), toType
.get(), ctxt
, getter_AddRefs(convertedStream
));
95 if (NS_FAILED(rv
)) return rv
;
98 convertedStream
->Available(&len
);
99 return mListener
->OnDataAvailable(request
, ctxt
, convertedStream
, sourceOffset
, len
);
102 // nsIRequestObserver methods
103 /* These methods just pass through directly to the mListener */
105 TestConverter::OnStartRequest(nsIRequest
* request
, nsISupports
*ctxt
) {
106 return mListener
->OnStartRequest(request
, ctxt
);
110 TestConverter::OnStopRequest(nsIRequest
* request
, nsISupports
*ctxt
,
112 return mListener
->OnStopRequest(request
, ctxt
, aStatus
);
116 ////////////////////////////////////////////////////////////////////////
117 // TestConverterFactory
118 ////////////////////////////////////////////////////////////////////////
119 TestConverterFactory::TestConverterFactory(const nsCID
&aClass
,
120 const char* className
,
121 const char* contractID
)
122 : mClassID(aClass
), mClassName(className
), mContractID(contractID
)
126 TestConverterFactory::~TestConverterFactory()
130 NS_IMPL_ISUPPORTS1(TestConverterFactory
, nsIFactory
)
133 TestConverterFactory::CreateInstance(nsISupports
*aOuter
,
138 return NS_ERROR_NULL_POINTER
;
141 return NS_ERROR_NO_AGGREGATION
;
147 nsISupports
*inst
= nsnull
;
148 if (mClassID
.Equals(kTestConverterCID
)) {
149 TestConverter
*conv
= new TestConverter();
150 if (!conv
) return NS_ERROR_OUT_OF_MEMORY
;
151 conv
->QueryInterface(NS_GET_IID(nsISupports
), (void**)&inst
);
154 return NS_ERROR_NO_INTERFACE
;
158 return NS_ERROR_OUT_OF_MEMORY
;
165 nsresult
TestConverterFactory::LockFactory(PRBool aLock
)
167 // Not implemented in simplest case.
170 ////////////////////////////////////////////////////////////////////////
171 // TestConverterFactory END
172 ////////////////////////////////////////////////////////////////////////