• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

NX: [Help] read & write flag on database

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I encounter a problem in accessing db. In the requirement, there is a flag for each record. The flag indicate if it's valid record (00) and deleted record (0x8000). They seems hex number. how can we read and write it? I am using RandomAccessFile. Anyone has idea?
Thanks a lot.
George
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi George,
You might want to take a look at this thread - it discusses several different aspects of the low level access to the data file, including what API can be used to read that field.
Regards, Andrew
 
George Fung
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Andrew. But I found some problems.
For null padded, is it equal to "0x00"?
I also found that it's space " " in data sample file. Is my data file corrupted or we should put ' ' as padded characters?
I have tried readUnsignedByte. I can read it. I also tried readFully. It seems work,too for flag and data content. But how to write 0x8000 into db file. The previous thread didnt' mention it.
For writing, I use RandomAccessFile.writeBytes. It It works fine.. Except flag, all data is consistent with sample data. So, anyone has idea to write '0x8000'?
George
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi George,
Sorry, still not answering your question about 0x8000: leaving it for someone else to jump in
Some thoughts on null termination and space filled fields can be found in this thread.
Regards, Andrew
 
George Fung
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell me if anyone know how to write '0x8000'.
For null padded, I will use space " " as I found that it's space padded in data file SUN gave. Or pad null value, you may try 'RandomAccessFile.writeByte(0)'. I think it's more suitable than new String("");
George

Originally posted by Andrew Monkhouse:
Hi George,
Sorry, still not answering your question about 0x8000: leaving it for someone else to jump in
Some thoughts on null termination and space filled fields can be found in this thread.
Regards, Andrew

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I just received my test and am having some problems with the flag as well. Since it is a 2 byte flag, is it possible to use the RandomAccessFile's getShort() method to read the value? If it's 0, the value is valid, otherwise, the record is invalid.
To set the record to invalid, we can set the decimal value for the equivalent of the 2 byte hex value (0X8000). I am not sure if this
is the right way to handle this flag.
Cheers,
Nick
 
George Fung
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nick,
I have tried it. You can't use getShort as it's byte value (not short type). From java spec (getShort):
If the two bytes read, in order, are b1 and b2, where each of the two values is between 0 and 255, inclusive, then the result is equal to:
(short)((b1 << 8) | b2)
This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.
So, you must use getByte. For invalid condition, can you try it and describe mroe details here? Thanks a lot.
George

Originally posted by Nick Ho:
Hi,
I just received my test and am having some problems with the flag as well. Since it is a 2 byte flag, is it possible to use the RandomAccessFile's getShort() method to read the value? If it's 0, the value is valid, otherwise, the record is invalid.
To set the record to invalid, we can set the decimal value for the equivalent of the 2 byte hex value (0X8000). I am not sure if this
is the right way to handle this flag.
Cheers,
Nick

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried it. You can't use getShort as it's byte value (not short type)
George, how many bytes do you think need to be read here? You said previously
The flag indicate if it's valid record (00) and deleted record (0x8000).
Now 00 looks like it's just one byte, but it could be any number of bytes I suppose. However 0x8000 - how many bytes is that? I really think that getShort() (or getUnsignedShort() perhaps) are exactly what you want here, and I can't understand what your objection is.
 
George Fung
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2 byte for 00
2 bytes for 0x8000,too.. 0x means hexadecimal. it's 4 bit/digit. I still dunno how to write 0x8000. anyone know?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried
raf.writeUnsignedShort(0x8000);
to write, and
int value = raf.readUnsignedShort();
to read? It's fairly straightforward, I think.
 
George Fung
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is no writeUnsignedShort. But we may use writeShort(0x8000) or (writeByte(0x80) & writeByte(0x00)
reply
    Bookmark Topic Watch Topic
  • New Topic