Results 1 to 9 of 9

Thread: Bad file descriptor?{resolved}

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Bad file descriptor?{resolved}

    Does anyone know what this might mean? I am trying to calculate a message digest for a stream but a java.io.Exception: Bad file descriptor keeps getting thrown.
    Last edited by Dilenger4; Jan 16th, 2004 at 01:46 PM.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What kind of stream?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    DigestInputStream.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Wrapped around what? A SocketInputStream?

    The message sounds a bit like a cryptic way of saying "network connection lost", or maybe "no data currently available".
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I was able to get things to work but im not quite sure if the output produced from the code is correct. Run this on a directory and see what you get.
    Code:
     import java.security.*; 
     import java.io.*;
      
     class Digest{
    
      FileInputStream fis;
      private MessageDigest md; 
      private DigestInputStream dis; 
      private BufferedOutputStream bis; 
      private byte[] digest;
      private File[] files;
      private int bytesread;
      private byte[] buffer;  
      
      public Digest(File[] files){
       this.files = files; 
      }  
    
      public void calDigest() throws IOException, NoSuchAlgorithmException{
    
        for(int i = 0; i < files.length; i++){
         md = MessageDigest.getInstance("SHA"); 
         fis = new FileInputStream(files[i]);  
         dis = new DigestInputStream(fis, md);
         buffer = new byte[(byte)files[i].length()];
         while((bytesread = dis.read(buffer)) != -1)  
         digest = dis.getMessageDigest().digest();
         System.out.println(files[i].getName() + ": " + digest);     
         System.out.println();
        
       }   
      }
     }
    
     public class DigestTest{
        public static void main(String[] args){
         
       File dir = new File(args[0]);
         if(args.length == 0){
           System.out.println("Usage: You must specify arguemets!");  
           System.exit(0); 
         }
         if(!dir.isDirectory()){
           System.out.println("Usage: You must specify a directory!");  
           System.exit(0); 
         }
         
        File[] files = dir.listFiles(); 
        Digest digest = new Digest(files);
        try{  
        digest.calDigest();
        }catch(IOException e){
            System.err.println(e);
        }
        catch(NoSuchAlgorithmException nsa){
          System.err.println(nsa);
          } 
        }
      }

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can't just pass a byte array to println, but aside from that it seems to be working. I have no means to check the digest though, but why should it be wrong?
    I adjusted the code slightly, but I still get IOExceptions when the thing tries to digest a directory.

    Code:
    import java.security.*;
    import java.io.*;
    
    class Digest{
    
    	FileInputStream fis;
    	private MessageDigest md;
    	private DigestInputStream dis;
    	private BufferedOutputStream bis;
    	private byte[] digest;
    	private File[] files;
    	private int bytesread;
    	private byte[] buffer;
    
    	public Digest(File[] files){
    		this.files = files;
    	}
    
    	public void calDigest() throws IOException, NoSuchAlgorithmException{
    		buffer = new byte[1024];
    		for(int i = 0; i < files.length; i++){
    			md = MessageDigest.getInstance("SHA");
    			fis = new FileInputStream(files[i]);
    			dis = new DigestInputStream(fis, md);
    			while((bytesread = dis.read(buffer)) != -1)
    			digest = dis.getMessageDigest().digest();
    			System.out.println(files[i].getName() + ": ");
    			writeDigest();
    			System.out.println();
    			System.out.flush();
    		}
    	}
    
    	private void writeDigest() {
    		int l = digest.length;
    		for(int i = 0; i < l; ++i) {
    			try {
    			System.out.print(Integer.toHexString(digest[i]).substring(0,2) + " ");
    			} catch(Exception e) {
    			}
    		}
    	}
    }
    
    public class DigestTest{
    	public static void main(String[] args){
    	 
    		if(args.length == 0){
    			System.out.println("Usage: You must specify arguments!");  
    			System.exit(0); 
    		}
    		File dir = new File(args[0]);
    		if(!dir.isDirectory()){
    			System.out.println("Usage: You must specify a directory!");  
    			System.exit(0); 
    		}
    		
    		File[] files = dir.listFiles(); 
    		Digest digest = new Digest(files);
    		try{  
    			digest.calDigest();
    		}catch(IOException e){
    			System.err.println(e);
    		}catch(NoSuchAlgorithmException nsa){
    			System.err.println(nsa);
    		} 
    	}
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I forgot a byte[] array cannot be passed. The writeDigest() method was exactly what was needed. Thanks. Yeah i didnt account for directories within directories but thats ok. Thanks for the help.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    BTW you need to refine writeDigest. Currently it just skips bytes where the most significant nibble is 0 (anything from 00 to 0F). This is because they are converted to the strings '' to 'F' and substring throws a StringIndexOutOfBoundsException.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Im not too sure if i follow your use of substring. I used a StringBuffer instead and it seems to work pretty good.
    Code:
    private void writeDigest(String file) {
      StringBuffer sb = new StringBuffer(file);
      sb.append(":");
      for(int i = 0; i < digest.length; ++i) {
         sb.append(digest[i] + " "); 
      }	
       System.out.println(sb);
    }
    Thanks for the help.

Posting Permissions

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



Click Here to Expand Forum to Full Width