From 700c90d15fdb626a1470b88299051b82060519ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BF=8A=20SionYang?= <263976490@qq.com> Date: Wed, 16 Sep 2020 12:33:33 +0800 Subject: [PATCH] Fix code style problem in DiskUtils (#3842) --- .../alibaba/nacos/core/utils/DiskUtils.java | 141 +++++++----------- style/nacos-code-style-for-idea.xml | 1 + 2 files changed, 57 insertions(+), 85 deletions(-) diff --git a/core/src/main/java/com/alibaba/nacos/core/utils/DiskUtils.java b/core/src/main/java/com/alibaba/nacos/core/utils/DiskUtils.java index cd9c33e5dd3..5a7c4b2550b 100644 --- a/core/src/main/java/com/alibaba/nacos/core/utils/DiskUtils.java +++ b/core/src/main/java/com/alibaba/nacos/core/utils/DiskUtils.java @@ -91,75 +91,48 @@ public static void touch(File file) throws IOException { } /** - * Creates a new empty file in the specified directory, using the given - * prefix and suffix strings to generate its name. The resulting - * {@code Path} is associated with the same {@code FileSystem} as the given - * directory. + * Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its + * name. The resulting {@code Path} is associated with the same {@code FileSystem} as the given directory. * *

The details as to how the name of the file is constructed is - * implementation dependent and therefore not specified. Where possible - * the {@code prefix} and {@code suffix} are used to construct candidate - * names in the same manner as the {@link - * java.io.File#createTempFile(String,String,File)} method. + * implementation dependent and therefore not specified. Where possible the {@code prefix} and {@code suffix} are + * used to construct candidate names in the same manner as the {@link java.io.File#createTempFile(String, String, File)} + * method. * - * - * @param dir - * the path to directory in which to create the file - * @param prefix - * the prefix string to be used in generating the file's name; - * may be {@code null} - * @param suffix - * the suffix string to be used in generating the file's name; - * may be {@code null}, in which case "{@code .tmp}" is used - * - * @return the path to the newly created file that did not exist before - * this method was invoked - * - * @throws IllegalArgumentException - * if the prefix or suffix parameters cannot be used to generate - * a candidate file name - * @throws UnsupportedOperationException - * if the array contains an attribute that cannot be set atomically - * when creating the directory - * @throws IOException - * if an I/O error occurs or {@code dir} does not exist - * @throws SecurityException - * In the case of the default provider, and a security manager is - * installed, the {@link SecurityManager#checkWrite(String) checkWrite} - * method is invoked to check write access to the file. + * @param dir the path to directory in which to create the file + * @param prefix the prefix string to be used in generating the file's name; may be {@code null} + * @param suffix the suffix string to be used in generating the file's name; may be {@code null}, in which case + * "{@code .tmp}" is used + * @return the path to the newly created file that did not exist before this method was invoked + * @throws IllegalArgumentException if the prefix or suffix parameters cannot be used to generate a candidate + * file name + * @throws UnsupportedOperationException if the array contains an attribute that cannot be set atomically when + * creating the directory + * @throws IOException if an I/O error occurs or {@code dir} does not exist + * @throws SecurityException In the case of the default provider, and a security manager is installed, + * the {@link SecurityManager#checkWrite(String) checkWrite} method is invoked + * to check write access to the file. */ public static File createTmpFile(String dir, String prefix, String suffix) throws IOException { return Files.createTempFile(Paths.get(dir), prefix, suffix).toFile(); } /** - * Creates an empty file in the default temporary-file directory, using - * the given prefix and suffix to generate its name. The resulting {@code - * Path} is associated with the default {@code FileSystem}. + * Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its + * name. The resulting {@code Path} is associated with the default {@code FileSystem}. * - * @param prefix - * the prefix string to be used in generating the file's name; - * may be {@code null} - * @param suffix - * the suffix string to be used in generating the file's name; - * may be {@code null}, in which case "{@code .tmp}" is used - * - * @return the path to the newly created file that did not exist before - * this method was invoked - * - * @throws IllegalArgumentException - * if the prefix or suffix parameters cannot be used to generate - * a candidate file name - * @throws UnsupportedOperationException - * if the array contains an attribute that cannot be set atomically - * when creating the directory - * @throws IOException - * if an I/O error occurs or the temporary-file directory does not - * exist - * @throws SecurityException - * In the case of the default provider, and a security manager is - * installed, the {@link SecurityManager#checkWrite(String) checkWrite} - * method is invoked to check write access to the file. + * @param prefix the prefix string to be used in generating the file's name; may be {@code null} + * @param suffix the suffix string to be used in generating the file's name; may be {@code null}, in which case + * "{@code .tmp}" is used + * @return the path to the newly created file that did not exist before this method was invoked + * @throws IllegalArgumentException if the prefix or suffix parameters cannot be used to generate a candidate + * file name + * @throws UnsupportedOperationException if the array contains an attribute that cannot be set atomically when + * creating the directory + * @throws IOException if an I/O error occurs or the temporary-file directory does not exist + * @throws SecurityException In the case of the default provider, and a security manager is installed, + * the {@link SecurityManager#checkWrite(String) checkWrite} method is invoked + * to check write access to the file. */ public static File createTmpFile(String prefix, String suffix) throws IOException { return Files.createTempFile(prefix, suffix).toFile(); @@ -380,8 +353,8 @@ public static File openFile(String path, String fileName, boolean rewrite) { */ public static void compress(final String rootDir, final String sourceDir, final String outputFile, final Checksum checksum) throws IOException { - try (final FileOutputStream fos = new FileOutputStream( - outputFile); final CheckedOutputStream cos = new CheckedOutputStream(fos, checksum); + try (final FileOutputStream fos = new FileOutputStream(outputFile); + final CheckedOutputStream cos = new CheckedOutputStream(fos, checksum); final ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(cos))) { compressDirectoryToZipFile(rootDir, sourceDir, zos); zos.flush(); @@ -401,8 +374,8 @@ private static void compressDirectoryToZipFile(final String rootDir, final Strin compressDirectoryToZipFile(rootDir, child, zos); } else { zos.putNextEntry(new ZipEntry(child)); - try (final FileInputStream fis = new FileInputStream( - file); final BufferedInputStream bis = new BufferedInputStream(fis)) { + try (final FileInputStream fis = new FileInputStream(file); + final BufferedInputStream bis = new BufferedInputStream(fis)) { IOUtils.copy(bis, zos); } } @@ -421,16 +394,16 @@ private static void compressDirectoryToZipFile(final String rootDir, final Strin */ public static void decompress(final String sourceFile, final String outputDir, final Checksum checksum) throws IOException { - try (final FileInputStream fis = new FileInputStream( - sourceFile); final CheckedInputStream cis = new CheckedInputStream(fis, checksum); + try (final FileInputStream fis = new FileInputStream(sourceFile); + final CheckedInputStream cis = new CheckedInputStream(fis, checksum); final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(cis))) { ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { final String fileName = entry.getName(); final File entryFile = new File(Paths.get(outputDir, fileName).toString()); FileUtils.forceMkdir(entryFile.getParentFile()); - try (final FileOutputStream fos = new FileOutputStream( - entryFile); final BufferedOutputStream bos = new BufferedOutputStream(fos)) { + try (final FileOutputStream fos = new FileOutputStream(entryFile); + final BufferedOutputStream bos = new BufferedOutputStream(fos)) { IOUtils.copy(zis, bos); bos.flush(); fos.getFD().sync(); @@ -447,11 +420,10 @@ public static void decompress(final String sourceFile, final String outputDir, f /** * Returns an Iterator for the lines in a File. *

- * This method opens an InputStream for the file. - * When you have finished with the iterator you should close the stream - * to free internal resources. This can be done by calling the - * {@link org.apache.commons.io.LineIterator#close()} or - * {@link org.apache.commons.io.LineIterator#closeQuietly(org.apache.commons.io.LineIterator)} method. + * This method opens an InputStream for the file. When you have finished with the iterator you should + * close the stream to free internal resources. This can be done by calling the {@link + * org.apache.commons.io.LineIterator#close()} or {@link org.apache.commons.io.LineIterator#closeQuietly(org.apache.commons.io.LineIterator)} + * method. *

* The recommended usage pattern is: *
@@ -466,12 +438,11 @@ public static void decompress(final String sourceFile, final String outputDir, f
      * }
      * 
*

- * If an exception occurs during the creation of the iterator, the - * underlying stream is closed. + * If an exception occurs during the creation of the iterator, the underlying stream is closed. *

* - * @param file the file to open for input, must not be null - * @param encoding the encoding to use, null means platform default + * @param file the file to open for input, must not be null + * @param encoding the encoding to use, null means platform default * @return an Iterator of the lines in the file, never null * @throws IOException in case of an I/O error (file closed) * @since 1.2 @@ -483,20 +454,20 @@ public static LineIterator lineIterator(File file, String encoding) throws IOExc /** * Returns an Iterator for the lines in a File using the default encoding for the VM. * - * @param file the file to open for input, must not be null + * @param file the file to open for input, must not be null * @return an Iterator of the lines in the file, never null * @throws IOException in case of an I/O error (file closed) - * @since 1.3 * @see #lineIterator(File, String) + * @since 1.3 */ public static LineIterator lineIterator(File file) throws IOException { return new LineIterator(FileUtils.lineIterator(file, null)); } public static class LineIterator implements AutoCloseable { - + private final org.apache.commons.io.LineIterator target; - + /** * Constructs an iterator of the lines for a Reader. * @@ -505,7 +476,7 @@ public static class LineIterator implements AutoCloseable { LineIterator(org.apache.commons.io.LineIterator target) { this.target = target; } - + public boolean hasNext() { return target.hasNext(); } @@ -513,20 +484,20 @@ public boolean hasNext() { public String next() { return target.next(); } - + public String nextLine() { return target.nextLine(); } - + @Override public void close() { target.close(); } - + public void remove() { target.remove(); } - + public void forEachRemaining(Consumer action) { target.forEachRemaining(action); } diff --git a/style/nacos-code-style-for-idea.xml b/style/nacos-code-style-for-idea.xml index 28846f8ae6b..6bd63a11332 100644 --- a/style/nacos-code-style-for-idea.xml +++ b/style/nacos-code-style-for-idea.xml @@ -139,6 +139,7 @@