My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 62 attachment: RecordStoreImpl.java (12.6 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
* MicroEmulator
* Copyright (C) 2001-2005 Bartek Teodorczyk <barteo@barteo.net>
*
* It is licensed under the following two licenses as alternatives:
* 1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
* 2. Apache License (the "AL") Version 2.0
*
* You may not use this file except in compliance with at least one of
* the above two licenses.
*
* You may obtain a copy of the LGPL at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*
* You may obtain a copy of the AL at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the LGPL or the AL for the specific language governing permissions and
* limitations.
*/

package org.microemu.util;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordListener;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotOpenException;

import org.microemu.RecordStoreManager;


public class RecordStoreImpl extends RecordStore
{
private static final byte[] fileIdentifier = { 0x4d, 0x49, 0x44, 0x52, 0x4d, 0x53 };

private static final byte versionMajor = 0x03;

private static final byte versionMinor = 0x00;

private int lastRecordId = 0;

private int size = 0;

private Hashtable records = new Hashtable();

private String recordStoreName;

private int version = 0;

private long lastModified = 0;

private transient boolean open;

private transient RecordStoreManager recordStoreManager;

private transient Vector recordListeners = new Vector();


public RecordStoreImpl(RecordStoreManager recordStoreManager, String recordStoreName)
{
this.recordStoreManager = recordStoreManager;
if (recordStoreName.length() <= 32) {
this.recordStoreName = recordStoreName;
} else {
this.recordStoreName = recordStoreName.substring(0, 32);
}
this.open = false;
}


public RecordStoreImpl(RecordStoreManager recordStoreManager)
throws IOException
{
this.recordStoreManager = recordStoreManager;
}


public int readHeader(DataInputStream dis)
throws IOException
{
for (int i = 0; i < fileIdentifier.length; i++) {
if (dis.read() != fileIdentifier[i]) {
throw new IOException();
}
}
dis.read(); // Major version number
dis.read(); // Minor version number
dis.read(); // Encrypted flag

recordStoreName = dis.readUTF();
lastModified = dis.readLong();
version = dis.readInt();
dis.readInt(); // TODO AuthMode
dis.readByte(); // TODO Writable
size = dis.readInt();

return size;
}


public void readRecord(DataInputStream dis)
throws IOException
{
int recordId = dis.readInt();
if (recordId > lastRecordId) {
lastRecordId = recordId;
}
dis.readInt(); // TODO Tag
byte[] data = new byte[dis.readInt()];
dis.read(data, 0, data.length);
this.records.put(new Integer(recordId), data);
}


public void writeHeader(DataOutputStream dos)
throws IOException
{
dos.write(fileIdentifier);
dos.write(versionMajor);
dos.write(versionMinor);
dos.write(0); // Encrypted flag

dos.writeUTF(recordStoreName);
dos.writeLong(lastModified);
dos.writeInt(version);
dos.writeInt(0); // TODO AuthMode
dos.writeByte(0); // TODO Writable
dos.writeInt(size);
}


public void writeRecord(DataOutputStream dos, int recordId)
throws IOException
{
dos.writeInt(recordId);
dos.writeInt(0); // TODO Tag
try {
byte[] data = getRecord(recordId);
if (data == null) {
dos.writeInt(0);
} else {
dos.writeInt(data.length);
dos.write(data);
}
} catch (RecordStoreException e) {
throw new IOException();
}
}


public boolean isOpen()
{
return open;
}


public void setOpen(boolean open)
{
this.open = open;
}


public void closeRecordStore()
throws RecordStoreNotOpenException, RecordStoreException
{
if (!open) {
throw new RecordStoreNotOpenException();
}

if (recordListeners != null) {
recordListeners.removeAllElements();
}
recordStoreManager.fireRecordStoreListener(ExtendedRecordListener.RECORDSTORE_CLOSE, this.getName());

if (recordStoreManager.cleanRecordsOnCloseRecordStore()) {
records.clear();
}

open = false;
}


public String getName()
throws RecordStoreNotOpenException
{
if (!open) {
throw new RecordStoreNotOpenException();
}

return recordStoreName;
}


public int getVersion()
throws RecordStoreNotOpenException
{
if (!open) {
throw new RecordStoreNotOpenException();
}

synchronized (this) {
return version;
}
}


public int getNumRecords()
throws RecordStoreNotOpenException
{
if (!open) {
throw new RecordStoreNotOpenException();
}

return size;
}


public int getSize()
throws RecordStoreNotOpenException
{
if (!open) {
throw new RecordStoreNotOpenException();
}

// TODO include size overhead such as the data structures used to hold the state of the record store

// Preload all records
enumerateRecords(null, null, false);

int result = 0;
Enumeration keys = records.keys();
while (keys.hasMoreElements()) {
int key = ((Integer) keys.nextElement()).intValue();
try {
byte[] data = getRecord(key);
if (data != null) {
result += data.length;
}
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
return result;
}


public int getSizeAvailable()
throws RecordStoreNotOpenException
{
if (!open) {
throw new RecordStoreNotOpenException();
}

return recordStoreManager.getSizeAvailable(this);
}


public long getLastModified()
throws RecordStoreNotOpenException
{
if (!open) {
throw new RecordStoreNotOpenException();
}

synchronized (this) {
return lastModified;
}
}


public void addRecordListener(RecordListener listener)
{
if (!recordListeners.contains(listener)) {
recordListeners.addElement(listener);
}
}


public void removeRecordListener(RecordListener listener)
{
recordListeners.removeElement(listener);
}


public int getNextRecordID()
throws RecordStoreNotOpenException, RecordStoreException
{
if (!open) {
throw new RecordStoreNotOpenException();
}

// lastRecordId needs to hold correct number, all records have to be preloaded
enumerateRecords(null, null, false);

synchronized (this) {
return lastRecordId + 1;
}
}


public int addRecord(byte[] data, int offset, int numBytes)
throws RecordStoreNotOpenException, RecordStoreException, RecordStoreFullException
{
if (!open) {
throw new RecordStoreNotOpenException();
}
if (data == null && numBytes > 0) {
throw new NullPointerException();
}
if (numBytes > recordStoreManager.getSizeAvailable(this)) {
throw new RecordStoreFullException();
}

// lastRecordId needs to hold correct number, all records have to be preloaded
enumerateRecords(null, null, false);

byte[] recordData = new byte[numBytes];
if (data != null) {
System.arraycopy(data, offset, recordData, 0, numBytes);
}

int nextRecordID = getNextRecordID();
synchronized (this) {
records.put(new Integer(nextRecordID), recordData);
version++;
lastModified = System.currentTimeMillis();
lastRecordId++;
size++;
}

recordStoreManager.saveRecord(this, nextRecordID);

fireRecordListener(ExtendedRecordListener.RECORD_ADD, nextRecordID);

return nextRecordID;
}


public void deleteRecord(int recordId)
throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException
{
if (!open) {
throw new RecordStoreNotOpenException();
}

synchronized (this) {
// throws InvalidRecordIDException when no record found
getRecord(recordId);
records.remove(new Integer(recordId));
version++;
lastModified = System.currentTimeMillis();
size--;
}

recordStoreManager.deleteRecord(this, recordId);

fireRecordListener(ExtendedRecordListener.RECORD_DELETE, recordId);
}


public int getRecordSize(int recordId)
throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException
{
if (!open) {
throw new RecordStoreNotOpenException();
}

synchronized (this) {
byte[] data = (byte[]) records.get(new Integer(recordId));
if (data == null) {
recordStoreManager.loadRecord(this, recordId);
data = (byte[]) records.get(new Integer(recordId));
if (data == null) {
throw new InvalidRecordIDException();
}
}

return data.length;
}
}


public int getRecord(int recordId, byte[] buffer, int offset)
throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException
{
int recordSize;
synchronized (this) {
recordSize = getRecordSize(recordId);
System.arraycopy(records.get(new Integer(recordId)), 0, buffer, offset, recordSize);
}

fireRecordListener(ExtendedRecordListener.RECORD_READ, recordId);

return recordSize;
}


public byte[] getRecord(int recordId)
throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException
{
if (!open) {
throw new RecordStoreNotOpenException();
}

byte[] data;

synchronized (this) {
data = new byte[getRecordSize(recordId)];
getRecord(recordId, data, 0);
}

return data.length < 1 ? null : data;
}


public void setRecord(int recordId, byte[] newData, int offset, int numBytes)
throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, RecordStoreFullException
{
if (!open) {
throw new RecordStoreNotOpenException();
}

// FIXME fixit
if (numBytes > recordStoreManager.getSizeAvailable(this)) {
throw new RecordStoreFullException();
}

byte[] recordData = new byte[numBytes];
System.arraycopy(newData, offset, recordData, 0, numBytes);

synchronized (this) {
// throws InvalidRecordIDException when no record found
getRecord(recordId);
records.put(new Integer(recordId), recordData);
version++;
lastModified = System.currentTimeMillis();
}

recordStoreManager.saveRecord(this, recordId);

fireRecordListener(ExtendedRecordListener.RECORD_CHANGE, recordId);
}


public RecordEnumeration enumerateRecords(RecordFilter filter, RecordComparator comparator, boolean keepUpdated)
throws RecordStoreNotOpenException
{
if (!open) {
throw new RecordStoreNotOpenException();
}

return new RecordEnumerationImpl(this, filter, comparator, keepUpdated);
}


public int getHeaderSize()
{
// TODO fixit
return recordStoreName.length() + 4 + 8 + 4;
}


public int getRecordHeaderSize()
{
return 4 + 4;
}


private void fireRecordListener(int type, int recordId)
{
long timestamp = System.currentTimeMillis();

if (recordListeners != null) {
for (Enumeration e = recordListeners.elements(); e.hasMoreElements();) {
RecordListener l = (RecordListener) e.nextElement();
if (l instanceof ExtendedRecordListener) {
((ExtendedRecordListener) l).recordEvent(type, timestamp, this, recordId);
} else {
switch (type) {
case ExtendedRecordListener.RECORD_ADD:
l.recordAdded(this, recordId);
break;
case ExtendedRecordListener.RECORD_CHANGE:
l.recordChanged(this, recordId);
break;
case ExtendedRecordListener.RECORD_DELETE:
l.recordDeleted(this, recordId);
}
}
}
}
}

}
Powered by Google Project Hosting