This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-vfs.git
commit 650f382b4c1611fbd2a38033f66cac6e5a628a2d Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Mon Aug 19 14:55:42 2019 -0700 Use try-with-resources. --- .../apache/commons/vfs2/tasks/ShowFileTask.java | 278 +++++++++++---------- 1 file changed, 140 insertions(+), 138 deletions(-) diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/tasks/ShowFileTask.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/tasks/ShowFileTask.java index 14a86bb..885b548 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/tasks/ShowFileTask.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/tasks/ShowFileTask.java @@ -1,138 +1,140 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.vfs2.tasks; - -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.Date; - -import org.apache.commons.vfs2.FileContent; -import org.apache.commons.vfs2.FileObject; -import org.apache.tools.ant.BuildException; - -/** - * An Ant task that writes the details of a file to Ant's log. - */ -public class ShowFileTask extends VfsTask { - - private static final String INDENT = " "; - private String url; - private boolean showContent; - private boolean recursive; - - /** - * The URL of the file to display. - * - * @param url The url of the file. - */ - public void setFile(final String url) { - this.url = url; - } - - /** - * Shows the content. Assumes the content is text, encoded using the platform's default encoding. - * - * @param showContent true if the content should be shown. - */ - public void setShowContent(final boolean showContent) { - this.showContent = showContent; - } - - /** - * Recursively shows the descendants of the file. - * - * @param recursive true if descendants should be shown. - */ - public void setRecursive(final boolean recursive) { - this.recursive = recursive; - } - - /** - * Executes the task. - * - * @throws BuildException if any exception is thrown. - */ - @Override - public void execute() throws BuildException { - try { - final FileObject file = resolveFile(url); - log("Details of " + file.getPublicURIString()); - showFile(file, INDENT); - } catch (final Exception e) { - throw new BuildException(e); - } - } - - /** - * Logs the details of a file. - */ - private void showFile(final FileObject file, final String prefix) throws Exception { - // Write details - final StringBuilder msg = new StringBuilder(prefix); - msg.append(file.getName().getBaseName()); - if (file.exists()) { - msg.append(" ("); - msg.append(file.getType().getName()); - msg.append(")"); - } else { - msg.append(" (unknown)"); - } - log(msg.toString()); - - if (file.exists()) { - final String newPrefix = prefix + INDENT; - if (file.getType().hasContent()) { - final FileContent content = file.getContent(); - log(newPrefix + "Content-Length: " + content.getSize()); - log(newPrefix + "Last-Modified" + new Date(content.getLastModifiedTime())); - if (showContent) { - log(newPrefix + "Content:"); - logContent(file, newPrefix); - } - } - if (file.getType().hasChildren()) { - final FileObject[] children = file.getChildren(); - for (final FileObject child : children) { - if (recursive) { - showFile(child, newPrefix); - } else { - log(newPrefix + child.getName().getBaseName()); - } - } - } - } - } - - /** - * Writes the content of the file to Ant log. - */ - private void logContent(final FileObject file, final String prefix) throws Exception { - try ( - final InputStream instr = file.getContent().getInputStream(); - final BufferedReader reader = new BufferedReader(new InputStreamReader(instr)); ) - { - while (true) { - final String line = reader.readLine(); - if (line == null) { - break; - } - log(prefix + line); - } - } - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.vfs2.tasks; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Date; + +import org.apache.commons.vfs2.FileContent; +import org.apache.commons.vfs2.FileObject; +import org.apache.tools.ant.BuildException; + +/** + * An Ant task that writes the details of a file to Ant's log. + */ +public class ShowFileTask extends VfsTask { + + private static final String INDENT = " "; + private String url; + private boolean showContent; + private boolean recursive; + + /** + * The URL of the file to display. + * + * @param url The url of the file. + */ + public void setFile(final String url) { + this.url = url; + } + + /** + * Shows the content. Assumes the content is text, encoded using the platform's default encoding. + * + * @param showContent true if the content should be shown. + */ + public void setShowContent(final boolean showContent) { + this.showContent = showContent; + } + + /** + * Recursively shows the descendants of the file. + * + * @param recursive true if descendants should be shown. + */ + public void setRecursive(final boolean recursive) { + this.recursive = recursive; + } + + /** + * Executes the task. + * + * @throws BuildException if any exception is thrown. + */ + @Override + public void execute() throws BuildException { + try { + try (final FileObject file = resolveFile(url)) { + log("Details of " + file.getPublicURIString()); + showFile(file, INDENT); + } + } catch (final Exception e) { + throw new BuildException(e); + } + } + + /** + * Logs the details of a file. + */ + private void showFile(final FileObject file, final String prefix) throws Exception { + // Write details + final StringBuilder msg = new StringBuilder(prefix); + msg.append(file.getName().getBaseName()); + if (file.exists()) { + msg.append(" ("); + msg.append(file.getType().getName()); + msg.append(")"); + } else { + msg.append(" (unknown)"); + } + log(msg.toString()); + + if (file.exists()) { + final String newPrefix = prefix + INDENT; + if (file.getType().hasContent()) { + try (final FileContent content = file.getContent()) { + log(newPrefix + "Content-Length: " + content.getSize()); + log(newPrefix + "Last-Modified" + new Date(content.getLastModifiedTime())); + } + if (showContent) { + log(newPrefix + "Content:"); + logContent(file, newPrefix); + } + } + if (file.getType().hasChildren()) { + final FileObject[] children = file.getChildren(); + for (final FileObject child : children) { + if (recursive) { + showFile(child, newPrefix); + } else { + log(newPrefix + child.getName().getBaseName()); + } + } + } + } + } + + /** + * Writes the content of the file to Ant log. + */ + private void logContent(final FileObject file, final String prefix) throws Exception { + try ( + final InputStream instr = file.getContent().getInputStream(); + final BufferedReader reader = new BufferedReader(new InputStreamReader(instr)); ) + { + while (true) { + final String line = reader.readLine(); + if (line == null) { + break; + } + log(prefix + line); + } + } + } +}