Forums Register Login

NX: Random Access File, byte buffer

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks for the tip on the arraycopy method.
I'll get back to my debugging
TJ
+Pie Number of slices to send: Send
Do your specs specify US-ASCII?
+Pie Number of slices to send: Send
Yes. It says "The character encoding is 8 bit US ASCII"
I have URLyBird 1.2.1.
TJ
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
See answer in this thread
TJ
They weren't very bright, but they were very, very big. Ad contrast:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1324 times.
Similar Threads
EOF Exception while reading Data Section
When to close the RandomAccessFile?!!
NX: Reading from the db.db file
NX:writing to the db.db file
NX: Writng String[] to datafile
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:16:03.