1999-09-16 06:33:31 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-09-16 06:33:31 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-09-16 06:33:31 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-09-16 06:33:31 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:40:37 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-09-16 06:33:31 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsIFileTransportService.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIAllocator.h"
|
|
|
|
#include "nsString.h"
|
2000-01-25 00:28:28 +03:00
|
|
|
#include "nsIFileStreams.h"
|
1999-09-16 06:33:31 +04:00
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsIEventQueue.h"
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kFileTransportServiceCID, NS_FILETRANSPORTSERVICE_CID);
|
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
1999-11-23 10:03:04 +03:00
|
|
|
static NS_DEFINE_CID(kEventQueueCID, NS_EVENTQUEUE_CID);
|
1999-09-16 06:33:31 +04:00
|
|
|
|
|
|
|
PRBool gDone = PR_FALSE;
|
|
|
|
nsIEventQueue* gEventQ = nsnull;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class MyListener : public nsIStreamListener {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD OnStartRequest(nsIChannel *channel, nsISupports *ctxt) {
|
|
|
|
printf("starting\n");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnStopRequest(nsIChannel *channel, nsISupports *ctxt,
|
|
|
|
nsresult status, const PRUnichar *errorMsg) {
|
|
|
|
printf("ending status=%0x total=%d\n", status, mTotal);
|
1999-10-06 12:26:01 +04:00
|
|
|
if (--mStopCount == 0)
|
|
|
|
gDone = PR_TRUE;
|
1999-09-16 06:33:31 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnDataAvailable(nsIChannel *channel, nsISupports *ctxt,
|
|
|
|
nsIInputStream *inStr, PRUint32 sourceOffset,
|
|
|
|
PRUint32 count) {
|
|
|
|
printf("receiving %d bytes\n", count);
|
|
|
|
char buf[256];
|
|
|
|
PRUint32 writeCount;
|
|
|
|
nsresult rv;
|
|
|
|
while (count > 0) {
|
|
|
|
PRUint32 amt = PR_MIN(count, 256);
|
|
|
|
PRUint32 readCount;
|
|
|
|
rv = inStr->Read(buf, amt, &readCount);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
NS_ASSERTION(readCount != 0, "premature EOF");
|
|
|
|
nsresult rv = mOut->Write(buf, readCount, &writeCount);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
NS_ASSERTION(writeCount == readCount, "failed to write all the data");
|
|
|
|
count -= readCount;
|
|
|
|
mTotal += readCount;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-10-06 12:26:01 +04:00
|
|
|
MyListener(PRUint32 stopCount = 1) : mTotal(0), mStopCount(stopCount) {
|
1999-09-16 06:33:31 +04:00
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Init(nsFileSpec& origFile) {
|
|
|
|
nsresult rv;
|
|
|
|
char* name = origFile.GetLeafName();
|
|
|
|
if (name == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
nsAutoString str(name);
|
|
|
|
nsAllocator::Free(name);
|
|
|
|
str += ".bak";
|
|
|
|
nsFileSpec spec(origFile);
|
|
|
|
spec.SetLeafName(str);
|
2000-01-25 00:28:28 +03:00
|
|
|
nsCOMPtr<nsILocalFile> file;
|
|
|
|
rv = NS_NewLocalFile(spec, getter_AddRefs(file));
|
2000-03-29 07:58:50 +04:00
|
|
|
rv = NS_NewLocalFileOutputStream(getter_AddRefs(mOut),
|
|
|
|
file,
|
|
|
|
PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE,
|
|
|
|
0664);
|
2000-01-25 00:28:28 +03:00
|
|
|
return rv;
|
1999-09-16 06:33:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MyListener() {
|
|
|
|
nsresult rv = mOut->Close();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Close failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsCOMPtr<nsIOutputStream> mOut;
|
|
|
|
PRUint32 mTotal;
|
1999-10-06 12:26:01 +04:00
|
|
|
PRUint32 mStopCount;
|
1999-09-16 06:33:31 +04:00
|
|
|
};
|
|
|
|
|
1999-10-06 12:26:01 +04:00
|
|
|
NS_IMPL_ISUPPORTS2(MyListener, nsIStreamListener, nsIStreamObserver);
|
1999-09-16 06:33:31 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TestAsyncRead(const char* fileName, PRUint32 offset, PRInt32 length)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
NS_WITH_SERVICE(nsIFileTransportService, fts, kFileTransportServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsFileSpec fs(fileName);
|
|
|
|
nsIChannel* fileTrans;
|
2000-01-25 00:28:28 +03:00
|
|
|
nsCOMPtr<nsILocalFile> file;
|
|
|
|
rv = NS_NewLocalFile(fs, getter_AddRefs(file));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-03-29 07:58:50 +04:00
|
|
|
rv = fts->CreateTransport(file, PR_RDONLY, 0, &fileTrans);
|
1999-09-16 06:33:31 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
MyListener* listener = new MyListener();
|
|
|
|
if (listener == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(listener);
|
|
|
|
rv = listener->Init(fs);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
gDone = PR_FALSE;
|
2000-03-29 07:58:50 +04:00
|
|
|
rv = fileTrans->SetTransferOffset(offset);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = fileTrans->SetTransferCount(length);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = fileTrans->AsyncRead(nsnull, listener);
|
1999-09-16 06:33:31 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
while (!gDone) {
|
|
|
|
PLEvent* event;
|
|
|
|
rv = gEventQ->GetEvent(&event);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = gEventQ->HandleEvent(event);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_RELEASE(listener);
|
|
|
|
NS_RELEASE(fileTrans);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TestAsyncWrite(const char* fileName, PRUint32 offset, PRInt32 length)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
NS_WITH_SERVICE(nsIFileTransportService, fts, kFileTransportServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsString outFile(fileName);
|
|
|
|
outFile += ".out";
|
|
|
|
nsFileSpec fs(outFile);
|
|
|
|
nsIChannel* fileTrans;
|
2000-01-25 00:28:28 +03:00
|
|
|
nsCOMPtr<nsILocalFile> file;
|
|
|
|
rv = NS_NewLocalFile(fs, getter_AddRefs(file));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = fts->CreateTransport(file,
|
|
|
|
PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE,
|
2000-03-29 07:58:50 +04:00
|
|
|
0664, &fileTrans);
|
1999-09-16 06:33:31 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
MyListener* listener = new MyListener();
|
|
|
|
if (listener == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(listener);
|
|
|
|
rv = listener->Init(fs);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsFileSpec spec(fileName);
|
2000-01-25 00:28:28 +03:00
|
|
|
nsCOMPtr<nsILocalFile> f;
|
|
|
|
rv = NS_NewLocalFile(spec, getter_AddRefs(f));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
nsCOMPtr<nsIInputStream> inStr;
|
2000-03-29 07:58:50 +04:00
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), f);
|
1999-09-16 06:33:31 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
gDone = PR_FALSE;
|
2000-03-29 07:58:50 +04:00
|
|
|
rv = fileTrans->SetTransferOffset(offset);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = fileTrans->SetTransferCount(length);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = fileTrans->AsyncWrite(inStr, nsnull, listener);
|
1999-09-16 06:33:31 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
while (!gDone) {
|
|
|
|
PLEvent* event;
|
|
|
|
rv = gEventQ->GetEvent(&event);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = gEventQ->HandleEvent(event);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_RELEASE(listener);
|
|
|
|
NS_RELEASE(fileTrans);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-10-06 12:26:01 +04:00
|
|
|
class MyOpenObserver : public nsIStreamObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD OnStartRequest(nsIChannel *channel, nsISupports *ctxt) {
|
|
|
|
nsresult rv;
|
|
|
|
char* contentType;
|
|
|
|
rv = channel->GetContentType(&contentType);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
PRInt32 length;
|
|
|
|
rv = channel->GetContentLength(&length);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
printf("stream opened: content type = %s, length = %d\n",
|
|
|
|
contentType, length);
|
|
|
|
nsCRT::free(contentType);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnStopRequest(nsIChannel *channel, nsISupports *ctxt,
|
|
|
|
nsresult status, const PRUnichar *errorMsg) {
|
|
|
|
printf("stream closed: status %x\n", status);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
MyOpenObserver() { NS_INIT_REFCNT(); }
|
|
|
|
virtual ~MyOpenObserver() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(MyOpenObserver, nsIStreamObserver);
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TestAsyncOpen(const char* fileName, PRUint32 offset, PRInt32 length)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
NS_WITH_SERVICE(nsIFileTransportService, fts, kFileTransportServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsFileSpec fs(fileName);
|
|
|
|
nsIChannel* fileTrans;
|
2000-01-25 00:28:28 +03:00
|
|
|
nsCOMPtr<nsILocalFile> file;
|
|
|
|
rv = NS_NewLocalFile(fs, getter_AddRefs(file));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-03-29 07:58:50 +04:00
|
|
|
rv = fts->CreateTransport(file, PR_RDONLY, 0, &fileTrans);
|
1999-10-06 12:26:01 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
MyListener* listener = new MyListener(1);
|
|
|
|
if (listener == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(listener);
|
|
|
|
rv = listener->Init(fs);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
MyOpenObserver* openObserver = new MyOpenObserver();
|
|
|
|
if (openObserver == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(openObserver);
|
|
|
|
|
|
|
|
gDone = PR_FALSE;
|
|
|
|
rv = fileTrans->AsyncOpen(openObserver, nsnull);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-03-29 07:58:50 +04:00
|
|
|
rv = fileTrans->SetTransferOffset(offset + 10);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = fileTrans->SetTransferCount(length);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = fileTrans->AsyncRead(nsnull, listener);
|
1999-10-06 12:26:01 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
#if 0
|
|
|
|
rv = fileTrans->AsyncRead(offset, length, nsnull, listener);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = fileTrans->AsyncRead(offset + 100, length, nsnull, listener);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
#endif
|
|
|
|
while (!gDone) {
|
|
|
|
PLEvent* event;
|
|
|
|
rv = gEventQ->GetEvent(&event);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = gEventQ->HandleEvent(event);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_RELEASE(openObserver);
|
|
|
|
NS_RELEASE(listener);
|
|
|
|
NS_RELEASE(fileTrans);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-09-16 06:33:31 +04:00
|
|
|
nsresult
|
|
|
|
NS_AutoregisterComponents()
|
|
|
|
{
|
|
|
|
nsresult rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup, NULL /* default */);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
printf("usage: %s <file-to-read>\n", argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
char* fileName = argv[1];
|
|
|
|
|
|
|
|
rv = NS_AutoregisterComponents();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-10-06 12:26:01 +04:00
|
|
|
NS_WITH_SERVICE(nsIEventQueueService, eventQService, kEventQueueServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = eventQService->CreateThreadEventQueue();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-11-30 03:32:43 +03:00
|
|
|
rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
|
1999-10-06 12:26:01 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-09-16 06:33:31 +04:00
|
|
|
rv = TestAsyncRead(fileName, 0, -1);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncRead failed");
|
|
|
|
|
|
|
|
rv = TestAsyncWrite(fileName, 0, -1);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncWrite failed");
|
|
|
|
|
|
|
|
rv = TestAsyncRead(fileName, 10, 100);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncRead failed");
|
|
|
|
|
1999-10-06 12:26:01 +04:00
|
|
|
rv = TestAsyncOpen(fileName, 10, 100);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncOpen failed");
|
|
|
|
|
|
|
|
NS_RELEASE(gEventQ);
|
1999-09-16 06:33:31 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|