public bool StoreBlob(byte[] data, BlobKey outKey)
 {
     BlobKey newKey = KeyForBlob(data);
     outKey.SetBytes(newKey.GetBytes());
     string path = PathForKey(outKey);
     FilePath file = new FilePath(path);
     if (file.CanRead())
     {
         return true;
     }
     FileOutputStream fos = null;
     try
     {
         fos = new FileOutputStream(file);
         fos.Write(data);
     }
     catch (FileNotFoundException e)
     {
         Log.E(Database.Tag, "Error opening file for output", e);
         return false;
     }
     catch (IOException ioe)
     {
         Log.E(Database.Tag, "Error writing to file", ioe);
         return false;
     }
     finally
     {
         if (fos != null)
         {
             try
             {
                 fos.Close();
             }
             catch (IOException)
             {
             }
         }
     }
     // ignore
     return true;
 }
 public bool IsGZipped(BlobKey key)
 {
     var magic = 0;
     var path = PathForKey(key);
     var file = new FilePath(path);
     if (file.CanRead())
     {
         try
         {
             var raf = new RandomAccessFile(file, "r");
             magic = raf.Read() & unchecked((0xff)) | ((raf.Read() << 8) & unchecked((0xff00)));
             raf.Close();
         }
         catch (Exception e)
         {
             Runtime.PrintStackTrace(e, Console.Error);
         }
     }
     return magic == GZIPInputStream.GzipMagic;
 }
 public Stream BlobStreamForKey(BlobKey key)
 {
     var path = PathForKey(key);
     Log.D(Database.Tag, "Blob Path : " + path);
     var file = new FilePath(path);
     if (file.CanRead())
     {
         try
         {
             return new FileStream(file, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
         }
         catch (FileNotFoundException e)
         {
             Log.E(Database.Tag, "Unexpected file not found in blob store", e);
             return null;
         }
         catch (Exception e)
         {
             Log.E(Database.Tag, "Cannot new FileStream", e);
         }
     }
     return null;
 }
        public bool StoreBlobStream(Stream inputStream, out BlobKey outKey)
        {
            FilePath tmp = null;
            try
            {
                tmp = FilePath.CreateTempFile(TmpFilePrefix, TmpFileExtension, new FilePath(this.path));
                FileOutputStream fos = new FileOutputStream(tmp);
                byte[] buffer = new byte[65536];
                int lenRead = ((InputStream)inputStream).Read(buffer);
                while (lenRead > 0)
                {
                    fos.Write(buffer, 0, lenRead);
                    lenRead = ((InputStream)inputStream).Read(buffer);
                }
                inputStream.Close();
                fos.Close();
            }
            catch (IOException e)
            {
                Log.E(Database.Tag, "Error writing blog to tmp file", e);
                outKey = null;
                return false;
            }

            outKey = KeyForBlobFromFile(tmp);
            var keyPath = PathForKey(outKey);
            var file = new FilePath(keyPath);
            if (file.CanRead())
            {
                // object with this hash already exists, we should delete tmp file and return true
                tmp.Delete();
            }
            else
            {
                // does not exist, we should rename tmp file to this name
                tmp.RenameTo(file);
            }

            return true;
        }
 public virtual bool IsGZipped(BlobKey key)
 {
     int magic = 0;
     string path = PathForKey(key);
     FilePath file = new FilePath(path);
     if (file.CanRead())
     {
         try
         {
             RandomAccessFile raf = new RandomAccessFile(file, "r");
             magic = raf.Read() & unchecked((int)(0xff)) | ((raf.Read() << 8) & unchecked((int
                 )(0xff00)));
             raf.Close();
         }
         catch (Exception e)
         {
             Sharpen.Runtime.PrintStackTrace(e, System.Console.Error);
         }
     }
     return magic == GZIPInputStream.GzipMagic;
 }
 public virtual InputStream BlobStreamForKey(BlobKey key)
 {
     string path = PathForKey(key);
     FilePath file = new FilePath(path);
     if (file.CanRead())
     {
         try
         {
             return new FileInputStream(file);
         }
         catch (FileNotFoundException e)
         {
             Log.E(Log.TagBlobStore, "Unexpected file not found in blob store", e);
             return null;
         }
     }
     return null;
 }
        public bool IsGZipped(BlobKey key)
        {
            var magic = 0;
            var path = PathForKey(key);
            var file = new FilePath(path);
            if (file.CanRead()) {
                try {
                    var raf = new FileStream (file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    magic = raf.ReadByte() & unchecked((0xff)) | ((raf.ReadByte() << 8) & unchecked((0xff00)));
                    raf.Close();
                }
                catch (Exception e) {
                    Runtime.PrintStackTrace(e, Console.Error);
                }
            }

            return magic == 0;
        }
Exemple #8
0
		/// <exception cref="System.IO.IOException"></exception>
		private static string ReadUrl(string filePath, string charCoding, bool urlIsFile)
		{
			int chunkLength;
			Stream @is = null;
			try
			{
				if (!urlIsFile)
				{
					Uri urlObj = new Uri(filePath);
					URLConnection uc = urlObj.OpenConnection();
					@is = uc.GetInputStream();
					chunkLength = uc.GetContentLength();
					if (chunkLength <= 0)
					{
						chunkLength = 1024;
					}
					if (charCoding == null)
					{
						string type = uc.GetContentType();
						if (type != null)
						{
							charCoding = GetCharCodingFromType(type);
						}
					}
				}
				else
				{
					FilePath f = new FilePath(filePath);
					if (!f.Exists())
					{
						throw new FileNotFoundException("File not found: " + filePath);
					}
					else
					{
						if (!f.CanRead())
						{
							throw new IOException("Cannot read file: " + filePath);
						}
					}
					long length = f.Length();
					chunkLength = (int)length;
					if (chunkLength != length)
					{
						throw new IOException("Too big file size: " + length);
					}
					if (chunkLength == 0)
					{
						return string.Empty;
					}
					@is = new FileInputStream(f);
				}
				TextReader r;
				if (charCoding == null)
				{
					r = new StreamReader(@is);
				}
				else
				{
					r = new StreamReader(@is, charCoding);
				}
				return ReadReader(r, chunkLength);
			}
			finally
			{
				if (@is != null)
				{
					@is.Close();
				}
			}
		}
		public Stream BlobStreamForKey(BlobKey key)
		{
			var path = PathForKey(key);
			var file = new FilePath(path);
            if (file.CanRead())
			{
				try
				{
					#if PORTABLE
					return File.OpenStream(file, false);
					#else
                    return new FileStream(file, FileMode.Open);
					#endif
				}
				catch (FileNotFoundException e)
				{
					Log.E(Database.Tag, "Unexpected file not found in blob store", e);
					return null;
				}
			}
			return null;
		}