File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

B&S, hardcoding file headers

 
hatim osman
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There...
my Data.java class looks something like this:

public class Data implements DB{
int RECORD_LENGTH;
long DATA_START_POINTER;
.
.
.
}

Now I was able to read the file and the length of each variable, should I hardcode the physical position of such variables specially the data_start_ptr, or have my class constructor or a helper method ex.
init(), take care of assigning values to those variables everytime the file is opened?
My idea is, in case someone change the format of the file, say, the length of a field then any data read or written will corrupt the file.
Any comment please.

Thanx in Advance
Hatim
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11833
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hatim,
My idea is, in case someone change the format of the file, say, the length of a field then any data read or written will corrupt the file.
This is a reasonable position to take. You could even take it a step further: since you know the size of all the fields and the header, it would be possible to calculate whether the size of the entire file (less the header) is a multiple of a record length - if it is not then the file is probably invalid.

However you might also like to think about a couple of things:
  • The supplied file comes with a "magic cookie" that presumably identifies the format of the file. As long as that magic cookie is correct, you can determine everything else (field lengths, therefore header length, therefore allowable file sizes).
  • There is nothing in the Data class methods that is specific to a contractors table. Change the underlying file, and you could access any type of data with the same class (as long as the file conforms to the format specified by the magic cookie). But if you hard code field lengths specific to the contractors table, then you not only stop yourself from being able to handle other types of data, you cannot even handle a change to the contractor's format without a recompile.

  • Regards, Andrew
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic