Question

Is it possible to write a byte[] in a column of RecordStore? For example I want to write a record which consists of byte[] in first column, String in second column and int in third column.

public void writeToRms(byte[] byteArray){
    RecordStore recordStore = RecordStore.openRecordStore("My recordStore", true);
    try{
        byte[] outputRecord;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        // i want to write the parameter **byteArray** here. What method should I use?
        dos.writeUTF("Name");
        dos.writeInt(ctr);
        dos.flush();
        outputRecord = baos.toByteArray();
        recordStore.addRecord(outputRecord, 0, outputRecord.length);
        recordStore.closeRecordStore();
        baos.reset();
        baos.close();
        dos.close();
    }catch(Exception e){}
}
Was it helpful?

Solution

Sure. The basic code to write out the byte[] is this:

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);

    // i want to write **byteArray** here. What method should I use?
    dos.writeShort(byteArray.length);
    dos.write(byteArray);

    dos.writeUTF("Name");
    dos.writeInt(ctr);
    dos.flush();
    outputRecord = baos.toByteArray();

and then when you read them back in again, you must first read the length of the array, and then the array. Like this:

    RecordStore recordStore = RecordStore.openRecordStore("My recordStore", false);
    RecordEnumeration records = recordStore.enumerateRecords(null, null, true);
    while (records.hasNextElement()) {
       byte[] recordData = records.nextRecord();

       ByteArrayInputStream bais = new ByteArrayInputStream(recordData);
       DataInputStream dis = new DataInputStream(bais);

       short arrayLength = dis.readShort();
       byte[] bytes = new byte[arrayLength];
       dis.read(bytes, 0, arrayLength);

I think there's a good RMS example from BlackBerry here ... take a look at CD.java. I know this is a general J2ME question, but I don't think BlackBerry used any RIM-specific libraries here ... it's just standard J2ME.

Note: this general technique works with any kind of array, not just byte[]. In order to save it to the record store, you first write out a short, indicating how long the array is, and then you write out the array itself. When you read it back in, you first read a short value, telling you how many array elements to expect, and then you read in the array, knowing how long it should be.

This is actually how RMS itself reads and write strings, using readUTF() and writeUTF(). It writes a 2-byte short value indicating the string length, and then the array of chars.

OTHER TIPS

Got it, another solution. If I want to write byte[] in one of the column of RMS all I need to do is encode first the byte[] to a string. Base64 specifically then write the String to the RMS.

public void writeToRms(byte[] byteArray){
    RecordStore recordStore = RecordStore.openRecordStore("My recordStore", true);
    try{
        byte[] outputRecord;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        String encoded = Base64.encode(byteArray);
        dos.writeUTF(encoded);
        dos.writeUTF("Name");
        dos.writeInt(ctr);
        dos.flush();
        outputRecord = baos.toByteArray();
        recordStore.addRecord(outputRecord, 0, outputRecord.length);
        recordStore.closeRecordStore();
        baos.reset();
        baos.close();
        dos.close();
    }catch(Exception e){}
}

And when I retrieve it I just need to decode the encoded String back to byte[].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top