⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sortedrms.java

📁 此文件是关于手机游戏开发的理论
💻 JAVA
字号:
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;

/**
 * A simple example that demonstrates sorting a record store.
 * @author Martin J. Wells
 */
public class SortedRMS extends MIDlet
{
   private RecordStore rs;
   private static final String STORE_NAME = "My Record Store";

   /**
    * An inner class implementing the RecordComparator interface.
    */
   class StringComparator implements RecordComparator
   {
      /**
       * The overriden compare method orders the records according to our
       * sorting system by returning whether one record is logically before,
       * after or the same as another. It's up to you how you define what that
       * means for your needs.
       * @param bytes the first record
       * @param bytes1 the second record
       * @return an integer representing whether the first record is either
       * before, after or the same as the second record
       */
      public int compare(byte[] bytes, byte[] bytes1)
      {
         String value = getStringValue(bytes);
         String value1 = getStringValue(bytes1);

         if (value.compareTo(value1) < 0) return PRECEDES;
         if (value.compareTo(value1) > 0) return FOLLOWS;
         return EQUIVALENT;
      }
   }

   /**
    * An example of a custom RecordFilter that filters any record that contains
    * a specified string.
    */
   class StringFilter implements RecordFilter
   {
      private String mustContainString;

      /**
       * Constructor for the filter that grabs the string we need when filtering
       * @param mustContain string that the record must contain in order to be
       * allowed through this filter
       */
      public StringFilter(String mustContain)
      {
         // save the match string
         mustContainString = mustContain;
      }

      /**
       * The RecordFilter interface method that is called to determine if a
       * particular record is able to pass through the fitler.
       * @param bytes the record
       * @return true if the record passes the filter
       */
      public boolean matches(byte[] bytes)
      {
         // check if our string is in the record
         if (getStringValue(bytes).indexOf(mustContainString) == -1)
            return false;
         return true;
      }
   }

   /**
    * Constructor for the example MIDlet that loads a record store (which
    * must have previous been created) then enumerates the records in sorted
    * order and filters out only entries that contain an "o".
    * @throws Exception
    */
   public SortedRMS() throws Exception
   {
      // Open (and optionally create a record store for our data
      rs = RecordStore.openRecordStore(STORE_NAME, true);

      System.out.println("Record store has " + rs.getNumRecords() +
                         " record(s) using " + rs.getSize() + " byte(s) " +
                         "[" + rs.getSizeAvailable() + " bytes free]");

      RecordEnumeration enum = rs.enumerateRecords(new StringFilter("o"),
                                                   new StringComparator(),
                                                   false);

      while (enum.hasNextElement())
      {
         byte[] record = enum.nextRecord();

         // construct a byte and wrapping data stream to read back the
         // java types from the binary format
         ByteArrayInputStream byteInputStream = new ByteArrayInputStream(record);
         DataInputStream dataInputStream = new DataInputStream(byteInputStream);

         String value = dataInputStream.readUTF();
         // ... add other dataOutputStream.readXXX statements here matching the
         // order they were written above

         System.out.println("Retrieved value " + value);

         dataInputStream.close();
         byteInputStream.close();
      }
      enum.destroy();
   }

   /**
    * Called by the Application Manager when the MIDlet is starting or resuming
    * after being paused. In this case we just exit as soon as we start.
    * @throws MIDletStateChangeException
    */
   protected void startApp() throws MIDletStateChangeException
   {
      destroyApp(false);
      notifyDestroyed();
   }

   /**
    * Called by the MID's Application Manager to pause the MIDlet. A good
    * example of this is when the user receives an incoming phone call whilst
    * playing your game. When they're done the Application Manager will call
    * startApp to resume. For this example we don't need to do anything.
    */
   protected void pauseApp()
   {
   }

   /**
    * Called by the MID's Application Manager when the MIDlet is about to
    * be destroyed (removed from memory). You should take this as an opportunity
    * to clear up any resources and save the game. For this example we don't
    * need to do anything.
    * @param unconditional if false you have the option of throwing a
    * MIDletStateChangeException to abort the destruction process.
    * @throws MIDletStateChangeException
    */
   protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
   {
   }

   /**
    * Utility method that converts a byte array into a more usable String
    * @param record bytes array to convert
    * @return string result
    */
   private String getStringValue(byte[] record)
   {
      try
      {
         ByteArrayInputStream byteInputStream = new ByteArrayInputStream(record);
         DataInputStream dataInputStream = new DataInputStream(byteInputStream);
         return dataInputStream.readUTF();
      }
      catch (Exception e)
      {
         System.out.println(e.toString());
         return "";
      }
   }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -