Skip to main content

J2ME : RecordStore related

5 replies [Last post]
Anonymous

Hello ,
I am trying to save the video file into RecordStore . But the following code raises an exception.
Can anyone clarify me on this.

public void addRecord(InputStream inputStream)
{
try
{
System.out.println("Record store size is :: "+store.getNumRecords());
int i = 0;
while(inputStream.read() != -1)
i++;
byte[] b = new byte[i];
System.out.println("Allocated "+i+" bytes");
int offset =0;
int numRead = 0;
while(offset < b.length && (numRead=inputStream.read(b, offset, b.length-offset)) >= 0)
{
offset += numRead;
}
System.out.println("Read the offset");
inputStream.close();

ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
System.out.println("Writing to dout");
dout.write(b);
System.out.println("Adding record to record store");
store.addRecord(bout.toByteArray(), 0,bout.toByteArray().length); // Add record
System.out.println("Record store size is :: "+store.getNumRecords());

store.closeRecordStore();
}
catch (RecordStoreFullException e)
{
System.out.println("Message 1 is :: "+e.getMessage());
e.printStackTrace();
}
catch (RecordStoreNotFoundException e)
{
System.out.println("Message 2 is :: "+e.getMessage());
e.printStackTrace();
}
catch (RecordStoreException e)
{
System.out.println("Message 3 is :: "+e.getMessage());
e.printStackTrace();
}
catch(Exception ex)
{
System.out.println("Message 4 is :: "+ex.getMessage());
ex.printStackTrace();
}
}

With Best Regards,
Raghavendra. K. Naik
Analyst - ISI Practice
PA Consulting Group
* Nitesh Broadway, M.G.Road, Bangalore
> * +91 25318855 - ext. 28381
> * Raghavendra.Naik@paconsulting.com
> * http://www.paconsulting.com
>
>

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff KVM-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Raghavendra K. Naik

Hello stefan,
Thanks for ur reply. But in the mark function for inputstream I need to
provide the readlimit.
Wht value do I need to specify for this ?

With Best Regards,
Raghavendra. K. Naik
Analyst - ISI Practice
PA Consulting Group
* Nitesh Broadway, M.G.Road, Bangalore
>* +91 25318855 - ext. 28381
>* Raghavendra.Naik@paconsulting.com
>* http://www.paconsulting.com
>

-----Original Message-----
From: A mailing list for KVM discussion
[mailto:KVM-INTEREST@JAVA.SUN.COM] On Behalf Of Stefan Haustein
Sent: 30 November 2006 19:08
To: KVM-INTEREST@JAVA.SUN.COM
Subject: Re: J2ME : RecordStore related

Hi Raghavendra,

you are reading the stream to the end just to get the size.

Use mark/reset or better copy the stream to a ByteArrayOutputStream and
use toByteArray().

Best regards,
Stefan

Raghavendra K. Naik wrote:

> Hello ,
> I am trying to save the video file into RecordStore . But the
> following code raises an exception.
> Can anyone clarify me on this.
>
>
> * public** void* addRecord(InputStream inputStream)
> {
> * try*
> {
> System./out/.println("Record store size is ::
> "+store.getNumRecords());
> * int* i = 0;
> * while*(inputStream.read() != -1)
> i++;
> * byte*[] b =* new** byte*[i];
> System./out/.println("Allocated "+i+" bytes");
> * int* offset =0;
> * int* numRead = 0;
> * while*(offset < b.length &&
> (numRead=inputStream.read(b, offset, b.length-offset)) >= 0)
> {
> offset += numRead;
> }
> System./out/.println("Read the offset");
> inputStream.close();
>
> ByteArrayOutputStream bout =* new*
> ByteArrayOutputStream();
> DataOutputStream dout =* new* DataOutputStream(bout);
> System./out/.println("Writing to dout");
> dout.write(b);
> System./out/.println("Adding record to record store");
> store.addRecord(bout.toByteArray(),
> 0,bout.toByteArray().length); // Add record
> System./out/.println("Record store size is ::
> "+store.getNumRecords());
>
> store.closeRecordStore();
> }
> * catch* (RecordStoreFullException e)
> {
> System./out/.println("Message 1 is ::
> "+e.getMessage());
> e.printStackTrace();
> }
> * catch* (RecordStoreNotFoundException e)
> {
> System./out/.println("Message 2 is ::
> "+e.getMessage());
> e.printStackTrace();
> }
> * catch* (RecordStoreException e)
> {
> System./out/.println("Message 3 is ::
> "+e.getMessage());
> e.printStackTrace();
> }
> * catch*(Exception ex)
> {
> System./out/.println("Message 4 is ::
> "+ex.getMessage());
> ex.printStackTrace();
> }
> }
>
> *With Best Regards,*
> *Raghavendra. K. Naik*
> *Analyst - ISI Practice*
> *PA Consulting Group*
> ? Nitesh Broadway, M.G.Road, Bangalore
> ( +91 25318855 - ext. 28381
> ** * Raghavendra.Naik@paconsulting.com
> 8 _http://www.paconsulting.com_
>
>
========================================================================
===
> To unsubscribe, send email to listserv@java.sun.com and include in the
> body of the message "signoff KVM-INTEREST". For general help, send
> email to listserv@java.sun.com and include in the body of the message
> "help".

========================================================================
===
To unsubscribe, send email to listserv@java.sun.com and include in the
body
of the message "signoff KVM-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff KVM-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Stefan Haustein

Hi Raghavendra,

the read limit depends on the maximum amount of data you are going to
read from the stream, so its a kind of catch-22.

You are probably better off with copying the content in large chunks to
a ByteArrayOutputStream -- reading single bytes from a system provided
stream will make your application crawl on some platforms (it goes all
the way down to native IO and then back to java).

BTW: The internal byte array of the ByteArrayOutputStream is accessible
by subclasses (protected), so it may make sense to implement a subclass.
However, this will not improve the worst case memory consumption (more
than twice the size of the array).

Best regards,
Stefan

Raghavendra K. Naik wrote:
> Hello stefan,
> Thanks for ur reply. But in the mark function for inputstream I need to
> provide the readlimit.
> Wht value do I need to specify for this ?
>
> With Best Regards,
> Raghavendra. K. Naik
> Analyst - ISI Practice
> PA Consulting Group
> * Nitesh Broadway, M.G.Road, Bangalore
>
>> * +91 25318855 - ext. 28381
>> * Raghavendra.Naik@paconsulting.com
>> * http://www.paconsulting.com
>>
>>
>
> -----Original Message-----
> From: A mailing list for KVM discussion
> [mailto:KVM-INTEREST@JAVA.SUN.COM] On Behalf Of Stefan Haustein
> Sent: 30 November 2006 19:08
> To: KVM-INTEREST@JAVA.SUN.COM
> Subject: Re: J2ME : RecordStore related
>
> Hi Raghavendra,
>
> you are reading the stream to the end just to get the size.
>
> Use mark/reset or better copy the stream to a ByteArrayOutputStream and
> use toByteArray().
>
> Best regards,
> Stefan
>
> Raghavendra K. Naik wrote:
>
>
>
>> Hello ,
>> I am trying to save the video file into RecordStore . But the
>> following code raises an exception.
>> Can anyone clarify me on this.
>>
>>
>> * public** void* addRecord(InputStream inputStream)
>> {
>> * try*
>> {
>> System./out/.println("Record store size is ::
>> "+store.getNumRecords());
>> * int* i = 0;
>> * while*(inputStream.read() != -1)
>> i++;
>> * byte*[] b =* new** byte*[i];
>> System./out/.println("Allocated "+i+" bytes");
>> * int* offset =0;
>> * int* numRead = 0;
>> * while*(offset < b.length &&
>> (numRead=inputStream.read(b, offset, b.length-offset)) >= 0)
>> {
>> offset += numRead;
>> }
>> System./out/.println("Read the offset");
>> inputStream.close();
>>
>> ByteArrayOutputStream bout =* new*
>> ByteArrayOutputStream();
>> DataOutputStream dout =* new* DataOutputStream(bout);
>> System./out/.println("Writing to dout");
>> dout.write(b);
>> System./out/.println("Adding record to record store");
>> store.addRecord(bout.toByteArray(),
>> 0,bout.toByteArray().length); // Add record
>> System./out/.println("Record store size is ::
>> "+store.getNumRecords());
>>
>> store.closeRecordStore();
>> }
>> * catch* (RecordStoreFullException e)
>> {
>> System./out/.println("Message 1 is ::
>> "+e.getMessage());
>> e.printStackTrace();
>> }
>> * catch* (RecordStoreNotFoundException e)
>> {
>> System./out/.println("Message 2 is ::
>> "+e.getMessage());
>> e.printStackTrace();
>> }
>> * catch* (RecordStoreException e)
>> {
>> System./out/.println("Message 3 is ::
>> "+e.getMessage());
>> e.printStackTrace();
>> }
>> * catch*(Exception ex)
>> {
>> System./out/.println("Message 4 is ::
>> "+ex.getMessage());
>> ex.printStackTrace();
>> }
>> }
>>
>> *With Best Regards,*
>> *Raghavendra. K. Naik*
>> *Analyst - ISI Practice*
>> *PA Consulting Group*
>> ? Nitesh Broadway, M.G.Road, Bangalore
>> ( +91 25318855 - ext. 28381
>> ** * Raghavendra.Naik@paconsulting.com
>> 8 _http://www.paconsulting.com_
>>
>>
>>
> ========================================================================
> ===
>
>> To unsubscribe, send email to listserv@java.sun.com and include in the
>> body of the message "signoff KVM-INTEREST". For general help, send
>> email to listserv@java.sun.com and include in the body of the message
>> "help".
>>
>
> ========================================================================
> ===
> To unsubscribe, send email to listserv@java.sun.com and include in the
> body
> of the message "signoff KVM-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff KVM-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".
>

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff KVM-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Thomas Landspurg

What is the exception?

If you try to save video, it's probably linked to the size of this video,
no?

In your specific case, just do a
* catch* (Exception e)
{
e.printStackTrace();
}

to catch all exception, if you just wants to debug these exception

On 11/30/06, Raghavendra K. Naik wrote:
>
> Hello ,
> I am trying to save the video file into RecordStore . But the following
> code raises an exception.
> Can anyone clarify me on this.
>
> * public** void* addRecord(InputStream inputStream)
> {
> * try*
> {
> System.*out*.println("Record store size is :: "+
> store.getNumRecords());
> * int* i = 0;
> * while*(inputStream.read() != -1)
> i++;
> * byte*[] b =* new** byte*[i];
> System.*out*.println("Allocated "+i+" bytes");
> * int* offset =0;
> * int* numRead = 0;
> * while*(offset < b.length && (numRead=
> inputStream.read(b, offset, b.length-offset)) >= 0)
> {
> offset += numRead;
> }
> System.*out*.println("Read the offset");
> inputStream.close();
>
> ByteArrayOutputStream bout =* new*ByteArrayOutputStream();
> DataOutputStream dout =* new* DataOutputStream(bout);
> System.*out*.println("Writing to dout");
> dout.write(b);
> System.*out*.println("Adding record to record store");
> store.addRecord(bout.toByteArray(), 0,bout.toByteArray().length
> ); // Add record
> System.*out*.println("Record store size is :: "+store
> .getNumRecords());
>
> store.closeRecordStore();
> }
> * catch* (RecordStoreFullException e)
> {
> System.*out*.println("Message 1 is :: "
> +e.getMessage());
> e.printStackTrace();
> }
> * catch* (RecordStoreNotFoundException e)
> {
> System.*out*.println("Message 2 is :: "
> +e.getMessage());
> e.printStackTrace();
> }
> * catch* (RecordStoreException e)
> {
> System.*out*.println("Message 3 is :: "
> +e.getMessage());
> e.printStackTrace();
> }
> * catch*(Exception ex)
> {
> System.*out*.println("Message 4 is :: "
> +ex.getMessage());
> ex.printStackTrace();
> }
> }
>
> *With Best Regards,*
> *Raghavendra. K. Naik*
> *****Analyst - ISI Practice*
> *PA Consulting Group*
> ? Nitesh Broadway, M.G.Road, Bangalore
> ( +91 25318855 - ext. 28381
> **** * Raghavendra.Naik@paconsulting.com
> 8 ***http://www.paconsulting.com*
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the
> body of the message "signoff KVM-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

--
Thomas Landspurg
http://blog.landspurg.net

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff KVM-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]

Stefan Haustein

Hi Raghavendra,

you are reading the stream to the end just to get the size.

Use mark/reset or better copy the stream to a ByteArrayOutputStream and
use toByteArray().

Best regards,
Stefan

Raghavendra K. Naik wrote:

> Hello ,
> I am trying to save the video file into RecordStore . But the
> following code raises an exception.
> Can anyone clarify me on this.
>
>
> * public** void* addRecord(InputStream inputStream)
> {
> * try*
> {
> System./out/.println("Record store size is ::
> "+store.getNumRecords());
> * int* i = 0;
> * while*(inputStream.read() != -1)
> i++;
> * byte*[] b =* new** byte*[i];
> System./out/.println("Allocated "+i+" bytes");
> * int* offset =0;
> * int* numRead = 0;
> * while*(offset < b.length &&
> (numRead=inputStream.read(b, offset, b.length-offset)) >= 0)
> {
> offset += numRead;
> }
> System./out/.println("Read the offset");
> inputStream.close();
>
> ByteArrayOutputStream bout =* new*
> ByteArrayOutputStream();
> DataOutputStream dout =* new* DataOutputStream(bout);
> System./out/.println("Writing to dout");
> dout.write(b);
> System./out/.println("Adding record to record store");
> store.addRecord(bout.toByteArray(),
> 0,bout.toByteArray().length); // Add record
> System./out/.println("Record store size is ::
> "+store.getNumRecords());
>
> store.closeRecordStore();
> }
> * catch* (RecordStoreFullException e)
> {
> System./out/.println("Message 1 is ::
> "+e.getMessage());
> e.printStackTrace();
> }
> * catch* (RecordStoreNotFoundException e)
> {
> System./out/.println("Message 2 is ::
> "+e.getMessage());
> e.printStackTrace();
> }
> * catch* (RecordStoreException e)
> {
> System./out/.println("Message 3 is ::
> "+e.getMessage());
> e.printStackTrace();
> }
> * catch*(Exception ex)
> {
> System./out/.println("Message 4 is ::
> "+ex.getMessage());
> ex.printStackTrace();
> }
> }
>
> *With Best Regards,*
> *Raghavendra. K. Naik*
> *Analyst - ISI Practice*
> *PA Consulting Group*
> ? Nitesh Broadway, M.G.Road, Bangalore
> ( +91 25318855 - ext. 28381
> ** * Raghavendra.Naik@paconsulting.com
> 8 _http://www.paconsulting.com_
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the
> body of the message "signoff KVM-INTEREST". For general help, send
> email to listserv@java.sun.com and include in the body of the message
> "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff KVM-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Eric Giguere

Also, don't call toByteArray() twice, you'll create two identical arrays
and waste a lot of memory that way.

Two years ago I wrote a detailed series about the RMS, you should take
the time to read the entire series before proceeding. Since it's hard to
find them on the Sun site, here are the direct links:

http://developers.sun.com/techtopics/mobility/midp/articles/databaserms/

http://developers.sun.com/techtopics/mobility/midp/articles/databasemap/

http://developers.sun.com/techtopics/mobility/midp/articles/databasemap/

http://developers.sun.com/techtopics/mobility/midp/articles/databasefilter/

http://developers.sun.com/techtopics/mobility/midp/articles/databasereco...

They're still very relevant and discuss many of the issues you would
encounter using the RMS.

Eric Giguere
http://j2me.synclastic.com
http://blackberry.synclastic.com
http://www.ericgiguere.com/j2me

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff KVM-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".