You calculated all of the IFD sizes right above. Why do you have to do that again in calculateSegmentLength()?  Can't you add up what you have above?