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. :confused:
Printable View
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. :confused:
What kind of stream?
DigestInputStream.
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".
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);
}
}
}
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);
}
}
}
I forgot a byte[] array cannot be passed. :blush: The writeDigest() method was exactly what was needed. Thanks. Yeah i didnt account for directories within directories but thats ok. Thanks for the help.
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.
Im not too sure if i follow your use of substring. I used a StringBuffer instead and it seems to work pretty good.
Thanks for the help.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);
}