View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
4    * 
5    *  Licensed to the Apache Software Foundation (ASF) under one
6    *  or more contributor license agreements.  See the NOTICE file
7    *  distributed with this work for additional information
8    *  regarding copyright ownership.  The ASF licenses this file
9    *  to you under the Apache License, Version 2.0 (the
10   *  "License"); you may not use this file except in compliance
11   *  with the License.  You may obtain a copy of the License at
12   *
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing,
16   *  software distributed under the License is distributed on an
17   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   *  KIND, either express or implied.  See the License for the
19   *  specific language governing permissions and limitations
20   *  under the License.
21   *
22   *  @author vlads
23   *  @version $Id: StorageRMS.java 2607 2008-12-17 23:51:33Z skarzhevskyy $
24   */ 
25  package net.sf.bluecove.util;
26  
27  import javax.microedition.rms.RecordStore;
28  import javax.microedition.rms.RecordStoreException;
29  
30  import org.bluecove.tester.log.Logger;
31  
32  
33  /**
34   *
35   */
36  public class StorageRMS implements Storage {
37  
38  	public String retriveData(String name) {
39  		RecordStore recordStore = null;
40  		try {
41  			recordStore = RecordStore.openRecordStore("BlueCove" + name, false);
42  			if (recordStore.getNumRecords() > 0) {
43  				int recordId = 1;
44  				byte[] data = recordStore.getRecord(recordId);
45  				return new String(data);
46  			} else {
47  				// recordStore empty
48  				return null;
49  			}
50  		} catch (Throwable e) {
51  			Logger.error("error accessing RecordStore", e);
52  			return null;
53  		} finally {
54  			closeQuietly(recordStore);
55  		}
56  	}
57  
58  	public void storeData(String name, String value) {
59  		RecordStore recordStore = null;
60  		try {
61  			recordStore = RecordStore.openRecordStore("BlueCove" + name, true);
62  			byte[] data = value.getBytes();
63  			int recordId;
64  			if (recordStore.getNumRecords() > 0) {
65  				recordId = 1;
66  				recordStore.setRecord(recordId, data, 0, data.length);
67  			} else {
68  				recordId = recordStore.addRecord(data, 0, data.length);
69  			}
70  		} catch (Throwable e) {
71  			Logger.error("error accessing RecordStore", e);
72  		} finally {
73  			closeQuietly(recordStore);
74  		}
75  	}
76  
77  	public static void closeQuietly(RecordStore recordStore) {
78  		try {
79  			if (recordStore != null) {
80  				recordStore.closeRecordStore();
81  			}
82  		} catch (RecordStoreException ignore) {
83  			// ignore
84  		}
85  	}
86  }