⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 concat.java

📁 java ant的源码!非常值得看的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }    /**     * Execute the concat task.     */    public void execute() {        ResourceCollection c = validate();        if (c == null) {            return;        }        // Do nothing if no resources (including nested text)        if (c.size() < 1 && header == null && footer == null) {            log("No existing resources and no nested text, doing nothing",                Project.MSG_INFO);            return;        }        if (binary) {            binaryCat(c);        } else {            cat(c);        }    }    /** perform the binary concatenation */    private void binaryCat(ResourceCollection c) {        log("Binary concatenation of " + c.size()            + " resources to " + destinationFile);        FileOutputStream out = null;        InputStream in = null;        try {            try {                out = new FileOutputStream(destinationFile);            } catch (Exception t) {                throw new BuildException("Unable to open "                    + destinationFile + " for writing", t);            }            in = new ConcatResourceInputStream(c);            ((ConcatResourceInputStream) in).setManagingComponent(this);            Thread t = new Thread(new StreamPumper(in, out));            t.start();            try {                t.join();            } catch (InterruptedException e) {                try {                    t.join();                } catch (InterruptedException ee) {                    // Empty                }            }        } finally {            FileUtils.close(in);            if (out != null) {                try {                    out.close();                } catch (Exception ex) {                    throw new BuildException(                        "Unable to close " + destinationFile, ex);                }            }        }    }    /** perform the concatenation */    private void cat(ResourceCollection c) {        OutputStream os = null;        char[] buffer = new char[BUFFER_SIZE];        try {            PrintWriter writer = null;            if (outputWriter != null) {                writer = new PrintWriter(outputWriter);            } else {                if (destinationFile == null) {                    // Log using WARN so it displays in 'quiet' mode.                    os = new LogOutputStream(this, Project.MSG_WARN);                } else {                    // ensure that the parent dir of dest file exists                    File parent = destinationFile.getParentFile();                    if (!parent.exists()) {                        parent.mkdirs();                    }                    os = new FileOutputStream(destinationFile.getAbsolutePath(),                                              append);                }                if (outputEncoding == null) {                    writer = new PrintWriter(                        new BufferedWriter(                            new OutputStreamWriter(os)));                } else {                    writer = new PrintWriter(                        new BufferedWriter(                            new OutputStreamWriter(os, outputEncoding)));                }            }            if (header != null) {                if (header.getFiltering()) {                    concatenate(                        buffer, writer, new StringReader(header.getValue()));                } else {                    writer.print(header.getValue());                }            }            if (c.size() > 0) {                concatenate(buffer, writer, new MultiReader(c));            }            if (footer != null) {                if (footer.getFiltering()) {                    concatenate(                        buffer, writer, new StringReader(footer.getValue()));                } else {                    writer.print(footer.getValue());                }            }            writer.flush();            if (os != null) {                os.flush();            }        } catch (IOException ioex) {            throw new BuildException("Error while concatenating: "                                     + ioex.getMessage(), ioex);        } finally {            FileUtils.close(os);        }    }    /** Concatenate a single reader to the writer using buffer */    private void concatenate(char[] buffer, Writer writer, Reader in)        throws IOException {        if (filterChains != null) {            ChainReaderHelper helper = new ChainReaderHelper();            helper.setBufferSize(BUFFER_SIZE);            helper.setPrimaryReader(in);            helper.setFilterChains(filterChains);            helper.setProject(getProject());            in = new BufferedReader(helper.getAssembledReader());        }        while (true) {            int nRead = in.read(buffer, 0, buffer.length);            if (nRead == -1) {                break;            }            writer.write(buffer, 0, nRead);        }        writer.flush();    }    /**     * Treat empty nested text as no text.     *     * <p>Depending on the XML parser, addText may have been called     * for &quot;ignorable whitespace&quot; as well.</p>     */    private void sanitizeText() {        if (textBuffer != null) {            if (textBuffer.substring(0).trim().length() == 0) {                textBuffer = null;            }        }    }    /**     * sub element points to a file or contains text     */    public static class TextElement extends ProjectComponent {        private String   value = "";        private boolean  trimLeading = false;        private boolean  trim = false;        private boolean  filtering = true;        private String   encoding = null;        /**         * whether to filter the text in this element         * or not.         *         * @param filtering true if the text should be filtered.         *                  the default value is true.         */        public void setFiltering(boolean filtering) {            this.filtering = filtering;        }        /** return the filtering attribute */        private boolean getFiltering() {            return filtering;        }        /**         * The encoding of the text element         *         * @param encoding the name of the charset used to encode         */        public void setEncoding(String encoding) {            this.encoding = encoding;        }        /**         * set the text using a file         * @param file the file to use         * @throws BuildException if the file does not exist, or cannot be         *                        read         */        public void setFile(File file) throws BuildException {            // non-existing files are not allowed            if (!file.exists()) {                throw new BuildException("File " + file + " does not exist.");            }            BufferedReader reader = null;            try {                if (this.encoding == null) {                    reader = new BufferedReader(new FileReader(file));                } else {                    reader = new BufferedReader(                        new InputStreamReader(new FileInputStream(file),                                              this.encoding));                }                value = FileUtils.readFully(reader);            } catch (IOException ex) {                throw new BuildException(ex);            } finally {                FileUtils.close(reader);            }        }        /**         * set the text using inline         * @param value the text to place inline         */        public void addText(String value) {            this.value += getProject().replaceProperties(value);        }        /**         * s:^\s*:: on each line of input         * @param strip if true do the trim         */        public void setTrimLeading(boolean strip) {            this.trimLeading = strip;        }        /**         * whether to call text.trim()         * @param trim if true trim the text         */        public void setTrim(boolean trim) {            this.trim = trim;        }        /**         * @return the text, after possible trimming         */        public String getValue() {            if (value == null) {                value = "";            }            if (value.trim().length() == 0) {                value = "";            }            if (trimLeading) {                char[] current = value.toCharArray();                StringBuffer b = new StringBuffer(current.length);                boolean startOfLine = true;                int pos = 0;                while (pos < current.length) {                    char ch = current[pos++];                    if (startOfLine) {                        if (ch == ' ' || ch == '\t') {                            continue;                        }                        startOfLine = false;                    }                    b.append(ch);                    if (ch == '\n' || ch == '\r') {                        startOfLine = true;                    }                }                value = b.toString();            }            if (trim) {                value = value.trim();            }            return value;        }    }    /**     * This class reads from each of the source files in turn.     * The concatentated result can then be filtered as     * a single stream.     */    private class MultiReader extends Reader {        private Reader reader = null;        private int    lastPos = 0;        private char[] lastChars = new char[eolString.length()];        private boolean needAddSeparator = false;        private Iterator i;        private MultiReader(ResourceCollection c) {            i = c.iterator();        }        private Reader getReader() throws IOException {            if (reader == null && i.hasNext()) {                Resource r = (Resource) i.next();                log("Concating " + r.toLongString(), Project.MSG_VERBOSE);                InputStream is = r.getInputStream();                reader = new BufferedReader(encoding == null                    ? new InputStreamReader(is)                    : new InputStreamReader(is, encoding));                Arrays.fill(lastChars, (char) 0);            }            return reader;        }        private void nextReader() throws IOException {            close();            reader = null;        }        /**         * Read a character from the current reader object. Advance         * to the next if the reader is finished.         * @return the character read, -1 for EOF on the last reader.         * @exception IOException - possibly thrown by the read for a reader         *            object.         */        public int read() throws IOException {            if (needAddSeparator) {                int ret = eolString.charAt(lastPos++);                if (lastPos >= eolString.length()) {                    lastPos = 0;                    needAddSeparator = false;                }                return ret;            }            while (getReader() != null) {                int ch = getReader().read();                if (ch == -1) {                    nextReader();                    if (fixLastLine && isMissingEndOfLine()) {                        needAddSeparator = true;                        lastPos = 0;                    }                } else {                    addLastChar((char) ch);                    return ch;                }            }            return -1;        }        /**         * Read into the buffer <code>cbuf</code>.         * @param cbuf The array to be read into.         * @param off The offset.         * @param len The length to read.         * @exception IOException - possibly thrown by the reads to the         *            reader objects.         */        public int read(char[] cbuf, int off, int len)            throws IOException {            int amountRead = 0;            while (getReader() != null || needAddSeparator) {                if (needAddSeparator) {                    cbuf[off] = eolString.charAt(lastPos++);                    if (lastPos >= eolString.length()) {                        lastPos = 0;                        needAddSeparator = false;                    }                    len--;                    off++;                    amountRead++;                    if (len == 0) {                        return amountRead;                    }                    continue;                }                int nRead = getReader().read(cbuf, off, len);                if (nRead == -1 || nRead == 0) {                    nextReader();                    if (fixLastLine && isMissingEndOfLine()) {                        needAddSeparator = true;                        lastPos = 0;                    }                } else {                    if (fixLastLine) {                        for (int i = nRead;                                 i > (nRead - lastChars.length);                                 --i) {                            if (i <= 0) {                                break;                            }                            addLastChar(cbuf[off + i - 1]);                        }                    }                    len -= nRead;                    off += nRead;                    amountRead += nRead;                    if (len == 0) {                        return amountRead;                    }                }            }            if (amountRead == 0) {                return -1;            } else {                return amountRead;            }        }        /**         * Close the current reader         */        public void close() throws IOException {            if (reader != null) {                reader.close();            }        }        /**         * if checking for end of line at end of file         * add a character to the lastchars buffer         */        private void addLastChar(char ch) {            for (int i = lastChars.length - 2; i >= 0; --i) {                lastChars[i] = lastChars[i + 1];            }            lastChars[lastChars.length - 1] = ch;        }        /**         * return true if the lastchars buffer does         * not contain the lineseparator         */        private boolean isMissingEndOfLine() {            for (int i = 0; i < lastChars.length; ++i) {                if (lastChars[i] != eolString.charAt(i)) {                    return true;                }            }            return false;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -