langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/DocFile.java
author ksrini
Mon, 05 Jun 2017 13:45:34 -0700
changeset 45417 f7479ee8de69
parent 40587 1c355ea550ed
permissions -rw-r--r--
8177848: Rename Configuration(Impl) classes Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
     1
/*
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40587
diff changeset
     2
 * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
     4
 *
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    10
 *
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    15
 * accompanied this code).
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    16
 *
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    20
 *
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    23
 * questions.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    24
 */
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    25
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    26
package jdk.javadoc.internal.doclets.toolkit.util;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    27
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14544
diff changeset
    28
import java.io.BufferedReader;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    29
import java.io.IOException;
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    30
import java.io.InputStream;
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14544
diff changeset
    31
import java.io.InputStreamReader;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    32
import java.io.OutputStream;
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    33
import java.io.UnsupportedEncodingException;
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    34
import java.io.Writer;
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    35
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    36
import javax.tools.JavaFileManager.Location;
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    37
import javax.tools.StandardLocation;
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    38
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40587
diff changeset
    39
import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    40
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    41
/**
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    42
 * Abstraction for handling files, which may be specified directly
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    43
 * (e.g. via a path on the command line) or relative to a Location.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    44
 *
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    45
 *  <p><b>This is NOT part of any supported API.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    46
 *  If you write code that depends on this, you do so at your own risk.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    47
 *  This code and its internal interfaces are subject to change or
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    48
 *  deletion without notice.</b>
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    49
 *
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    50
 */
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    51
public abstract class DocFile {
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    52
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    53
    /** Create a DocFile for a directory. */
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40587
diff changeset
    54
    public static DocFile createFileForDirectory(BaseConfiguration configuration, String file) {
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    55
        return DocFileFactory.getFactory(configuration).createFileForDirectory(file);
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    56
    }
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    57
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    58
    /** Create a DocFile for a file that will be opened for reading. */
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40587
diff changeset
    59
    public static DocFile createFileForInput(BaseConfiguration configuration, String file) {
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    60
        return DocFileFactory.getFactory(configuration).createFileForInput(file);
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    61
    }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    62
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    63
    /** Create a DocFile for a file that will be opened for writing. */
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40587
diff changeset
    64
    public static DocFile createFileForOutput(BaseConfiguration configuration, DocPath path) {
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    65
        return DocFileFactory.getFactory(configuration).createFileForOutput(path);
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    66
    }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    67
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    68
    /**
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    69
     * The location for this file. Maybe null if the file was created without
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    70
     * a location or path.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    71
     */
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    72
    protected final Location location;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    73
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    74
    /**
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    75
     * The path relative to the (output) location. Maybe null if the file was
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    76
     * created without a location or path.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    77
     */
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    78
    protected final DocPath path;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    79
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    80
    /**
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    81
     * List the directories and files found in subdirectories along the
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    82
     * elements of the given location.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    83
     * @param configuration the doclet configuration
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    84
     * @param location currently, only {@link StandardLocation#SOURCE_PATH} is supported.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    85
     * @param path the subdirectory of the directories of the location for which to
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    86
     *  list files
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    87
     */
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40587
diff changeset
    88
    public static Iterable<DocFile> list(BaseConfiguration configuration, Location location, DocPath path) {
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    89
        return DocFileFactory.getFactory(configuration).list(location, path);
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    90
    }
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    91
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
    92
    /** Create a DocFile without a location or path */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    93
    protected DocFile() {
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    94
        this.location = null;
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    95
        this.path = null;
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    96
    }
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    97
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
    98
    /** Create a DocFile for a given location and relative path. */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    99
    protected DocFile(Location location, DocPath path) {
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   100
        this.location = location;
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   101
        this.path = path;
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   102
    }
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   103
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   104
    /**
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   105
     * Open an input stream for the file.
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   106
     *
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   107
     * @return an open input stream for the file
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   108
     * @throws DocFileIOException if there is a problem opening the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   109
     */
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   110
    public abstract InputStream openInputStream() throws DocFileIOException;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   111
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   112
    /**
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   113
     * Open an output stream for the file.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   114
     * The file must have been created with a location of
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14544
diff changeset
   115
     * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT}
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14544
diff changeset
   116
     * and a corresponding relative path.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   117
     *
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   118
     * @return an open output stream for the file
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   119
     * @throws DocFileIOException if there is a problem opening the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   120
     * @throws UnsupportedEncodingException if the configured encoding is not supported
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   121
     */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   122
    public abstract OutputStream openOutputStream() throws DocFileIOException, UnsupportedEncodingException;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   123
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   124
    /**
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   125
     * Open an writer for the file, using the encoding (if any) given in the
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   126
     * doclet configuration.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   127
     * The file must have been created with a location of
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14544
diff changeset
   128
     * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   129
     *
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   130
     * @return an open output stream for the file
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   131
     * @throws DocFileIOException if there is a problem opening the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   132
     * @throws UnsupportedEncodingException if the configured encoding is not supported
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   133
     */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   134
    public abstract Writer openWriter() throws DocFileIOException, UnsupportedEncodingException;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   135
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   136
    /**
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   137
     * Copy the contents of another file directly to this file.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   138
     *
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   139
     * @param fromFile the file to be copied
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   140
     * @throws DocFileIOException if there is a problem file copying the file
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   141
     */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   142
    public void copyFile(DocFile fromFile) throws DocFileIOException {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   143
        try (OutputStream output = openOutputStream()) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   144
            try (InputStream input = fromFile.openInputStream()) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   145
                byte[] bytearr = new byte[1024];
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   146
                int len;
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   147
                while ((len = read(fromFile, input, bytearr)) != -1) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   148
                    write(this, output, bytearr, len);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   149
                }
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   150
            } catch (IOException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   151
                throw new DocFileIOException(fromFile, DocFileIOException.Mode.READ, e);
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   152
            }
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   153
        } catch (IOException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   154
            throw new DocFileIOException(this, DocFileIOException.Mode.WRITE, e);
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   155
        }
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   156
    }
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   157
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   158
    /**
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   159
     * Copy the contents of a resource file to this file.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   160
     *
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   161
     * @param resource the path of the resource, relative to the package of this class
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   162
     * @param overwrite whether or not to overwrite the file if it already exists
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   163
     * @param replaceNewLine if false, the file is copied as a binary file;
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   164
     *     if true, the file is written line by line, using the platform line
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   165
     *     separator
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   166
     *
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   167
     * @throws DocFileIOException if there is a problem while writing the copy
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   168
     * @throws ResourceIOException if there is a problem while reading the resource
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   169
     */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   170
    public void copyResource(DocPath resource, boolean overwrite, boolean replaceNewLine)
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   171
                throws DocFileIOException, ResourceIOException {
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   172
        if (exists() && !overwrite)
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   173
            return;
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   174
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   175
        try {
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40587
diff changeset
   176
            InputStream in = BaseConfiguration.class.getResourceAsStream(resource.getPath());
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   177
            if (in == null)
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   178
                return;
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   179
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   180
            try {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   181
                if (replaceNewLine) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   182
                    try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   183
                        try (Writer writer = openWriter()) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   184
                            String line;
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   185
                            while ((line = readResourceLine(resource, reader)) != null) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   186
                                write(this, writer, line);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   187
                                write(this, writer, DocletConstants.NL);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   188
                            }
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   189
                        } catch (IOException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   190
                            throw new DocFileIOException(this, DocFileIOException.Mode.WRITE, e);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   191
                        }
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   192
                    }
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   193
                } else {
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   194
                    try (OutputStream out = openOutputStream()) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   195
                        byte[] buf = new byte[2048];
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   196
                        int n;
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   197
                        while ((n = readResource(resource, in, buf)) > 0) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   198
                            write(this, out, buf, n);
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   199
                        }
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   200
                    } catch (IOException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   201
                        throw new DocFileIOException(this, DocFileIOException.Mode.WRITE, e);
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   202
                    }
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   203
                }
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   204
            } finally {
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   205
                in.close();
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   206
            }
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   207
        } catch (IOException e) {
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   208
            throw new ResourceIOException(resource, e);
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   209
        }
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   210
    }
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   211
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   212
    /** Return true if the file can be read. */
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   213
    public abstract boolean canRead();
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   214
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   215
    /** Return true if the file can be written. */
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   216
    public abstract boolean canWrite();
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   217
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   218
    /** Return true if the file exists. */
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   219
    public abstract boolean exists();
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   220
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   221
    /** Return the base name (last component) of the file name. */
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   222
    public abstract String getName();
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   223
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   224
    /** Return the file system path for this file. */
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   225
    public abstract String getPath();
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   226
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   227
    /** Return true if file has an absolute path name. */
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   228
    public abstract boolean isAbsolute();
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   229
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   230
    /** Return true if file identifies a directory. */
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   231
    public abstract boolean isDirectory();
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   232
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   233
    /** Return true if file identifies a file. */
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   234
    public abstract boolean isFile();
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   235
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   236
    /** Return true if this file is the same as another. */
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   237
    public abstract boolean isSameFile(DocFile other);
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   238
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   239
    /** If the file is a directory, list its contents.
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   240
     *
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   241
     * @return the contents of the directory
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   242
     * @throws DocFileIOException if there is a problem while listing the directory
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   243
     */
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   244
    public abstract Iterable<DocFile> list() throws DocFileIOException;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   245
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   246
    /** Create the file as a directory, including any parent directories. */
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   247
    public abstract boolean mkdirs();
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   248
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   249
    /**
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   250
     * Derive a new file by resolving a relative path against this file.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   251
     * The new file will inherit the configuration and location of this file
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   252
     * If this file has a path set, the new file will have a corresponding
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   253
     * new path.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   254
     */
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   255
    public abstract DocFile resolve(DocPath p);
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   256
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   257
    /**
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   258
     * Derive a new file by resolving a relative path against this file.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   259
     * The new file will inherit the configuration and location of this file
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   260
     * If this file has a path set, the new file will have a corresponding
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   261
     * new path.
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   262
     */
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents: 14368
diff changeset
   263
    public abstract DocFile resolve(String p);
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   264
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   265
    /**
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   266
     * Resolve a relative file against the given output location.
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14544
diff changeset
   267
     * @param locn Currently, only
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14544
diff changeset
   268
     * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported.
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   269
     */
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14544
diff changeset
   270
    public abstract DocFile resolveAgainst(Location locn);
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   271
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   272
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   273
    /**
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   274
     * Reads from an input stream opened from a given file into a given buffer.
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   275
     * If an IOException occurs, it is wrapped in a DocFileIOException.
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   276
     *
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   277
     * @param inFile the file for the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   278
     * @param input the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   279
     * @param buf the buffer
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   280
     * @return the number of bytes read, or -1 if at end of file
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   281
     * @throws DocFileIOException if an exception occurred while reading the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   282
     */
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   283
    private static int read(DocFile inFile, InputStream input, byte[] buf) throws DocFileIOException {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   284
        try {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   285
            return input.read(buf);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   286
        } catch (IOException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   287
            throw new DocFileIOException(inFile, DocFileIOException.Mode.READ, e);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   288
        }
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   289
    }
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   290
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   291
    /**
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   292
     * Writes to an output stream for a given file from a given buffer.
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   293
     * If an IOException occurs, it is wrapped in a DocFileIOException.
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   294
     *
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   295
     * @param outFile the file for the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   296
     * @param out the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   297
     * @param buf the buffer
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   298
     * @throws DocFileIOException if an exception occurred while writing the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   299
     */
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   300
    private static void write(DocFile outFile, OutputStream out, byte[] buf, int len) throws DocFileIOException {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   301
        try {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   302
            out.write(buf, 0, len);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   303
        } catch (IOException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   304
            throw new DocFileIOException(outFile, DocFileIOException.Mode.WRITE, e);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   305
        }
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   306
    }
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   307
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   308
    /**
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   309
     * Writes text to an output stream for a given file from a given buffer.
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   310
     * If an IOException occurs, it is wrapped in a DocFileIOException.
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   311
     *
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   312
     * @param outFile the file for the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   313
     * @param out the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   314
     * @param text the text to be written
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   315
     * @throws DocFileIOException if an exception occurred while writing the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   316
     */
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   317
    private static void write(DocFile outFile, Writer out, String text) throws DocFileIOException {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   318
        try {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   319
            out.write(text);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   320
        } catch (IOException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   321
            throw new DocFileIOException(outFile, DocFileIOException.Mode.WRITE, e);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   322
        }
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   323
    }
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   324
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   325
    /**
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   326
     * Reads from an input stream opened from a given resource into a given buffer.
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   327
     * If an IOException occurs, it is wrapped in a ResourceIOException.
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   328
     *
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   329
     * @param resource the resource for the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   330
     * @param in the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   331
     * @param buf the buffer
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   332
     * @return the number of bytes read, or -1 if at end of file
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   333
     * @throws ResourceIOException if an exception occurred while reading the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   334
     */
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   335
    private static int readResource(DocPath resource, InputStream in, byte[] buf) throws ResourceIOException {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   336
        try {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   337
            return in.read(buf);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   338
        } catch (IOException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   339
            throw new ResourceIOException(resource, e);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   340
        }
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   341
    }
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   342
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   343
    /**
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   344
     * Reads a line of characters from an input stream opened from a given resource.
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   345
     * If an IOException occurs, it is wrapped in a ResourceIOException.
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   346
     *
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   347
     * @param resource the resource for the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   348
     * @param in the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   349
     * @return the line of text, or {@code null} if at end of stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   350
     * @throws ResourceIOException if an exception occurred while reading the stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   351
     */
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   352
    private static String readResourceLine(DocPath docPath, BufferedReader in) throws ResourceIOException {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   353
        try {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   354
            return in.readLine();
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   355
        } catch (IOException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   356
            throw new ResourceIOException(docPath, e);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   357
        }
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   358
    }
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
diff changeset
   359
}