[Ant] Echo task

In the Ant build file build.xml, we can create one echo task like following snippets.

<project>
	<target>
		<echo> Merry Christmas </echo>
	</target>
</project>
When you use Ant command to run the target, it will print out "Merry Christmas" on the screen.You may wonder of how Ant achieves this task, i will let the dog out of bag.

   public class Echo extends Task 
   public void execute() throws BuildException {
        final String msg = "".equals(message) ? StringUtils.LINE_SEP : message;
        try {
            ResourceUtils
                    .copyResource(new StringResource(msg), output == null
                                  ? new LogOutputResource(this, logLevel)
                                  : output,
                                  null, null, false, false, append, null,
                                  "".equals(encoding) ? null : encoding,
                                  getProject(), force);
        } catch (IOException ioe) {
            throw new BuildException(ioe, getLocation());
        }
    }
Resource use OutputStream to output the message to screen or file.

    public static void copyResource(Resource source, Resource dest,
                            FilterSetCollection filters, Vector filterChains,
                            boolean overwrite, boolean preserveLastModified,
                                    boolean append,
                                    String inputEncoding, String outputEncoding,
                                    Project project, boolean force)
        throws IOException {
        if (!(overwrite || SelectorUtils.isOutOfDate(source, dest, FileUtils.getFileUtils()
                .getFileTimestampGranularity()))) {
            return;
        }
        final boolean filterSetsAvailable = (filters != null
                                             && filters.hasFilters());
        final boolean filterChainsAvailable = (filterChains != null
                                               && filterChains.size() > 0);

        File destFile = null;
        if (dest.as(FileProvider.class) != null) {
            destFile = ((FileProvider) dest.as(FileProvider.class)).getFile();
        }
        if (destFile != null && destFile.isFile() && !destFile.canWrite()) {
            if (!force) {
                throw new IOException("can't write to read-only destination "
                                      + "file " + destFile);
            } else if (!FILE_UTILS.tryHardToDelete(destFile)) {
                throw new IOException("failed to delete read-only "
                                      + "destination file " + destFile);
            }
        }

        if (filterSetsAvailable) {
            BufferedReader in = null;
            BufferedWriter out = null;
            try {
                InputStreamReader isr = null;
                if (inputEncoding == null) {
                    isr = new InputStreamReader(source.getInputStream());
                } else {
                    isr = new InputStreamReader(source.getInputStream(),
                                                inputEncoding);
                }
                in = new BufferedReader(isr);
                OutputStream os = getOutputStream(dest, append, project);
                OutputStreamWriter osw;
                if (outputEncoding == null) {
                    osw = new OutputStreamWriter(os);
                } else {
                    osw = new OutputStreamWriter(os, outputEncoding);
                }
                out = new BufferedWriter(osw);
                if (filterChainsAvailable) {
                    ChainReaderHelper crh = new ChainReaderHelper();
                    crh.setBufferSize(FileUtils.BUF_SIZE);
                    crh.setPrimaryReader(in);
                    crh.setFilterChains(filterChains);
                    crh.setProject(project);
                    Reader rdr = crh.getAssembledReader();
                    in = new BufferedReader(rdr);
                }
                LineTokenizer lineTokenizer = new LineTokenizer();
                lineTokenizer.setIncludeDelims(true);
                String newline = null;
                String line = lineTokenizer.getToken(in);
                while (line != null) {
                    if (line.length() == 0) {
                        // this should not happen, because the lines are
                        // returned with the end of line delimiter
                        out.newLine();
                    } else {
                        newline = filters.replaceTokens(line);
                        out.write(newline);
                    }
                    line = lineTokenizer.getToken(in);
                }
            } finally {
                FileUtils.close(out);
                FileUtils.close(in);
            }
        } else if (filterChainsAvailable
                   || (inputEncoding != null
                       && !inputEncoding.equals(outputEncoding))
                   || (inputEncoding == null && outputEncoding != null)) {
            BufferedReader in = null;
            BufferedWriter out = null;
            try {
                InputStreamReader isr = null;
                if (inputEncoding == null) {
                    isr = new InputStreamReader(source.getInputStream());
                } else {
                    isr = new InputStreamReader(source.getInputStream(),
                                                inputEncoding);
                }
                in = new BufferedReader(isr);
                OutputStream os = getOutputStream(dest, append, project);
                OutputStreamWriter osw;
                if (outputEncoding == null) {
                    osw = new OutputStreamWriter(os);
                } else {
                    osw = new OutputStreamWriter(os, outputEncoding);
                }
                out = new BufferedWriter(osw);
                if (filterChainsAvailable) {
                    ChainReaderHelper crh = new ChainReaderHelper();
                    crh.setBufferSize(FileUtils.BUF_SIZE);
                    crh.setPrimaryReader(in);
                    crh.setFilterChains(filterChains);
                    crh.setProject(project);
                    Reader rdr = crh.getAssembledReader();
                    in = new BufferedReader(rdr);
                }
                char[] buffer = new char[FileUtils.BUF_SIZE];
                while (true) {
                    int nRead = in.read(buffer, 0, buffer.length);
                    if (nRead == -1) {
                        break;
                    }
                    out.write(buffer, 0, nRead);
                }
            } finally {
                FileUtils.close(out);
                FileUtils.close(in);
            }
        } else if (source.as(FileProvider.class) != null
                   && destFile != null) {
            File sourceFile =
                ((FileProvider) source.as(FileProvider.class)).getFile();

            File parent = destFile.getParentFile();
            if (parent != null && !parent.isDirectory()
                && !destFile.getParentFile().mkdirs()) {
                throw new IOException("failed to create the parent directory"
                                      + " for " + destFile);
            }

            FileInputStream in = null;
            FileOutputStream out = null;
            FileChannel srcChannel = null;
            FileChannel destChannel = null;

            try {
                in = new FileInputStream(sourceFile);
                out = new FileOutputStream(destFile);
                    
                srcChannel = in.getChannel();
                destChannel = out.getChannel();
                
                long position = 0;
                long count = srcChannel.size();
                while (position < count) {
                    long chunk = Math.min(MAX_IO_CHUNK_SIZE, count - position);
                    position +=
                        destChannel.transferFrom(srcChannel, position, chunk);
                }
            } finally {
                FileUtils.close(srcChannel);
                FileUtils.close(destChannel);
                FileUtils.close(out);
                FileUtils.close(in);
            }
        } else {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = source.getInputStream();
                out = getOutputStream(dest, append, project);

                byte[] buffer = new byte[FileUtils.BUF_SIZE];
                int count = 0;
                do {
                    out.write(buffer, 0, count);
                    count = in.read(buffer, 0, buffer.length);
                } while (count != -1);
            } finally {
                FileUtils.close(out);
                FileUtils.close(in);
            }
        }
        if (preserveLastModified) {
            Touchable t = (Touchable) dest.as(Touchable.class);
            if (t != null) {
                setLastModified(t, source.getLastModified());
            }
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值