aspose file tools*
The moose likes Java in General and the fly likes Trying to get a byte[] length using covered quotient Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Trying to get a byte[] length using covered quotient" Watch "Trying to get a byte[] length using covered quotient" New topic
Author

Trying to get a byte[] length using covered quotient

John Gable
Greenhorn

Joined: Apr 12, 2004
Posts: 11
Hi,
I'm trying to read from a text file into a byte[] array, and then convert that byte array into hex code.
The problem i have is assigning the length of my array. At the moment my code looks as such;
----------------------------------------
FileInputStream fis = new FileInputStream(file);
// R E A D
byte[] ba = new byte[1024];
// -1 means eof
int bytesRead;
bytesRead = fis.read( ba, 0 /* offset in ba */, ba.length /* bytes to read */ );
return ba;
}
-------------------------------------------
i've set my byte to the value of 1024. Which gives me alot of zeros, but i get the right hex input into the start of the array.
i need to make the length only as long as the next greatest power of 64bit (8 bytes). (and pad to complete the fields)
The covered quotient looks as follows
(dividend >= 0) ? ( (dividend + divisor - 1 ) / divisor) : (dividend / divisor );
however i do not know how to implement that ..
Any help would be much appreciated. .
John
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
I notice that you open the file with a file object. That file object has a length () method you can use to set the length of your array.


The nice thing about Standards is that there are so many to choose from!
John Gable
Greenhorn

Joined: Apr 12, 2004
Posts: 11
Unfortunately i cannot .. As i'm converting to a hex string the file.length() is too small .. I originally had it as that .. and only approx half of the file would be read into the byte array . .
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ummm, that doesn't make sense to me. The hex conversion is something you do after reading the bytes, right? You should be able to read all the bytes into an appropriately-sized array, and then convert to hex.
Are you aware that the read() method is not guaranteed to read all the bytes you request, and therefore you should usually read in a loop, checking return values as you go? E.g.


"I'm not back." - Bill Harding, Twister
John Gable
Greenhorn

Joined: Apr 12, 2004
Posts: 11
Sorry, i'm unsure where you attained buffer from ??
should it be filename instead ? ?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Sorry, "bytes" and "buffer" were supposed to be the same thing. If you look at the API for read(), there's only one version that takes three arguments, and the first argument has to be a byte[]. So there's no way filename would be involved.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to get a byte[] length using covered quotient
 
Similar Threads
File Read & Writing
Read file X lines
BufferedInputStream
file transfer over TCP problem
Copying Multiple Files Over A Socket