OutputStream Problem!!
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Akshay_B
Posted On:   Monday, August 1, 2005 02:39 PM

OK,.. i dont know wether this is the appropriate forum to ask this,.. but here goes,.........


I am tryin to do this from the server side --> os.write(file1.length());

Where file1.length() is a long value and os = client.OutputStream.

I get a error that one cannot write Long on OutputStream.

So how should i do this? ??

Re: OutputStream Problem!!

Posted By:   Almagest_FUTT  
Posted On:   Monday, August 1, 2005 03:20 PM

Come on... you shouldn't even be able to compile that.

The write method takes either an int, to write a single byte / char, or a byte array, to write data.


Now, i don't know what you are trying to do, but if you really just want to write the (numeric) file length's value to your stream (which seems bogus to me, but whatever), you'll have to split your long into the eight bytes it's made up of, for instance like that:


public static byte[] longToBytes(long l, boolean msb){
byte[] ret = new byte[8];
int shift;
for(int ss = 0; ss < ret.length; ss++){
shift = 8 * (msb ? ret.length - ss - 1 : ss);
ret[ss] = (byte) ((l >>> shift) & 0xff);
}
return ret;
}

(Note: "msb" means "most significant byte first", aka nokia-network-byte-order. Test it. You'll probably need "true".)


And then: os.write(longToBytes(file1.length(), true));


Mind you that, though of course i do not know your needs, your operation seems highly bizarre.

About | Sitemap | Contact