Bug 588735 - Mirror glass caption buttons for rtl windows. r=roc, a=blocking-betaN.
[mozilla-central.git] / startupcache / StartupCacheUtils.cpp
blobecafc11658c667e20df8ee1c02df108c90200538
2 #include "nsCOMPtr.h"
3 #include "nsIInputStream.h"
4 #include "nsIStringStream.h"
5 #include "nsAutoPtr.h"
6 #include "StartupCacheUtils.h"
7 #include "mozilla/scache/StartupCache.h"
9 namespace mozilla {
10 namespace scache {
12 NS_EXPORT nsresult
13 NS_NewObjectInputStreamFromBuffer(char* buffer, PRUint32 len,
14 nsIObjectInputStream** stream)
16 nsCOMPtr<nsIStringInputStream> stringStream
17 = do_CreateInstance("@mozilla.org/io/string-input-stream;1");
18 nsCOMPtr<nsIObjectInputStream> objectInput
19 = do_CreateInstance("@mozilla.org/binaryinputstream;1");
21 stringStream->AdoptData(buffer, len);
22 objectInput->SetInputStream(stringStream);
24 objectInput.forget(stream);
25 return NS_OK;
28 NS_EXPORT nsresult
29 NS_NewObjectOutputWrappedStorageStream(nsIObjectOutputStream **wrapperStream,
30 nsIStorageStream** stream)
32 nsresult rv;
33 nsCOMPtr<nsIStorageStream> storageStream
34 = do_CreateInstance("@mozilla.org/storagestream;1");
36 rv = storageStream->Init(256, PR_UINT32_MAX, nsnull);
37 NS_ENSURE_SUCCESS(rv, rv);
39 nsCOMPtr<nsIObjectOutputStream> objectOutput
40 = do_CreateInstance("@mozilla.org/binaryoutputstream;1");
41 nsCOMPtr<nsIOutputStream> outputStream
42 = do_QueryInterface(storageStream);
44 objectOutput->SetOutputStream(outputStream);
46 #ifdef DEBUG
47 // Wrap in debug stream to detect unsupported writes of
48 // multiply-referenced non-singleton objects
49 StartupCache* sc = StartupCache::GetSingleton();
50 NS_ENSURE_TRUE(sc, NS_ERROR_UNEXPECTED);
51 nsCOMPtr<nsIObjectOutputStream> debugStream;
52 sc->GetDebugObjectOutputStream(objectOutput, getter_AddRefs(debugStream));
53 debugStream.forget(wrapperStream);
54 #else
55 objectOutput.forget(wrapperStream);
56 #endif
58 storageStream.forget(stream);
59 return NS_OK;
62 NS_EXPORT nsresult
63 NS_NewBufferFromStorageStream(nsIStorageStream *storageStream,
64 char** buffer, PRUint32* len)
66 nsresult rv;
67 nsCOMPtr<nsIInputStream> inputStream;
68 rv = storageStream->NewInputStream(0, getter_AddRefs(inputStream));
69 NS_ENSURE_SUCCESS(rv, rv);
71 PRUint32 avail, read;
72 rv = inputStream->Available(&avail);
73 NS_ENSURE_SUCCESS(rv, rv);
75 nsAutoArrayPtr<char> temp (new char[avail]);
76 rv = inputStream->Read(temp, avail, &read);
77 if (NS_SUCCEEDED(rv) && avail != read)
78 rv = NS_ERROR_UNEXPECTED;
80 if (NS_FAILED(rv)) {
81 return rv;
84 *len = avail;
85 *buffer = temp.forget();
86 return NS_OK;