예제 #1
0
        /// <summary>
        /// Visits the given file, returning the {@code Event} corresponding to that
        /// visit.
        ///
        /// The {@code ignoreSecurityException} parameter determines whether
        /// any SecurityException should be ignored or not. If a SecurityException
        /// is thrown, and is ignored, then this method returns {@code null} to
        /// mean that there is no event corresponding to a visit to the file.
        ///
        /// The {@code canUseCached} parameter determines whether cached attributes
        /// for the file can be used or not.
        /// </summary>
        private Event Visit(Path entry, bool ignoreSecurityException, bool canUseCached)
        {
            // need the file attributes
            BasicFileAttributes attrs;

            try
            {
                attrs = GetAttributes(entry, canUseCached);
            }
            catch (IOException ioe)
            {
                return(new Event(EventType.ENTRY, entry, ioe));
            }
            catch (SecurityException se)
            {
                if (ignoreSecurityException)
                {
                    return(null);
                }
                throw se;
            }

            // at maximum depth or file is not a directory
            int depth = Stack.Size();

            if (depth >= MaxDepth || !attrs.Directory)
            {
                return(new Event(EventType.ENTRY, entry, attrs));
            }

            // check for cycles when following links
            if (FollowLinks && WouldLoop(entry, attrs.FileKey()))
            {
                return(new Event(EventType.ENTRY, entry, new FileSystemLoopException(entry.ToString())));
            }

            // file is a directory, attempt to open it
            DirectoryStream <Path> stream = null;

            try
            {
                stream = Files.NewDirectoryStream(entry);
            }
            catch (IOException ioe)
            {
                return(new Event(EventType.ENTRY, entry, ioe));
            }
            catch (SecurityException se)
            {
                if (ignoreSecurityException)
                {
                    return(null);
                }
                throw se;
            }

            // push a directory node to the stack and return an event
            Stack.Push(new DirectoryNode(entry, attrs.FileKey(), stream));
            return(new Event(EventType.START_DIRECTORY, entry, attrs));
        }
예제 #2
0
 internal DirectoryNode(Path dir, Object key, DirectoryStream <Path> stream)
 {
     this.Dir              = dir;
     this.Key_Renamed      = key;
     this.Stream_Renamed   = stream;
     this.Iterator_Renamed = stream.Iterator();
 }
        public void LengthTest()
        {
            //arrange
            DirectoryStream ds = new DirectoryStream("TestDirectory");
            //act
            var length = ds.Length;

            //assert
            Assert.AreEqual(14L, length);
        }
        public void ReadByteTestUpdatesPosition()
        {
            //arrange
            DirectoryStream ds = new DirectoryStream("TestDirectory");

            //act
            ds.ReadByte();
            //assert
            Assert.AreEqual(1, ds.Position);
        }
        public void ReadByteTest()
        {
            //arrange
            DirectoryStream ds = new DirectoryStream("TestDirectory");
            //act
            byte b = (byte)ds.ReadByte();

            //assert
            Assert.AreEqual((byte)'0', b);
        }
        public void ReadByteTestAtPosition()
        {
            //arrange
            DirectoryStream ds = new DirectoryStream("TestDirectory");

            ds.Position = 5;
            byte b = (byte)ds.ReadByte();

            //assert
            Assert.AreEqual((byte)'A', b);
        }
        public void ReadTestDoesntGoBeyondFiles()
        {
            //arrange
            DirectoryStream ds = new DirectoryStream("TestDirectory");

            byte[] buffer = new byte[32];
            //act
            int read = ds.Read(buffer, 0, 32);

            //assert
            Assert.AreEqual(14, read);
        }
        public void ReadTestUpdatesPosition()
        {
            //arrange
            DirectoryStream ds = new DirectoryStream("TestDirectory");

            byte[] buffer = new byte[4];
            ds.Position = 4;
            //act
            ds.Read(buffer, 0, 4);
            //assert
            Assert.AreEqual(8, ds.Position);
        }
        public void ReadTest()
        {
            //arrange
            DirectoryStream ds = new DirectoryStream("TestDirectory");

            byte[] data = new byte[14];
            //act
            ds.Read(data, 0, 14);
            //assert
            byte[] expected = Encoding.ASCII.GetBytes("0000AABBBCCCCE");
            CollectionAssert.AreEqual(expected, data);
        }
        public void SeekTestBegin()
        {
            //arrange
            DirectoryStream ds = new DirectoryStream("TestDirectory");

            //act
            ds.Seek(7, System.IO.SeekOrigin.Begin);
            byte b = (byte)ds.ReadByte();

            //assert
            Assert.AreEqual((byte)'B', b);
        }
        public void SeekTestCurrent()
        {
            //arrange
            DirectoryStream ds = new DirectoryStream("TestDirectory");

            ds.Position = 4;
            //act
            ds.Seek(1, System.IO.SeekOrigin.Current);
            byte b = (byte)ds.ReadByte();

            //assert
            Assert.AreEqual((byte)'A', b);
        }
        public void ReadTestAtFileBoundary()
        {
            //arrange
            var       ds          = new DirectoryStream("TestDirSameSize");
            const int bytesToRead = 10;

            byte[] buffer   = new byte[bytesToRead];
            byte[] expected = Encoding.ASCII.GetBytes("2222222222");
            ds.Seek(10, SeekOrigin.Begin);
            //act
            int read = ds.Read(buffer, 0, bytesToRead);

            //assert
            Assert.AreEqual(bytesToRead, read);
            CollectionAssert.AreEqual(expected, buffer);
        }
        public void ReadTestWithOffset()
        {
            //arrange
            var       ds          = new DirectoryStream("TestDirSameSize");
            const int bytesToRead = 20;

            byte[] buffer   = new byte[bytesToRead];
            byte[] expected = Encoding.ASCII.GetBytes("11222222222233333333");
            ds.Position = 8;
            //act
            int read = ds.Read(buffer, 0, bytesToRead);

            //assert
            Assert.AreEqual(bytesToRead, read);
            CollectionAssert.AreEqual(expected, buffer);
        }
        public void ReadTestLargeFileFromStreamHasTheSameContentAsFileOnDisk()
        {
            //arrange
            int             size = 1024 * 1024 * 2;
            DirectoryStream ds   = new DirectoryStream("TestDirLarge");

            byte[] buffer   = new byte[size];
            byte[] expected = new byte[size];
            using (var fs = File.Open(@"TestDirLarge\file0.bin", FileMode.Open))
            {
                fs.Read(expected, 0, size);
            }
            //act
            ds.Read(buffer, 0, size);
            //assert
            CollectionAssert.AreEqual(expected, buffer);
        }
예제 #15
0
        /// <summary>
        /// Check if directory is empty.
        /// </summary>
        /// <param name="directory"> - directory to check </param>
        /// <returns> false if directory exists and empty, true otherwise. </returns>
        /// <exception cref="IllegalArgumentException"> if specified directory represent a file </exception>
        /// <exception cref="IOException"> if some problem encountered during reading directory content </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static boolean isEmptyDirectory(java.io.File directory) throws java.io.IOException
        public static bool IsEmptyDirectory(File directory)
        {
            if (directory.exists())
            {
                if (!directory.Directory)
                {
                    throw new System.ArgumentException("Expected directory, but was file: " + directory);
                }
                else
                {
                    using (DirectoryStream <Path> directoryStream = Files.newDirectoryStream(directory.toPath()))
                    {
                        return(!directoryStream.GetEnumerator().hasNext());
                    }
                }
            }
            return(true);
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void copyRecursively(java.nio.file.Path source, java.nio.file.Path target) throws java.io.IOException
        private void CopyRecursively(Path source, Path target)
        {
            using (DirectoryStream <Path> directoryStream = Files.newDirectoryStream(source))
            {
                foreach (Path sourcePath in directoryStream)
                {
                    Path targetPath = target.resolve(sourcePath.FileName);
                    if (Files.isDirectory(sourcePath))
                    {
                        Files.createDirectories(targetPath);
                        CopyRecursively(sourcePath, targetPath);
                    }
                    else
                    {
                        Files.copy(sourcePath, targetPath, REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
                    }
                }
            }
        }
예제 #17
0
        /// <summary>
        /// Return the complete list of files in a directory as strings.<p/>
        /// This is better than File#listDir because it does not ignore IOExceptions.
        /// </summary>
        /// <param name="dir">The directory to list.</param>
        /// <param name="filter">
        /// If non-null, the filter to use when listing
        /// this directory.
        /// </param>
        /// <returns>The list of files in the directory.</returns>
        /// <exception cref="System.IO.IOException">On I/O error</exception>
        public static IList <string> ListDirectory(FilePath dir, FilenameFilter filter)
        {
            AList <string> list = new AList <string>();

            try
            {
                using (DirectoryStream <Path> stream = Files.NewDirectoryStream(dir.ToPath()))
                {
                    foreach (Path entry in stream)
                    {
                        string fileName = entry.GetFileName().ToString();
                        if ((filter == null) || filter.Accept(dir, fileName))
                        {
                            list.AddItem(fileName);
                        }
                    }
                }
            }
            catch (DirectoryIteratorException e)
            {
                throw ((IOException)e.InnerException);
            }
            return(list);
        }
 private EstimationResult EstimateDirectory(DirectoryInfo arg)
 {
     try
     {
         logger.Log("Starting: " + arg.Name);
         DirectoryStream ds         = new DirectoryStream(arg.FullName);
         StreamEstimator se         = new StreamEstimator();
         var             packedSize = se.EstimatePackedSize(ds, MAX_READ_SIZE, BLOCK_SIZE);
         var             result     = new EstimationResult()
         {
             Directory      = arg.FullName,
             ShortName      = arg.Name,
             OriginalSize   = ds.Length,
             EstimatedSize  = packedSize,
             NtfsCompressed = arg.IsCompressed()
         };
         logger.Log("Finished: " + arg.Name + " - ratio: " + result.CompressionRatio);
         return(result);
     }
     catch (Exception e)
     {
         return(new EstimationResult());
     }
 }