0 Replies Latest reply: Oct 1, 2012 4:20 PM by 965525 RSS

    opencard framewoFileSystemCardService.create( ) results in "file not found"

    965525
      Hello, i´m using opencard framework to create a simple application to write a file in my smartcard, but i getting this error even following the OCF guide!

      opencard.opt.service.CardServiceObjectNotAvailableException: File not found

      Here is my program source:

      package org.xlib.cardTest.smartcard;

      import java.io.FileNotFoundException;

      import opencard.core.OpenCardException;
      import opencard.core.service.CardRequest;
      import opencard.core.service.CardServiceException;
      import opencard.core.service.SmartCard;
      import opencard.core.terminal.CardTerminalException;
      import opencard.core.util.OpenCardPropertyLoadingException;
      import opencard.opt.iso.fs.CardFile;
      import opencard.opt.iso.fs.CardFileInfo;
      import opencard.opt.iso.fs.CardFilePath;
      import opencard.opt.iso.fs.FileAccessCardService;
      import opencard.opt.iso.fs.FileSystemCardService;

      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;

      public class SmartCardReader {
           private static final Log log = LogFactory.getLog(SmartCardReader.class);

           private FileSystemCardService systemCardService;

           public SmartCardReader() {

           }

           public void readFile() throws SmartCardException {
                CardRequest cardRequest;
                SmartCard smartCard;
                FileAccessCardService cardService;
                byte[] data;

                try {
                     log.info("Reading smartcard file ...");

                     SmartCard.start();

                     // Here OCF can be used:

                     log.info("wait for a smartcard with file access support");
                     cardRequest = new CardRequest(CardRequest.ANYCARD, null,
                               FileAccessCardService.class);

                     smartCard = SmartCard.waitForCard(cardRequest);

                     // here, we will read from the smart card
                     cardService = (FileAccessCardService) smartCard.getCardService(
                               FileAccessCardService.class, true);

                     systemCardService = (FileSystemCardService) smartCard
                               .getCardService(FileSystemCardService.class, true);

                     log.info("writing and creating a file ...");
                     writeFile(cardService, ":c009", "Hello World!");

                     log.info("reading what we saved ...");
                     data = readFile(cardService, ":c009");

                     smartCard.close();

                     // printing the data can be done here
                     log.info(String.format("content stored in file ...", new String(
                               data)));

                } catch (OpenCardPropertyLoadingException e) {
                     log.error(e);

                     throw new SmartCardException(e);
                } catch (CardServiceException e) {
                     throwit(e);
                } catch (CardTerminalException e) {
                     throwit(e);
                } catch (ClassNotFoundException e) {
                     throwit(e);
                } finally {
                     shutdown();
                }

           }

           private void writeFile(FileAccessCardService cardService, String fileName,
                     String text) throws SmartCardException {
                CardFile cardFile;
                byte[] data;

                try {
                     cardFile = openCardFile(cardService, fileName, true);
                     data = new byte[cardFile.getLength()];
                     System.arraycopy(text.getBytes(), 0, data, 0, cardFile.getLength());

                     cardService.write(cardFile.getPath(), 0, data);
                } catch (CardServiceException e) {
                     throwit(e);
                } catch (CardTerminalException e) {
                     throwit(e);
                }
           }

           private byte[] readFile(FileAccessCardService cardService, String fileName)
                     throws SmartCardException {
                CardFile cardFile;
                byte[] data = null;

                try {
                     cardFile = openCardFile(cardService, fileName, false);
                     data = new byte[cardFile.getLength()];

                     data = cardService
                               .read(cardFile.getPath(), 0, cardFile.getLength());
                } catch (CardServiceException e) {
                     throwit(e);
                } catch (CardTerminalException e) {
                     throwit(e);
                }

                return data;
           }

           private CardFile openCardFile(FileAccessCardService cardService,
                     String fileName, boolean create) throws SmartCardException {
                CardFile root;
                CardFile cardFile = null;
                CardFilePath path;
                CardFileInfo fileInfo;

                try {
                     if (create) {
                          root = new CardFile(systemCardService);
      path = new CardFilePath(fileName);

      systemCardService.create(path, root.getHeader());
                     }

                     root = new CardFile(cardService);
                     cardFile = new CardFile(root, fileName);
                } catch (FileNotFoundException e) {
                     throwit(e);
                } catch (OpenCardException e) {
                     throwit(e);
                }

                return cardFile;
           }

           private void shutdown() throws SmartCardException {
                try {
                     SmartCard.shutdown();
                } catch (CardTerminalException e) {
                     throwit(e);
                }
           }

           private void throwit(Exception e) throws SmartCardException {
                log.error(e);
                throw new SmartCardException(e);
           }
      }