Results 1 to 16 of 16
  1. #1
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default Stream closed on a ClassLoader input

    Here is my code (yes, I know part of it isn't used anymore):

    public static boolean comparePasswords(String input) throws IOException{
    String password = "";
    String file = "src/password.spm";

    FileInputStream passwordRead = new FileInputStream(file);
    BufferedInputStream passwordBuffer = new BufferedInputStream(ClassLoader.getSystemResourceA sStream(file));
    DataInputStream passwordInput = new DataInputStream(passwordBuffer);

    while(passwordInput.available() != 0){ //<---- Here is the error
    password = passwordInput.readLine();
    }

    if(encrypt(input).equals(password)){
    return true;
    }
    else{
    return false;
    }
    }
    and my problem:

    Exception in thread "main" java.io.IOException: Stream closed
    at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
    at java.io.BufferedInputStream.available(Unknown Source)
    at java.io.FilterInputStream.available(Unknown Source)
    at login.login.comparePasswords(login.java:29)
    at login.login.main(login.java:73)
    I don't close the stream anywhere, so I don't understand what it is whining about - can "ClassLoader.getSystemResourceAsStream(file)" not be used like that?

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Check the file path once.
    Ramya:cool:

  3. #3
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Yes? It is not a FileNotFound error, which it was initially, until I changed it to what it says now.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    public static void main(String[] args) throws IOException{
    String password = "hej";
    System.out.println(encrypt(password));
    System.out.println(comparePasswords(password));
    }
    Just a main to check it.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Is those display the correct/expected values?

    Java Code:
    System.out.println(encrypt(password));
    System.out.println(comparePasswords(password));

  7. #7
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    The first one is, but the second is where it bugs, because it calls my comparePasswords.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    How/where is the code being executed?
    What jdk and jre are you using?

  9. #9
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Well the code is being executed in that main, in the same class (see my reply), I am using JavaSE 1.6 in my eclipse

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Have you tried it at a command prompt, not in eclipse?

  11. #11
    mjawwad is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default code

    Please try this code :

    public static boolean comparePasswords(String input) throws IOException
    {
    FileReader reader = new FileReader("src/password.spm");
    StringBuffer buffer = new StringBuffer();
    BufferedReader passwordBuffer = new BufferedReader(reader);

    String line = null;
    while ((line = passwordBuffer.readLine()) != null)
    buffer.append(line);

    return encrypt(input).equals(buffer.toString());
    }

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    Have you tried it at a command prompt, not in eclipse?
    I'm confusing why you suggest this lol. Any special reason to try out this?

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    To remove any special paths used by IDE. If everything is in the same folder for testing it removes one source of confusion

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Hmmm, that's interesting. So we can boil done those path issues. But at the same time, I think that in most of the IDEs we can specify relative paths as well as absolute paths.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    I like to keep it simple (only one folder) until I've fixed the other problems. Then add code for allow for using other folders.

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    In most of the cases, what I'm doing is that keep all resources (files, images, etc..) in a single folder and use the hard corded values. Later I'm worried about the structuring them.

Similar Threads

  1. Replies: 1
    Last Post: 01-31-2010, 04:17 AM
  2. standard input stream storing to a generic method?
    By vendetta in forum New To Java
    Replies: 3
    Last Post: 01-29-2010, 09:13 PM
  3. classloader
    By vijayabaskar in forum Advanced Java
    Replies: 8
    Last Post: 04-06-2009, 09:08 AM
  4. ClassLoader and JVM
    By Pradeen in forum New To Java
    Replies: 0
    Last Post: 01-19-2009, 07:04 PM
  5. Replies: 2
    Last Post: 08-07-2007, 04:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •