@Override
public Component load(CapInputStream in) throws UnableToReadCapFileException {
assert offsets != null;
MethodComponent methodComponent = new MethodComponent();
// we first read tag and size
super.load((byte) ComponentEnum.METHOD_COMPONENT.getValue(), in, methodComponent);
// we reset the count of byte read to zero
in.resetCount();
methodComponent.setOffsets(offsets);
methodComponent.getOffsets().remove(new Short((short) 0xffff));
Collections.sort(methodComponent.getOffsets());
methodComponent.setHandlerCount(in.readByte());
methodComponent.setExceptionHandlers(new ArrayList<ExceptionHandlerInfo>(methodComponent.getHandlerCount()));
for (int i = 0; i < methodComponent.getHandlerCount(); i++) {
ExceptionHandlerInfo exceptionHandlerInfo = new ExceptionHandlerInfoRead().load(in);
methodComponent.getExceptionHandlers().add(exceptionHandlerInfo);
}
methodComponent.setMethods(new ArrayList<MethodInfo>(methodComponent.getOffsets().size()));
if (!methodComponent.getOffsets().isEmpty()) {
// TODO: Rustine :s
if (methodComponent.getOffsets().get(0) != in.getByteRead()) {
MethodInfo m = null;
// TODO: Rustinev2 after 2 years old :s
if (methodComponent.getOffsets().get(1) <= in.getByteRead()) {
m = new MethodInfoRead().load(in, (short) in.getByteRead(), methodComponent.getOffsets().get(2));
methodComponent.getOffsets().remove(0);
methodComponent.getOffsets().remove(0);
} else {
m = new MethodInfoRead().load(in, (short) in.getByteRead(), methodComponent.getOffsets().get(0));
}
methodComponent.getMethods().add(m);
}
/*
* if (this.offsets.get(0) != (short) 0x0001) { // Read first
* method this.methods.add(new MethodInfo(in, (short) 0x0001,
* this.offsets.get(0)));
}
*/
// Read methods
for (int i = 0; i < (methodComponent.getOffsets().size() - 1); i++) {
MethodInfo m = new MethodInfoRead().load(in, methodComponent.getOffsets().get(i), methodComponent.getOffsets().get(i + 1));
methodComponent.getMethods().add(m);
}
// Read Last method
MethodInfo m = new MethodInfoRead().load(in, methodComponent.getOffsets().get(methodComponent.getOffsets().size() - 1), methodComponent.getSize());
methodComponent.getMethods().add(m);
}
checkSize(in, methodComponent);