Example #1
0
        public bool IsGZipped(BlobKey key)
        {
            int      magic = 0;
            string   path  = PathForKey(key);
            FilePath 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)
                {
                                        #if PORTABLE
                    Runtime.PrintStackTrace(e);
                                        #else
                    Runtime.PrintStackTrace(e, Console.Error);
                                        #endif
                }
            }
            return(magic == GZIPInputStream.GzipMagic);
        }
Example #2
0
        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);
        }
Example #3
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);
        }
Example #4
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestCreateLocalTempFile()
        {
            SetupDirs();
            FilePath baseFile = new FilePath(tmp, "base");
            FilePath tmp1     = FileUtil.CreateLocalTempFile(baseFile, "foo", false);
            FilePath tmp2     = FileUtil.CreateLocalTempFile(baseFile, "foo", true);

            NUnit.Framework.Assert.IsFalse(tmp1.GetAbsolutePath().Equals(baseFile.GetAbsolutePath
                                                                             ()));
            NUnit.Framework.Assert.IsFalse(tmp2.GetAbsolutePath().Equals(baseFile.GetAbsolutePath
                                                                             ()));
            Assert.True(tmp1.Exists() && tmp2.Exists());
            Assert.True(tmp1.CanWrite() && tmp2.CanWrite());
            Assert.True(tmp1.CanRead() && tmp2.CanRead());
            tmp1.Delete();
            tmp2.Delete();
            Assert.True(!tmp1.Exists() && !tmp2.Exists());
        }
Example #5
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 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(Database.Tag, "Unexpected file not found in blob store", e);
                    return(null);
                }
            }
            return(null);
        }
Example #7
0
        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);
        }
        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 == 0);
        }
Example #9
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void Read([NotNull] FilePath file, [NotNull] Com.Drew.Metadata.Metadata metadata)
        {
            if (!file.IsFile())
            {
                throw new IOException("File object must reference a file");
            }
            if (!file.Exists())
            {
                throw new IOException("File does not exist");
            }
            if (!file.CanRead())
            {
                throw new IOException("File is not readable");
            }
            FileMetadataDirectory directory = new FileMetadataDirectory();

            directory.SetString(FileMetadataDirectory.TagFileName, file.GetName());
            directory.SetLong(FileMetadataDirectory.TagFileSize, file.Length());
            directory.SetDate(FileMetadataDirectory.TagFileModifiedDate, Sharpen.Extensions.CreateDate(file.LastModified()));
            metadata.AddDirectory(directory);
        }
        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);
        }
Example #11
0
        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);
        }