Problem with bytes and Byte

i have a email client class for sending mails.

I have to define a limit for the attachments to be sent to 1 MB.

But the problem is it is not working.

Please help. i have given snippets of my code dealing with the attachment size.

So i defined a Hashtable to store the amount of MB it can take as key-value pair, given below.

hashObj.put("bytes","1048576");

now the constructor of the class i have extracted this value like this :

public EmailClient(Hashtable hashConfigParam)throws Exception

{

String strBytes = (String)hashConfigParam.get("bytes");

// System.out.println(" string from hashtable " + strBytes);

Long longObj =new Long(strBytes);

//System.out.println(" Long obj : " + longObj);

byte byteResult = longObj.byteValue();

// System.out.println(" bytes from hashtable " + byteResult);

objConfigBytes =new Byte(byteResult);

/*objConfigBytes = new Byte(Byte.parseByte(strBytes,10));

System.out.println(" bytes from hashtable " + objConfigBytes.toString());*/

}

i have another piece of code for adding up the attachment size. i have stored all my attachment files in a vector..so the code is as below.

public Boolean setAttachmentPath(String filepath,String filename)

{

try

{

File fileObj =new File(filepath,filename);

if(fileObj.exists() && fileObj.isFile())

{

vecAddFile.add(fileObj);

returnnew Boolean(true);

}

else

returnnew Boolean(false);

}

catch (Exception e)

{

returnnull;

}

}

the function where i am checking for constraints that is for the attachment size not to be greater than 1 mb. is this

public Boolean checkConstraints()

{

try

{

byte resultInBytes = getAttachmentSize(vecAddFile);

Byte objByte =new Byte(resultInBytes);

int compareResult = objByte.compareTo(objConfigBytes);

if( compareResult < 0)

returnnew Boolean(false);

else

returnnew Boolean(true);

}

catch (Exception e)

{

returnnull;

}

}

[3919 byte] By [nb123a] at [2007-11-26 12:41:20]
# 1
You can get() the size of a File object prior to attaching it to the e-mail. Why use all that code, when it is fairly simple?
watertownjordana at 2007-7-7 16:14:11 > top of Java-index,Java Essentials,New To Java...
# 2

Just define a simple variable

// Denotes the number of bytes used for attachments <= 1MB

long bytesUsed = 0;

and use

// File file = new File("filename");

if (bytesUsed + file.length() <= 1048576)

bytes += file.length();

//...

else

// Attachment will not fit

when attaching a new File.

Jukka

duckbilla at 2007-7-7 16:14:11 > top of Java-index,Java Essentials,New To Java...
# 3

This is what i am doing to get teh attachment sizes of all files

public byte getAttachmentSize(Vector vecFile)

{

long result = 0;

Long objLong = null;

for(int i=0; i < vecFile.size(); i++)

{

File objFile = (File)vecFile.get(i);

result += objFile.length();

}

objLong = new Long(result);

return objLong.byteValue();

}

nb123a at 2007-7-7 16:14:11 > top of Java-index,Java Essentials,New To Java...
# 4
Why not just return primitive longs and booleans instead of fiddling around with bytes and objects?And do you really need that Hashtable?Jukka
duckbilla at 2007-7-7 16:14:11 > top of Java-index,Java Essentials,New To Java...
# 5
Ya thanks...thats wat i did now dealing with long...
nb123a at 2007-7-7 16:14:11 > top of Java-index,Java Essentials,New To Java...