throws IOException {
byte[] writeKeyValue = keyConfig == null ? null : keyConfig.getWriteKeyValue();
MemoryLayout memoryLayout = readerWriter.getMemoryLayout();
MfClassicAccess accessTrailer = new MfClassicAccess(MfClassicConstants.MAD_KEY, 0,
memoryLayout.getTrailerBlockNumberForSector(0));
TrailerBlock madTrailer = (TrailerBlock)readerWriter.readBlock(accessTrailer)[0];
if ((madTrailer.getGeneralPurposeByte() & MadConstants.GPB_MAD_AVAILABLE) != 0) {
if ((madTrailer.getGeneralPurposeByte() & MadConstants.GPB_MAD_V1) == MadConstants.GPB_MAD_V1) {
madTrailer.setKey(Key.A, MadConstants.DEFAULT_MAD_KEY);
if (writeKeyValue != null)
madTrailer.setKey(Key.B, writeKeyValue);
Mad1 mad1 = new Mad1(readerWriter, keyConfig, madTrailer);
mad1.readMad();
if (writeKeyValue == null)
mad1.setReadonly();
return mad1;
}
else if ((madTrailer.getGeneralPurposeByte() & MadConstants.GPB_MAD_V2) == MadConstants.GPB_MAD_V2) {
madTrailer.setKey(Key.A, MadConstants.DEFAULT_MAD_KEY);
if (writeKeyValue != null)
madTrailer.setKey(Key.B, writeKeyValue);
Mad2 mad = new Mad2(readerWriter, keyConfig, madTrailer);
mad.readMad();
if (writeKeyValue == null)
mad.setReadonly();
return mad;