📄 courier.java
字号:
// Copyright 2002 Nokia Corporation.
//
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
// BY THIRD PARTIES
//
// Furthermore, information provided in this source code is preliminary,
// and may be changed substantially prior to final release. Nokia Corporation
// retains the right to make changes to this source code at
// any time, without notice. This source code is provided for informational
// purposes only.
//
// Nokia and Nokia Connecting People are registered trademarks of Nokia
// Corporation.
// Java and all Java-based marks are trademarks or registered trademarks of
// Sun Microsystems, Inc.
// Other product and company names mentioned herein may be trademarks or
// trade names of their respective owners.
//
// A non-exclusive, non-transferable, worldwide, limited license is hereby
// granted to the Licensee to download, print, reproduce and modify the
// source code. The licensee has the right to market, sell, distribute and
// make available the source code in original or modified form only when
// incorporated into the programs developed by the Licensee. No other
// license, express or implied, by estoppel or otherwise, to any other
// intellectual property rights is granted herein.
package example.delivery;
import java.util.*;
import javax.microedition.rms.*;
// The Courier class is a singleton which wraps the 'Courier'
// record store. There are two records in that record store.
// After the records are first created in the new
// record store, no new records are ever added. So the recordIds
// always have known fixed values between 1 and 2.
// See the recordId index values defined below.
public class Courier
{
// recordId's for USERNAME and PASSWORD
private static final int USERNAME = 1;
private static final int PASSWORD = 2;
private static final byte[] empty = new byte[0];
public static final String RSNAME = "Courier";
private static RecordStore rs = null;
private static Courier instance = null;
// The unique singleton instance of this class.
static Courier getInstance()
throws RecordStoreException
{
if (instance == null)
{
instance = new Courier();
}
return instance;
}
private Courier()
throws RecordStoreException
{
openRecordStore();
if (rs.getNumRecords() == 0)
{
init(null, null);
}
}
static void openRecordStore()
throws RecordStoreException
{
// At most, one rs should ever be open.
if (rs == null)
{
rs = RecordStore.openRecordStore(RSNAME, true);
}
}
static void closeRecordStore()
throws RecordStoreException
{
if (rs != null)
{
rs.closeRecordStore();
rs = null;
}
}
// Method newRecordStore can also be used to delete all records too.
private void newRecordStore()
throws RecordStoreException
{
// Note: To perform a deleteRecordStore, rs should first be closed as
// many times as it was opened. In this class, that's at most once.
closeRecordStore();
try
{
RecordStore.deleteRecordStore(RSNAME);
}
catch(RecordStoreNotFoundException e)
{
// It may not exist the first time newRecordStore is called.
}
openRecordStore();
}
private void init(String username, String password)
throws RecordStoreException
{
// The method adds records to the record store in the order that
// matches the fixed recordId values defined above.
newRecordStore();
addRecord(username); // recordId USERNAME (1)
addRecord(password); // recordId PASSWORD (2)
}
String getUsername()
throws RecordStoreException
{
byte[] barray = rs.getRecord(USERNAME);
if (barray == null || barray.length == 0)
{
return "";
}
else
{
return new String(barray);
}
}
void setUsername(String str)
throws RecordStoreException
{
setRecord(USERNAME, str);
}
String getPassword()
throws RecordStoreException
{
byte[] barray = rs.getRecord(PASSWORD);
if (barray == null || barray.length == 0)
{
return "";
}
else
{
return new String(barray);
}
}
void setPassword(String str)
throws RecordStoreException
{
setRecord(PASSWORD, str);
}
private void addRecord(String str)
throws RecordStoreException
{
if (str == null || str.length() == 0)
{
rs.addRecord(null, 0, 0);
}
else
{
rs.addRecord(str.getBytes(), 0, str.length());
}
}
private void setRecord(int ix, String str)
throws RecordStoreException
{
if (str == null || str.length() == 0)
{
rs.setRecord(ix, empty, 0, 0);
}
else
{
rs.setRecord(ix, str.getBytes(), 0, str.length());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -