Esempio n. 1
0
        public bool GetKeyForFilename(BlobKey outKey, string filename)
        {
            if (!filename.EndsWith(FileExtension))
            {
                return(false);
            }
            //trim off extension
            string rest = Sharpen.Runtime.Substring(filename, path.Length + 1, filename.Length
                                                    - FileExtension.Length);

            outKey.SetBytes(BlobKey.ConvertFromHex(rest));
            return(true);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        public bool StoreBlobStream(InputStream inputStream, 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.Read(buffer);
                while (lenRead > 0)
                {
                    fos.Write(buffer, 0, lenRead);
                    lenRead = inputStream.Read(buffer);
                }
                inputStream.Close();
                fos.Close();
            }
            catch (IOException e)
            {
                Log.E(Database.Tag, "Error writing blog to tmp file", e);
                return(false);
            }
            BlobKey newKey = KeyForBlobFromFile(tmp);

            outKey.SetBytes(newKey.GetBytes());
            string   path = PathForKey(outKey);
            FilePath file = new FilePath(path);

            if (file.CanRead())
            {
                // object with this hash already exists, we should delete tmp file and return true
                tmp.Delete();
                return(true);
            }
            else
            {
                // does not exist, we should rename tmp file to this name
                tmp.RenameTo(file);
            }
            return(true);
        }
 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 GetKeyForFilename(BlobKey outKey, string filename)
 {
     if (!filename.EndsWith(FileExtension))
     {
         return false;
     }
     //trim off extension
     string rest = Sharpen.Runtime.Substring(filename, path.Length + 1, filename.Length
          - FileExtension.Length);
     outKey.SetBytes(BlobKey.ConvertFromHex(rest));
     return true;
 }
 public virtual bool StoreBlobStream(InputStream inputStream, BlobKey outKey)
 {
     FilePath tmp = null;
     try
     {
         tmp = FilePath.CreateTempFile(TmpFilePrefix, TmpFileExtension, new FilePath(path)
             );
         FileOutputStream fos = new FileOutputStream(tmp);
         byte[] buffer = new byte[65536];
         int lenRead = inputStream.Read(buffer);
         while (lenRead > 0)
         {
             fos.Write(buffer, 0, lenRead);
             lenRead = inputStream.Read(buffer);
         }
         inputStream.Close();
         fos.Close();
     }
     catch (IOException e)
     {
         Log.E(Log.TagBlobStore, "Error writing blog to tmp file", e);
         return false;
     }
     BlobKey newKey = KeyForBlobFromFile(tmp);
     outKey.SetBytes(newKey.GetBytes());
     string path = PathForKey(outKey);
     FilePath file = new FilePath(path);
     if (file.CanRead())
     {
         // object with this hash already exists, we should delete tmp file and return true
         tmp.Delete();
         return true;
     }
     else
     {
         // does not exist, we should rename tmp file to this name
         tmp.RenameTo(file);
     }
     return true;
 }