/* * Copyright © 2013 Nokia Corporation. All rights reserved. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. * Oracle and Java are trademarks or registered trademarks of Oracle and/or its * affiliates. Other product and company names mentioned herein may be trademarks * or trade names of their respective owners. * See LICENSE.TXT for license information. */ package com.nokia.example; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import javax.microedition.rms.RecordStore; /** * MIDlet for benchmarking different record store access strategies. */ public class RmsMidlet extends MIDlet implements CommandListener { private Command exit; private Command run; private Command delete; private Form form; private long startTime; private long stopTime; private RmsConnection basicRms = new BasicRms(); private RmsConnection badRms = new BadRms(); private RmsConnection cacheRms = new CacheRms(); private RmsConnection fileRms = new FileRms(); private final String recordData = "some dummy data for record"; public RmsMidlet() { form = new Form("RMS"); exit = new Command("Exit", Command.EXIT, 1); run = new Command("Run", Command.SCREEN, 1); delete = new Command("Delete", Command.SCREEN, 1); form.addCommand(exit); form.addCommand(run); form.addCommand(delete); form.setCommandListener(this); } public void startApp() { Display.getDisplay(this).setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command command, Displayable displayable) { if (command == exit) { destroyApp(true); notifyDestroyed(); } else if (command == run) { new Thread(new Runnable() { public void run() { execute(); } }).start(); } else if (command == delete) { new Thread(new Runnable() { public void run() { reset(); } }).start(); } } private void log(String msg) { form.append(msg + "\n"); } private void clear() { form.deleteAll(); } private void execute() { init(); log("Starting test..."); String simpleResult = test(basicRms); log("Basic RecordStore:\n" + simpleResult); String badResult = test(badRms); log("Bad RecordStore:\n" + badResult); String cacheResult = test(cacheRms); log("Cache RecordStore:\n" + cacheResult); String fileResult = test(fileRms); log("File RecordStore:\n" + fileResult); log("Test done"); } private void init() { clear(); String[] stores = RecordStore.listRecordStores(); if (stores == null) { log("Populating recordstores..."); populateStore(basicRms); populateStore(badRms); populateStore(cacheRms); populateStore(fileRms); log("Populating recordstores done"); } } private void populateStore(RmsConnection rms) { try { rms.open(); rms.set(recordData.getBytes()); } catch (Exception e) { log("Can not populate recordstores with the " + rms.getClass() + "!"); } finally { rms.close(); } } private void reset() { clear(); log("Deleting recordstores..."); // Remove normal record stores String[] stores = RecordStore.listRecordStores(); for (int i = 0; stores != null && i < stores.length; i++) { try { RecordStore.deleteRecordStore(stores[i]); } catch (Exception e) { log("Deleting of the recordstore " + stores[i] + " failed!"); } } // Remove file record store try { ((FileRms) fileRms).delete(); } catch (Exception e) { log("Deleting of the recordstore FileRms failed!"); } log("Deleting recordstores done"); } private String test(RmsConnection rms) { String rtn = ""; try { startTiming(); rms.open(); for (int i = 0; i < 100; i++) { byte[] data = rms.get(); String out = new String(data); // sanity check - what goes in must come out checkEquals(recordData, out); } rms.close(); stopTiming(); rtn = getTime(); } catch (SecurityException e) { rtn = "Not allowed to test"; } catch (DataMatchException e) { rtn = "Tested data does not match"; } catch (Exception e) { rtn = "Error in running the test"; } finally { rms.close(); } return rtn; } private class DataMatchException extends RuntimeException { } private void checkEquals(String expected, String actual) { if (expected.compareTo(actual) != 0) { throw new DataMatchException(); } } private void startTiming() { startTime = System.currentTimeMillis(); } private void stopTiming() { stopTime = System.currentTimeMillis(); } private String getTime() { return (new Long(stopTime - startTime)).toString() + " ms"; } }