• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

db access returning String

 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My assignment (URLyBird) specifies to have String array to be returned for read. I am searching for a method to replace readByte:byte with something like readString(offset, length):String.

DataInputStream is what I looked so far.

I would like to know any of the java library addresses this and provide a method like readString.

I understand readUTF is supposed to function so. But an offset and length cannot be specified. I will love to use readUTF. I just need to know how to tame it.

Please let me know your thoughts. I don't feel reading bytes and concatenating it to String is a good idea if a library function already exists.
 
Stephen Anson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following works for me:
1. create a byte[] of the correct length (found from record metadata)
2. use readFully(byte[])
3. use the String constructor that takes a byte[] argument

hope that helps
Steve
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen,
Thanks for your reply.

It works for me too. I don't like the idea of creating array bytes each time I read database. Hence I am planning to keep the byte array static or move to a MetaInfo object.

I was checking to see whether anybody tried anything else.

Jiju
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one correction. Keeping a static reference to byte array will not work since it has to be shared with many users. Infact each thread has to have it's own byte array.

Hence Stephen's approach seems to be right. Pardon me for the thoughtless suggestion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic