<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.velocity.tools.nio;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.util.List;
import java.util.Map;

/**
 * Provides access to some of the static NIO2 Files methods.
 *
 * @version $Id$
 * @since 7.4M2
 */
public class NIOTool
{
    /**
     * See {@link Files#newDirectoryStream(Path)}.
     * <p>
     * Velocity Example:
     * <pre>{@code
     * #set ($dirStream = $niotool.newDirectoryStream("attach:Sandbox.WebHome@vma.txt.zip/"))
     * #foreach ($entry in $dirStream)
     *   * {{{$entry}}}
     * #end
     * }</pre>
     *
     * @param dir See {@link Files#newDirectoryStream(Path)}
     * @return See {@link Files#newDirectoryStream(Path)}
     */
    public DirectoryStream<Path> newDirectoryStream(Path dir)
    {
        try {
            return new WrappingDirectoryStream(Files.newDirectoryStream(dir));
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * See {@link Files#newDirectoryStream(Path, DirectoryStream.Filter)}.
     *
     * @param dir See {@link Files#newDirectoryStream(Path, DirectoryStream.Filter)}
     * @param filter See {@link Files#newDirectoryStream(Path, DirectoryStream.Filter)}
     * @return See {@link Files#newDirectoryStream(Path, DirectoryStream.Filter)}
     */
    public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter filter)
    {
        try {
            return new WrappingDirectoryStream(Files.newDirectoryStream(dir, filter));
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * See {@link Files#newDirectoryStream(Path, String)}.
     *
     * @param dir See {@link Files#newDirectoryStream(Path, String)}
     * @param glob See {@link Files#newDirectoryStream(Path, String)}
     * @return See {@link Files#newDirectoryStream(Path, String)}
     */
    public DirectoryStream<Path> newDirectoryStream(Path dir, String glob)
    {
        try {
            return new WrappingDirectoryStream(Files.newDirectoryStream(dir, glob));
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * See {@link Files#isDirectory(Path, LinkOption...)}.
     *
     * @param path See {@link Files#isDirectory(Path, LinkOption...)}
     * @param options See {@link Files#isDirectory(Path, LinkOption...)}
     * @return See {@link Files#isDirectory(Path, LinkOption...)}
     */
    public boolean isDirectory(Path path, LinkOption... options)
    {
        return Files.isDirectory(path, options);
    }

    /**
     * See {@link Files#isRegularFile(Path, LinkOption...)}.
     *
     * @param path See {@link Files#isRegularFile(Path, LinkOption...)}
     * @param options See {@link Files#isRegularFile(Path, LinkOption...)}
     * @return See {@link Files#isRegularFile(Path, LinkOption...)}
     */
    public boolean isRegularFile(Path path, LinkOption... options)
    {
        return Files.isRegularFile(path, options);
    }

    /**
     * See {@link Files#isReadable(Path)}.
     *
     * @param path See {@link Files#isReadable(Path)}
     * @return See {@link Files#isReadable(Path)}
     */
    public boolean isReadable(Path path)
    {
        return Files.isReadable(path);
    }

    /**
     * See {@link Files#isSymbolicLink(Path)}.
     *
     * @param path See {@link Files#isSymbolicLink(Path)}
     * @return See {@link Files#isSymbolicLink(Path)}
     */
    public boolean isSymbolicLink(Path path)
    {
        return Files.isSymbolicLink(path);
    }

    /**
     * See {@link Files#exists(Path, LinkOption...)}.
     *
     * @param path See {@link Files#exists(Path, LinkOption...)}
     * @param options See {@link Files#exists(Path, LinkOption...)}
     * @return See {@link Files#exists(Path, LinkOption...)}
     */
    public boolean exists(Path path, LinkOption... options)
    {
        return Files.exists(path, options);
    }

    /**
     * See {@link Files#readAllBytes(Path)}.
     * <p>
     * Velocity Example:
     * </p>
     * <code><pre>
     * $stringtool.toString($niotool.readAllBytes("attach:Sandbox.WebHome@vma.txt.zip/vma.txt"), "utf-8")
     * </pre></code>
     *
     * @param path See {@link Files#readAllBytes(Path)}
     * @return See {@link Files#readAllBytes(Path)}
     */
    public byte[] readAllBytes(Path path)
    {
        try {<commit_msg>[Misc] Fix javadoc<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.velocity.tools.nio;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.util.List;
import java.util.Map;

/**
 * Provides access to some of the static NIO2 Files methods.
 *
 * @version $Id$
 * @since 7.4M2
 */
public class NIOTool
{
    /**
     * See {@link Files#newDirectoryStream(Path)}.
     * <p>
     * Velocity Example:
     * <pre>{@code
     * #set ($dirStream = $niotool.newDirectoryStream("attach:Sandbox.WebHome@vma.txt.zip/"))
     * #foreach ($entry in $dirStream)
     *   * {{{$entry}}}
     * #end
     * }</pre>
     *
     * @param dir See {@link Files#newDirectoryStream(Path)}
     * @return See {@link Files#newDirectoryStream(Path)}
     */
    public DirectoryStream<Path> newDirectoryStream(Path dir)
    {
        try {
            return new WrappingDirectoryStream(Files.newDirectoryStream(dir));
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * See {@link Files#newDirectoryStream(Path, DirectoryStream.Filter)}.
     *
     * @param dir See {@link Files#newDirectoryStream(Path, DirectoryStream.Filter)}
     * @param filter See {@link Files#newDirectoryStream(Path, DirectoryStream.Filter)}
     * @return See {@link Files#newDirectoryStream(Path, DirectoryStream.Filter)}
     */
    public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter filter)
    {
        try {
            return new WrappingDirectoryStream(Files.newDirectoryStream(dir, filter));
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * See {@link Files#newDirectoryStream(Path, String)}.
     *
     * @param dir See {@link Files#newDirectoryStream(Path, String)}
     * @param glob See {@link Files#newDirectoryStream(Path, String)}
     * @return See {@link Files#newDirectoryStream(Path, String)}
     */
    public DirectoryStream<Path> newDirectoryStream(Path dir, String glob)
    {
        try {
            return new WrappingDirectoryStream(Files.newDirectoryStream(dir, glob));
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * See {@link Files#isDirectory(Path, LinkOption...)}.
     *
     * @param path See {@link Files#isDirectory(Path, LinkOption...)}
     * @param options See {@link Files#isDirectory(Path, LinkOption...)}
     * @return See {@link Files#isDirectory(Path, LinkOption...)}
     */
    public boolean isDirectory(Path path, LinkOption... options)
    {
        return Files.isDirectory(path, options);
    }

    /**
     * See {@link Files#isRegularFile(Path, LinkOption...)}.
     *
     * @param path See {@link Files#isRegularFile(Path, LinkOption...)}
     * @param options See {@link Files#isRegularFile(Path, LinkOption...)}
     * @return See {@link Files#isRegularFile(Path, LinkOption...)}
     */
    public boolean isRegularFile(Path path, LinkOption... options)
    {
        return Files.isRegularFile(path, options);
    }

    /**
     * See {@link Files#isReadable(Path)}.
     *
     * @param path See {@link Files#isReadable(Path)}
     * @return See {@link Files#isReadable(Path)}
     */
    public boolean isReadable(Path path)
    {
        return Files.isReadable(path);
    }

    /**
     * See {@link Files#isSymbolicLink(Path)}.
     *
     * @param path See {@link Files#isSymbolicLink(Path)}
     * @return See {@link Files#isSymbolicLink(Path)}
     */
    public boolean isSymbolicLink(Path path)
    {
        return Files.isSymbolicLink(path);
    }

    /**
     * See {@link Files#exists(Path, LinkOption...)}.
     *
     * @param path See {@link Files#exists(Path, LinkOption...)}
     * @param options See {@link Files#exists(Path, LinkOption...)}
     * @return See {@link Files#exists(Path, LinkOption...)}
     */
    public boolean exists(Path path, LinkOption... options)
    {
        return Files.exists(path, options);
    }

    /**
     * See {@link Files#readAllBytes(Path)}.
     * <p>
     * Velocity Example:
     * <pre>{@code
     * $stringtool.toString($niotool.readAllBytes("attach:Sandbox.WebHome@vma.txt.zip/vma.txt"), "utf-8")
     * }</pre>
     *
     * @param path See {@link Files#readAllBytes(Path)}
     * @return See {@link Files#readAllBytes(Path)}
     */
    public byte[] readAllBytes(Path path)
    {
        try {
{'fraction_non_alphanumeric': 0.1279294182519989, 'fraction_numerical': 0.0033085194375516956, 'mean_word_length': 2.8753561253561255, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5049029', 'n_tokens_mistral': 3211, 'n_tokens_neox': 3162, 'n_words': 879}