| Code with Finding: |
class JPCSCService {
/**
* Opens a session with the card.
* Selects a reader. Connects to the card.
*/
public void open() {
context = new Context();
context.EstablishContext(PCSC.SCOPE_GLOBAL, null, null);
card = null;
String[] readers = context.ListReaders();
if (readers.length == 1) {
/* Only one reader, take it. */
card = context.Connect();
} else {
/* Multiple readers, find the first one with our name. */
for (int i = 0; i < readers.length; i++) {
String reader = readers[i];
if (reader.startsWith(PREFERRED_READER_NAME)) {
card = context.Connect(reader);
if (card != null) {
break;
}
} else if (i + 1 == readers.length) {
/* Multiple readers, none with our name, take the last one. */
card = context.Connect(reader);
}
}
}
if (card == null) {
throw new IllegalStateException("No readers found!");
}
card.BeginTransaction();
state = SESSION_STARTED_STATE;
notifyStartedAPDUSession();
}
}
class JPCSCService {
public void open(String reader) {
context = new Context();
context.EstablishContext(PCSC.SCOPE_GLOBAL, null, null);
card = context.Connect(reader);
if (card == null) {
throw new IllegalStateException("Reader \"" + reader + "\" not found!");
}
card.BeginTransaction();
state = SESSION_STARTED_STATE;
notifyStartedAPDUSession();
}
}
|