Search in sources :

Example 1 with RecordStoreException

use of javax.microedition.rms.RecordStoreException in project J2ME-Loader by nikita36078.

the class AndroidRecordStoreManager method saveToDisk.

/**
 * @param recordId -1 for storing only header
 */
private synchronized void saveToDisk(RecordStoreImpl recordStore, int recordId) throws RecordStoreException {
    try {
        DataOutputStream dos = new DataOutputStream(ContextHolder.openFileOutput(getHeaderFileName(recordStore.getName())));
        recordStore.writeHeader(dos);
        dos.close();
    } catch (IOException e) {
        Log.e(TAG, "RecordStore.saveToDisk: ERROR writting object to " + getHeaderFileName(recordStore.getName()), e);
        throw new RecordStoreException(e.getMessage());
    }
    if (recordId != -1) {
        try {
            DataOutputStream dos = new DataOutputStream(ContextHolder.openFileOutput(getRecordFileName(recordStore.getName(), recordId)));
            recordStore.writeRecord(dos, recordId);
            dos.close();
        } catch (IOException e) {
            Log.e(TAG, "RecordStore.saveToDisk: ERROR writting object to " + getRecordFileName(recordStore.getName(), recordId), e);
            throw new RecordStoreException(e.getMessage());
        }
    }
}
Also used : RecordStoreException(javax.microedition.rms.RecordStoreException) DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException)

Example 2 with RecordStoreException

use of javax.microedition.rms.RecordStoreException in project J2ME-Loader by nikita36078.

the class AndroidRecordStoreManager method openRecordStore.

@Override
public RecordStore openRecordStore(String recordStoreName, boolean createIfNecessary) throws RecordStoreException {
    initializeIfNecessary();
    RecordStoreImpl recordStoreImpl;
    try {
        DataInputStream dis = new DataInputStream(ContextHolder.openFileInput(getHeaderFileName(recordStoreName)));
        recordStoreImpl = new RecordStoreImpl(this);
        recordStoreImpl.readHeader(dis);
        recordStoreImpl.setOpen(true);
        dis.close();
    } catch (FileNotFoundException e) {
        if (!createIfNecessary) {
            throw new RecordStoreNotFoundException(recordStoreName);
        }
        recordStoreImpl = new RecordStoreImpl(this, recordStoreName);
        recordStoreImpl.setOpen(true);
        saveToDisk(recordStoreImpl, -1);
    } catch (IOException e) {
        throw new RecordStoreException();
    }
    recordStores.put(recordStoreName, recordStoreImpl);
    return recordStoreImpl;
}
Also used : RecordStoreException(javax.microedition.rms.RecordStoreException) FileNotFoundException(java.io.FileNotFoundException) RecordStoreNotFoundException(javax.microedition.rms.RecordStoreNotFoundException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 3 with RecordStoreException

use of javax.microedition.rms.RecordStoreException in project J2ME-Loader by nikita36078.

the class RecordStoreImpl method writeRecord.

public void writeRecord(DataOutputStream dos, int recordId) throws IOException {
    dos.writeInt(recordId);
    // TODO Tag
    dos.writeInt(0);
    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();
    }
}
Also used : RecordStoreException(javax.microedition.rms.RecordStoreException) IOException(java.io.IOException)

Example 4 with RecordStoreException

use of javax.microedition.rms.RecordStoreException in project J2ME-Loader by nikita36078.

the class AndroidRecordStoreManager method deleteFromDisk.

private synchronized void deleteFromDisk(RecordStoreImpl recordStore, int recordId) throws RecordStoreException {
    try {
        DataOutputStream dos = new DataOutputStream(ContextHolder.openFileOutput(getHeaderFileName(recordStore.getName())));
        recordStore.writeHeader(dos);
        dos.close();
    } catch (IOException e) {
        Log.e(TAG, "RecordStore.saveToDisk: ERROR writting object to " + getHeaderFileName(recordStore.getName()), e);
        throw new RecordStoreException(e.getMessage());
    }
    ContextHolder.deleteFile(getRecordFileName(recordStore.getName(), recordId));
}
Also used : RecordStoreException(javax.microedition.rms.RecordStoreException) DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException)

Example 5 with RecordStoreException

use of javax.microedition.rms.RecordStoreException in project J2ME-Loader by nikita36078.

the class AndroidRecordStoreManager method deleteRecordStore.

@Override
public void deleteRecordStore(final String recordStoreName) throws RecordStoreNotFoundException, RecordStoreException {
    initializeIfNecessary();
    Object value = recordStores.get(recordStoreName);
    if (value == null) {
        throw new RecordStoreNotFoundException(recordStoreName);
    }
    if (value instanceof RecordStoreImpl && ((RecordStoreImpl) value).isOpen()) {
        throw new RecordStoreException();
    }
    RecordStoreImpl recordStoreImpl;
    try {
        DataInputStream dis = new DataInputStream(ContextHolder.openFileInput(getHeaderFileName(recordStoreName)));
        recordStoreImpl = new RecordStoreImpl(this);
        recordStoreImpl.readHeader(dis);
        dis.close();
    } catch (IOException e) {
        Log.e(TAG, "RecordStore.deleteRecordStore: ERROR reading " + getHeaderFileName(recordStoreName), e);
        throw new RecordStoreException();
    }
    recordStoreImpl.setOpen(true);
    RecordEnumeration re = recordStoreImpl.enumerateRecords(null, null, false);
    while (re.hasNextElement()) {
        ContextHolder.deleteFile(getRecordFileName(recordStoreName, re.nextRecordId()));
    }
    recordStoreImpl.setOpen(false);
    ContextHolder.deleteFile(getHeaderFileName(recordStoreName));
    recordStores.remove(recordStoreName);
}
Also used : RecordStoreException(javax.microedition.rms.RecordStoreException) RecordEnumeration(javax.microedition.rms.RecordEnumeration) RecordStoreNotFoundException(javax.microedition.rms.RecordStoreNotFoundException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Aggregations

RecordStoreException (javax.microedition.rms.RecordStoreException)6 IOException (java.io.IOException)5 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 RecordEnumeration (javax.microedition.rms.RecordEnumeration)2 RecordStoreNotFoundException (javax.microedition.rms.RecordStoreNotFoundException)2 FileNotFoundException (java.io.FileNotFoundException)1 Enumeration (java.util.Enumeration)1 RecordStoreNotOpenException (javax.microedition.rms.RecordStoreNotOpenException)1