Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to get a byte[] length using covered quotient

 
John Gable
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Gable
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Gable
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, i'm unsure where you attained buffer from ??
should it be filename instead ? ?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic