Reputation: 809
I am running the following program and get the Stream Closed IO Error. but only in the second loop. the first one works fine. Can anybody please tell me why? (I checked that the file is existing and not empty.)
private static TimerTask perform(){
//logging on to FTP-Server
InputStream in = client.retrieveFileStream("./htdocs/pwiMain/main.txt");
InputStream pw = client.retrieveFileStream("./htdocs/pwiMain/cred_pwd.txt");
BufferedInputStream inbf = new BufferedInputStream(in);
int bytesRead;
byte[] buffer = new byte[1024];
String wholeFile = null;
String[] contents;
while((bytesRead = inbf.read(buffer)) != -1){
wholeFile = new String(buffer,0,bytesRead);
}
sentPassword = wholeFile.substring(wholeFile.indexOf("#lap"));
inbf.close();
inbf = new BufferedInputStream(pw);
while((bytesRead = inbf.read(buffer)) != -1){ // this is line72 where the error occurrs...
wholeFile = new String(buffer,0,bytesRead);
}
md5hash = wohleFile;
inbf.close();
contents = sentPassword.split("\\r\\n|\\n|\\r");
System.out.println("contents: " + contents[0] + " " + contents[1]);
//check the password
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("ioexception");
} finally {
}
return null;
}
Here is the error message:
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:159)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at com.protonmail.taylor.faebl.development.main.perform(main.java:72)
at com.protonmail.taylor.faebl.development.main.main(main.java:23)
Thanks a lot for your help :)
Upvotes: 1
Views: 14269
Reputation: 310840
You evidently can't have two retrieval streams active at the same time, which isn't surprising. Just reorder your code:
private static TimerTask perform(){
try {
//logging on to FTP-Server
InputStream in = client.retrieveFileStream("./htdocs/pwiMain/main.txt");
BufferedInputStream inbf = new BufferedInputStream(in);
int bytesRead;
byte[] buffer = new byte[1024];
String wholeFile = null;
String wholeCred = null;
String[] contents;
while((bytesRead = inbf.read(buffer)) != -1){
wholeFile = new String(buffer,0,bytesRead);
}
inbf.close(); // ADDED
InputStream pw = client.retrieveFileStream("./htdocs/pwiMain/cred_pwd.txt");
BufferedInputStream pwbf = new BufferedInputStream(pw);
int pwBytesRead; // YOU DON'T NEED THIS, you could reuse the previous one
byte[] pwBuffer = new byte [1024]; // DITTO
while((pwBytesRead = pwbf.read(pwBuffer)) != -1){
wholeCred = new String(pwBuffer,0,pwBytesRead);
}
pwbf.close(); // ADDED
sentPassword = wholeFile.substring(sentPassword.indexOf("#lap"));
md5hash = wholeCred;
contents = sentPassword.split("\\r\\n|\\n|\\r");
System.out.println("contents: " + contents[0] + " " + contents[1]);
//check the password
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("ioexception");
} finally {
}
return null;
}
There's no point or advantage in the way you're doing it now, you're just wasting space, and as you've discovered it doesn't work.
Of course you will then discover that if either of the inputs exceeds one buffer it won't work, but you didn't ask about that.
Upvotes: 4