Skip to content

Commit

Permalink
rms: Account for cases where apps want to add zero-length data
Browse files Browse the repository at this point in the history
Apotheosize (240x320, md5: 190c1d339a45bc80526cb694035b7030) fails
to save in all circumstances if it fails to set up its initial
record tables on boot, which are all zero-length. Previously FreeJ2ME
would throw a RecordStoreException in those cases, but as per the
java docs, ME apps are allowed to save zero-length data.
  • Loading branch information
AShiningRay committed Sep 16, 2024
1 parent 215b037 commit 3563e83
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/javax/microedition/rms/RecordStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,31 +194,37 @@ private void setUInt16(byte[] data, int offset, int val)
data[offset+1] = (byte)((val) & 0xFF);
}

public int addRecord(byte[] data, int offset, int numBytes) throws RecordStoreException
public int addRecord(byte[] data, int offset, int numBytes) throws RecordStoreNotOpenException, RecordStoreException, RecordStoreFullException
{
//System.out.println("> Add Record "+nextid+ " to "+name);
try
{
byte[] rec = Arrays.copyOfRange(data, offset, offset+numBytes);
records.addElement(rec);
// Only try to copy data if there's data to begin with, some apps may try to store a record with zero-length data
if(data != null && data.length != 0)
{
if(offset < 0 || numBytes < 0 || offset + numBytes > data.length) { throw new ArrayIndexOutOfBoundsException("Tried to access invalid record data position"); }

byte[] rec = Arrays.copyOfRange(data, offset, offset+numBytes);
records.addElement(rec);
}
else
{
// offset and numBytes aren't even taken into account here, since the data will have zero-length anyway. Apotheosize relies on this to set up its records on boot
byte[] rec = {};
records.addElement(rec);
}

lastModified = nextid;
nextid++;
version++;

save();

for(int i=0; i<listeners.size(); i++)
{
listeners.get(i).recordAdded(this, lastModified);
}
for(int i=0; i<listeners.size(); i++) { listeners.get(i).recordAdded(this, lastModified); }

return lastModified;
}
catch (Exception e)
{
//System.out.println("> Add Record Failed");
throw(new RecordStoreException("Can't Add RMS Record"));
}
catch (Exception e) { throw(new RecordStoreException("Can't Add RMS Record: " + e.getMessage())); }
}

public void addRecordListener(RecordListener listener)
Expand Down

0 comments on commit 3563e83

Please sign in to comment.