×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Gurgaon, Haryana
    Posts
    62

    Arrow Appending another byte array to the same record in a record store

    Hi,
    I want to append a byte array in a previously created record in a record store. Eg: I have a string "Nokia" in a record in a recordstore. I want to add "Forum" in it, to make it "NokiaForum". I tried using recordStore.setRecord(iD, byte[], offset); but it always overwrited the record.

    Then I used this method :
    Code:
    public static void setRecord(int id, String dataToAppend, int offset){
            try {
                byte[] outputRecord;
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                DataOutputStream outputDataStream = new DataOutputStream(outputStream);
    
                    outputDataStream.writeUTF(replacement);
                    outputDataStream.flush();
                    outputRecord = outputStream.toByteArray();
                    if(recordStore.getNumRecords()>0){
                        int ar = recordStore.getRecord(1, outputRecord, outputRecord.length);
                        System.out.println("number of bytes added"+ar);
                }
                    recordStore.setRecord(1, outputRecord, 0, outputRecord.length);
                outputStream.reset();
                outputStream.close();
                outputDataStream.close();
            } catch (Exception error) {
                error.printStackTrace();
            }
        }
    But this gives array index out of bounds exception. Please someone provide some code to achieve this.

    Thanks,

  2. #2
    Super Contributor
    Join Date
    Mar 2005
    Location
    Paris
    Posts
    814

    Re: Appending another byte array to the same record in a record store

    read again the getRecord method documentation.
    what is replacement ?

    what you want to do is basically :
    open the record store, fetch your record, measure the length of it, measure the length of the data to append, create a new byte array long enougth to store both, copy your record at the begining, copy your new data after, set the record, close the record store.

  3. #3
    Nokia Developer Champion
    Join Date
    Aug 2009
    Posts
    18

    Re: Appending another byte array to the same record in a record store

    Code:
    public void setRecord(RecordStore rs, int id, String dataToAppend) throws Exception
    	{
    		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    		byte[] data = rs.getRecord(id);
    
    		baos.write(data);
    		baos.write(dataToAppend.getBytes());
    		data = baos.toByteArray();
    
    		rs.setRecord(id, data, 0, data.length);
    	}

Similar Threads

  1. Record store
    By nishaks in forum Mobile Java Tools & SDKs
    Replies: 3
    Last Post: 2010-02-19, 06:17
  2. How to play & record audio byte arrays with j2me on nokia
    By mmbabele in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2009-12-10, 12:19
  3. Encrypting data to store in record store
    By matan_d in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2009-01-04, 01:21
  4. RECORD STORE
    By jinda007 in forum Mobile Java General
    Replies: 7
    Last Post: 2006-03-15, 09:15
  5. Record Store vs JDBC. How to store data?
    By 180ºrevolution in forum Mobile Java General
    Replies: 0
    Last Post: 2004-09-17, 15:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×