• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: Random Access File, byte buffer

 
Terry Martinson
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write to my Random Access File with the update functionality and I think I may be going about it wrong. I have the information to be updated in a String field. I am attempting to convert it to a byte[] so that I can use the raf.write(byte[]) method. I am setting the position in the raf with seek.
My first try was way off because when I converted the String to byte[] I did not pad the array - I just did the following:
raf.write(dataString.getBytes("US-ASCII"));
Now I am trying to pad the byte array as follows:
byte[] fullyPaddedByteArray = new byte[aLengthIKnow];
byte[] stringByteArray = dataString.getBytes("US-ASCII");
for (int j=0; j < stringByteArray.length; j++) {
fullyPaddedByteArray[j] = stringByteArray[j];
}
raf.write(fullyPaddedByteArray);
Am I on the right track?
Any suggestions on a better way to do this?
Any help appreciated.
TJ
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Terry,
Now I am trying to pad the byte array as follows:
byte[] fullyPaddedByteArray = new byte[aLengthIKnow];
byte[] stringByteArray = dataString.getBytes("US-ASCII");
for (int j=0; j < stringByteArray.length; j++) {
fullyPaddedByteArray[j] = stringByteArray[j];
}
raf.write(fullyPaddedByteArray);
Am I on the right track?
Any suggestions on a better way to do this?
Any help appreciated.

I think it's OK. Now, instead of the copy you do in a loop, you could use System.arraycopy(). It's probably a little faster.
Best,
Phil.
 
Terry Martinson
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tip on the arraycopy method.
I'll get back to my debugging
TJ
 
Bill Robertson
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do your specs specify US-ASCII?
 
Terry Martinson
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. It says "The character encoding is 8 bit US ASCII"
I have URLyBird 1.2.1.
TJ
 
Terry Martinson
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After I do an update to the database file, I read it again and print it out. Before my update, when I print results of name field, it looks fine (i.e. no weird characters at the end). However, after my update, I get weird characters at the end. These weird characters do not show up in Notepad, but do show up when I use the Programmers File Editor tool. Updates for all my fields work fine with the exception of the weird characters at the end. (i.e. I am not just off by a few bytes for each field.)
When I read the initial file and print out an unused ending byte from one of the string fields, it prints out as 0. Also, when I print out the 1st element of my "just initialized" fully padded byte array below, that also shows up as 0. So I thought I would be in synch. Note that my specs say data is "null terminated if less than the maximum length for the field".
Here is my pseudo code for the update:

1. Do I need to explicitly pad the fullyPaddedByteArray above with something
for null? If so, what do I stick in the byte array?
2. Do I just trust the Notepad display and ignore the weird characters coming up in my PFE editor? (scares me, because something is different if it shows up differently before/after)
Any help appreciated. Thanks.
TJ
 
Terry Martinson
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See answer in this thread
TJ
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic