Back to index

lightning-sunbird  0.9+nobinonly
TestStorageStream.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla.
00015  *
00016  * The Initial Developer of the Original Code is IBM Corporation.
00017  * Portions created by IBM Corporation are Copyright (C) 2004
00018  * IBM Corporation. All Rights Reserved.
00019  *
00020  * Contributor(s):
00021  *   Darin Fisher <darin@meer.net>
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include <stdlib.h>
00038 #include "nsIStorageStream.h"
00039 #include "nsIInputStream.h"
00040 #include "nsIOutputStream.h"
00041 #include "nsCOMPtr.h"
00042 
00043 int main()
00044 {
00045   char kData[4096];
00046   memset(kData, 0, sizeof(kData));
00047 
00048   nsresult rv;
00049   nsCOMPtr<nsIStorageStream> stor;
00050 
00051   rv = NS_NewStorageStream(4096, PR_UINT32_MAX, getter_AddRefs(stor));
00052   if (NS_FAILED(rv))
00053     return -1;
00054 
00055   nsCOMPtr<nsIOutputStream> out;
00056   rv = stor->GetOutputStream(0, getter_AddRefs(out));
00057   if (NS_FAILED(rv))
00058     return -1;
00059 
00060   PRUint32 n;
00061 
00062   rv = out->Write(kData, sizeof(kData), &n);
00063   if (NS_FAILED(rv))
00064     return -1;
00065 
00066   rv = out->Write(kData, sizeof(kData), &n);
00067   if (NS_FAILED(rv))
00068     return -1;
00069 
00070   rv = out->Close();
00071   if (NS_FAILED(rv))
00072     return -1;
00073 
00074   out = nsnull;
00075   
00076   nsCOMPtr<nsIInputStream> in;
00077   rv = stor->NewInputStream(0, getter_AddRefs(in));
00078   if (NS_FAILED(rv))
00079     return -1;
00080 
00081   char buf[4096];
00082 
00083   // consume contents of input stream
00084   do {
00085     rv = in->Read(buf, sizeof(buf), &n);
00086     if (NS_FAILED(rv))
00087       return -1;
00088   } while (n != 0);
00089 
00090   rv = in->Close();
00091   if (NS_FAILED(rv))
00092     return -1;
00093   in = nsnull;
00094 
00095   // now, write 3 more full 4k segments + 11 bytes, starting at 8192
00096   // total written equals 20491 bytes
00097 
00098   rv = stor->GetOutputStream(8192, getter_AddRefs(out));
00099   if (NS_FAILED(rv))
00100     return -1;
00101 
00102   rv = out->Write(kData, sizeof(kData), &n);
00103   if (NS_FAILED(rv))
00104     return -1;
00105 
00106   rv = out->Write(kData, sizeof(kData), &n);
00107   if (NS_FAILED(rv))
00108     return -1;
00109 
00110   rv = out->Write(kData, sizeof(kData), &n);
00111   if (NS_FAILED(rv))
00112     return -1;
00113 
00114   rv = out->Write(kData, 11, &n);
00115   if (NS_FAILED(rv))
00116     return -1;
00117 
00118   rv = out->Close();
00119   if (NS_FAILED(rv))
00120     return -1;
00121 
00122   out = nsnull;
00123 
00124   // now, read all
00125   rv = stor->NewInputStream(0, getter_AddRefs(in));
00126   if (NS_FAILED(rv))
00127     return -1;
00128 
00129   // consume contents of input stream
00130   do {
00131     rv = in->Read(buf, sizeof(buf), &n);
00132     if (NS_FAILED(rv))
00133       return -1;
00134   } while (n != 0);
00135 
00136   rv = in->Close();
00137   if (NS_FAILED(rv))
00138     return -1;
00139   in = nsnull;
00140 
00141   return 0;
00142 }