Bug 51161 - Stream Closed error in DefaultInputHandler with embedded ant <input> task
Stream Closed error in DefaultInputHandler with embedded ant <input> task
Status: RESOLVED FIXED
Product: Ant
Classification: Unclassified
Component: Core
1.8.1
Macintosh All
: P2 normal (vote)
: 1.8.3
Assigned To: Ant Notifications List
:
Depends on:
Blocks:
  Show dependency tree
 
Reported: 2011-05-06 18:50 UTC by Kelly Davis
Modified: 2011-07-26 22:03 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kelly Davis 2011-05-06 18:50:47 UTC
I am using the maven-antrun-plugin 1.6 with the <input> task and the default input handler. When I enter input, it throws a BuildException, "Failed to read input from Console". The root exception is:


java.io.IOException: Stream closed
        at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:145)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:308)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
        at java.io.InputStreamReader.read(InputStreamReader.java:167)
        at java.io.BufferedReader.fill(BufferedReader.java:136)
        at java.io.BufferedReader.readLine(BufferedReader.java:299)
        at java.io.BufferedReader.readLine(BufferedReader.java:362)


I was able to fix this issue by wrapping the input stream in KeepAliveInputStream as follows:

    public void handleInput(InputRequest request) throws BuildException {
        String prompt = getPrompt(request);
        BufferedReader r = null;
        try {
            r = new BufferedReader(new InputStreamReader(new KeepAliveInputStream(getInputStream())));
            do {
                System.err.print(prompt + ": ");
                System.err.flush();
                try {
                    String input = r.readLine();
                    request.setInput(input);
                } catch (IOException e) {
                    throw new BuildException("Failed to read input from"
                                             + " Console.", e);
                }
            } while (!request.isInputValid());
        } finally {
            if (r != null) {
                try {
                    r.close();
                } catch (IOException e) {
                    throw new BuildException("Failed to close input.", e);
                }
            }
        }
    }
Comment 1 Nicolas Lalevée 2011-06-08 08:41:14 UTC
I am not that used to write pom.xml file. Could you provide a piece of pom.xml which reproduce the bug please ?
Comment 2 Kelly Davis 2011-06-08 12:17:30 UTC
The following pom will generate the error:


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.help</groupId>
    <artifactId>Test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Test</name>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <id>configure-resources</id>
                        <phase>generate-resources</phase>
                        <configuration>
                            <target>
                                <input message="Enter Foo Property" addproperty="foo.property" defaultvalue="FOO"/>
                                <input message="Enter Bar Property" addproperty="bar.property" defaultvalue="BAR"/>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

execute 'mvn generate-sources'
Comment 3 Nicolas Lalevée 2011-07-24 11:54:32 UTC
Sorry for the late follow up, but I cannot reproduce the error. I tried with both maven 2 and maven 3. Here is what I got:

hibou@hibpro ~/dev/ant/svn/core/trunk/tmp $ mvn generate-sources       
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.135s
[INFO] Finished at: Sun Jul 24 13:52:29 CEST 2011
[INFO] Final Memory: 2M/81M
[INFO] ------------------------------------------------------------------------
hibou@hibpro ~/dev/ant/svn/core/trunk/tmp $ mvn2 generate-sources      
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Test
[INFO]    task-segment: [generate-sources]
[INFO] ------------------------------------------------------------------------
[INFO] No goals needed for project - skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Sun Jul 24 13:52:36 CEST 2011
[INFO] Final Memory: 2M/81M
[INFO] ------------------------------------------------------------------------
Comment 4 Kelly Davis 2011-07-26 18:33:43 UTC
Ok, sorry about that. If you run 'mvn generate-sources' it doesn't run the ant plugin. Please run it with 'mvn compile'. That should trigger the problem.
Comment 5 Nicolas Lalevée 2011-07-26 22:03:09 UTC
reproduced and fixed. Thanks for your report.