29 #include <nsIFileStreams.h>
30 #include <nsIFileURL.h>
31 #include <nsILocalFile.h>
35 #include <nsComponentManagerUtils.h>
38 #include <nsNetUtil.h>
49 NS_ENSURE_ARG_POINTER(aStream);
52 nsCOMPtr<nsILocalFile>
file = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
53 NS_ENSURE_SUCCESS(rv, rv);
55 rv = file->InitWithPath(aPath);
56 NS_ENSURE_SUCCESS(rv, rv);
59 NS_ENSURE_SUCCESS(rv, rv);
66 NS_ENSURE_ARG_POINTER(aStream);
67 NS_ENSURE_ARG_POINTER(aURI);
70 nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aURI, &rv);
71 NS_ENSURE_SUCCESS(rv, rv);
73 nsCOMPtr<nsIFile>
file;
74 rv = fileURL->GetFile(getter_AddRefs(file));
75 NS_ENSURE_SUCCESS(rv, rv);
78 NS_ENSURE_SUCCESS(rv, rv);
85 NS_ENSURE_ARG_POINTER(aStream);
86 NS_ENSURE_ARG_POINTER(aFile);
89 nsCOMPtr<nsIFileInputStream> fileStream =
90 do_CreateInstance(NS_LOCALFILEINPUTSTREAM_CONTRACTID, &rv);
91 NS_ENSURE_SUCCESS(rv, rv);
93 rv = fileStream->Init(aFile, -1, -1, 0);
94 NS_ENSURE_SUCCESS(rv, rv);
96 nsCOMPtr<nsIInputStream> stream = do_QueryInterface(fileStream, &rv);
97 NS_ENSURE_SUCCESS(rv, rv);
99 stream.forget(aStream);
106 NS_ENSURE_ARG_POINTER(aStream);
107 NS_ENSURE_ARG_POINTER(aFile);
110 nsCOMPtr<nsIFileOutputStream> fileStream =
111 do_CreateInstance(NS_LOCALFILEOUTPUTSTREAM_CONTRACTID, &rv);
112 NS_ENSURE_SUCCESS(rv, rv);
114 rv = fileStream->Init(aFile, -1, -1, 0);
115 NS_ENSURE_SUCCESS(rv, rv);
117 nsCOMPtr<nsIOutputStream> stream = do_QueryInterface(fileStream, &rv);
118 NS_ENSURE_SUCCESS(rv, rv);
120 stream.forget(aStream);
127 NS_ENSURE_ARG_POINTER(aStream);
130 nsCOMPtr<nsILocalFile>
file = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
131 NS_ENSURE_SUCCESS(rv, rv);
133 rv = file->InitWithPath(aPath);
134 NS_ENSURE_SUCCESS(rv, rv);
137 NS_ENSURE_SUCCESS(rv, rv);
145 NS_ENSURE_ARG_POINTER(aFile);
149 rv = aFile->GetFileSize(&fileSize);
150 NS_ENSURE_SUCCESS(rv, rv);
152 nsCOMPtr<nsIInputStream> stream;
154 NS_ENSURE_SUCCESS(rv, rv);
156 rv =
sbConsumeStream(stream, static_cast<PRUint32>(fileSize), aBuffer);
157 NS_ENSURE_SUCCESS(rv, rv);
173 rv = stream->Available(&avail);
175 if (rv == NS_BASE_STREAM_CLOSED)
181 if (avail > maxCount)
185 PRUint32 length = result.Length();
186 result.SetLength(length + avail);
187 if (result.Length() != (length + avail))
188 return NS_ERROR_OUT_OF_MEMORY;
189 char *buf = result.BeginWriting() + length;
192 rv = stream->Read(buf, avail, &n);
196 result.SetLength(length + n);
209 if (NS_IsMainThread()) {
210 return do_GetIOService(&rv);
219 NS_ENSURE_ARG_POINTER(aFile);
220 NS_ENSURE_ARG_POINTER(aURI);
226 NS_ENSURE_SUCCESS(rv, rv);
230 #if XP_UNIX && !XP_MACOSX
231 nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(aFile, &rv);
232 if (NS_SUCCEEDED(rv)) {
234 nsCAutoString descriptor;
235 rv = localFile->GetPersistentDescriptor(descriptor);
236 if (NS_SUCCEEDED(rv)) {
238 nsCOMPtr<nsINetUtil> netUtil =
239 do_CreateInstance(
"@mozilla.org/network/util;1", &rv);
240 NS_ENSURE_SUCCESS(rv, rv);
242 rv = netUtil->EscapeString(descriptor,
243 nsINetUtil::ESCAPE_URL_PATH,
245 NS_ENSURE_SUCCESS(rv, rv);
248 spec.Insert(
"file://", 0);
252 NS_ENSURE_SUCCESS(rv, rv);
260 nsCOMPtr<nsIURI>
uri;
261 rv = ioService->NewFileURI(aFile, getter_AddRefs(uri));
262 NS_ENSURE_SUCCESS(rv, rv);
266 NS_ENSURE_SUCCESS(rv, rv);
269 mainThreadURI.forget(aURI);
276 PRBool aAllPlatforms)
279 const char* badCharacters;
286 aFileName.StripChars(badCharacters);
292 aFileName.Trim(
" .", PR_TRUE, PR_TRUE);
#define SB_FILE_BAD_CHARACTERS_ALL_PLATFORMS
nsresult sbOpenInputStream(nsAString const &aPath, nsIInputStream **aStream)
nsCOMPtr< nsIIOService > GetIOService(nsresult &rv)
nsresult sbReadFile(nsIFile *aFile, nsACString &aBuffer)
nsresult sbOpenOutputStream(nsIFile *aFile, nsIOutputStream **aStream)
#define SB_FILE_BAD_CHARACTERS
static nsresult SB_NewURI(nsIURI **aURI, const nsACString &aSpec, const char *aCharSet=nsnull, nsIURI *aBaseURI=nsnull)
nsresult sbConsumeStream(nsIInputStream *stream, PRUint32 maxCount, nsACString &result)
const sbCreateProxiedComponent do_ProxiedGetService(const nsCID &aCID, nsresult *error=0)
sbMainThreadQueryInterface do_MainThreadQueryInterface(nsISupports *aSupports, nsresult *aResult=nsnull)
nsresult sbNewFileURI(nsIFile *aFile, nsIURI **aURI)
void RemoveBadFileNameCharacters(nsAString &aFileName, PRBool aAllPlatforms)