java.lang.Object
com.pi4j.crowpi.components.internal.rfid.RfidCard
Direct Known Subclasses:
Mifare1K

public abstract class RfidCard extends Object
Abstract base class to be implemented by all supported RFID cards. Provides various helper methods to abstract away internal details of various PICC types.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Logger
    Logger instance
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new RFID card instance for the given PICC UID.
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract int
    Returns the maximum capacity in bytes this card can store.
    Returns the human-readable serial of this card, based on the UID.
    protected RfidCardUid
    Returns the UID of this card.
    protected abstract byte[]
    Reads all available blocks from the card and returns a byte array.
    Reads a single Java object from the card without casting the result into a more-specific type.
    <T> T
    readObject(Class<T> type)
    Reads a single Java object from the card by deserializing a GZIP-compressed byte stream previously written onto the card.
    protected abstract void
    writeBytes(byte[] data)
    Writes the given data to the card, using as many blocks as needed.
    void
    Stores a single Java object onto the card by serializing the passed object and writing a GZIP-compressed byte stream.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • logger

      protected final Logger logger
      Logger instance
  • Constructor Details

    • RfidCard

      public RfidCard(RfidCardUid uid)
      Creates a new RFID card instance for the given PICC UID.
      Parameters:
      uid - UID of PICC
  • Method Details

    • getSerial

      public String getSerial()
      Returns the human-readable serial of this card, based on the UID.
      Returns:
      Human-readable card serial
    • writeObject

      public void writeObject(Object data) throws RfidException
      Stores a single Java object onto the card by serializing the passed object and writing a GZIP-compressed byte stream. While this method may be called multiple times, only a single object can be stored at once on the card. Please note that the capacity of RFID cards is limited, so this command might fail if the given object is too large.
      Parameters:
      data - Serializable object to be stored on card
      Throws:
      RfidException - Object serialization or write process failed
    • readObject

      public Object readObject() throws RfidException
      Reads a single Java object from the card without casting the result into a more-specific type.
      Throws:
      RfidException
      See Also:
    • readObject

      public <T> T readObject(Class<T> type) throws RfidException
      Reads a single Java object from the card by deserializing a GZIP-compressed byte stream previously written onto the card. This method can only read a single object from the card, so calling it multiple times results in the same value every time. The deserialized object is automatically casted into the given type to simplify usage.
      Type Parameters:
      T - Target type for deserialized object, determined by type parameter.
      Parameters:
      type - Class instance of target type for deserialized object
      Returns:
      Deserialized object read from card
      Throws:
      RfidException - Object deserialization or read process failed
    • getUid

      protected RfidCardUid getUid()
      Returns the UID of this card.
      Returns:
      Card/PICC UID
    • getCapacity

      public abstract int getCapacity()
      Returns the maximum capacity in bytes this card can store. This method should already subtract all unavailable blocks / sectors, e.g. trailer blocks.
      Returns:
      Maximum capacity in bytes
    • readBytes

      protected abstract byte[] readBytes() throws RfidException
      Reads all available blocks from the card and returns a byte array. This method targets the same blocks as writeBytes(byte[]) does.
      Returns:
      Byte array of available data on card
      Throws:
      RfidException - Reading data from card failed
    • writeBytes

      protected abstract void writeBytes(byte[] data) throws RfidException, IllegalArgumentException
      Writes the given data to the card, using as many blocks as needed. This method will only write to safe blocks and does not overwrite any internal blocks.
      Parameters:
      data - Data to write to the card
      Throws:
      RfidException - Writing data to card failed
      IllegalArgumentException - Given data exceeds card capacity