How important is this issue? Well, it's probably not
that important - the way the assignment is set up to use US-ASCII, you can get away with converting chars to bytes simply by casting. That may well be fine as far as the exam's graders are concerned; dunno. However it's something that sets off alarm bells for me from personal experience; I find when people
say their data is "ASCII" it means it might be Cp-1252, or ISO-8859-1, or UTF-8, or something else (possibly all of these, if they're collecting data from multiple clueless sources) and at some point in the future someone's going to wonder why some of the characters come out funny-looking. At which point I get to discuss with the client what encoding is
really being used in the data. :roll: So I tend to assume that it's important to minimize assumptions about character encoding, and to make it as easy as possible to switch encodings later be changing just one line of code. So yes, I'm using the NIO charset stuff. I just save a private Charset instance, and whenever I need to convert a
String to bytes or vice versa, I use the decode() or encode() method. Seems to work just fine. Note that the constant-length format of the data means that it would be a bit more difficult to change the encoding to something like UTF-8 - but any single-byte encoding would be easy to substitute in place of US-ASCII.
In the contractors assignment, I return the trimmed version of the fields of my records from the readRecord method. That is, any trailing whitespace on each field is removed. Yeah, I'm taking a slightly different approach on this issue. The file format specs say fields are null terminated if shorter than the maximum length - even though the data file always terminates with spaces rather than nulls. AndI see no mention of trimming fields anywhere. So I'm writing my Data class to follow the API exactly, which (to me) means there's a difference between updating a field with value "foo" and updating it with "foo ". I just use a simple for loop to find the first null, as trim() would trim other whitepace as well.
Now common sense tells me that there's a good chance this isn't really what real-world users would
want - but it is what they explicitly asked for. So I'm (proably) going to offer a few configuration options, allowing the user to choose between "save fields exactly", "trim fields on save", "append spaces on save". The first is what they get by default; the last is what they probably really want. Yeah, this is probably more effort than is necessary - but when I see Sun's absolutist policy on things we "must" do, I respond by taking their instructions
very literally. In the real world I'd just call up the client and say hey, you probably
really wanted those fields to be padded with spaces, not nulls, right? But lacking that option here, I refuse to get burned by crappy customer specs.
[ May 22, 2003: Message edited by: Jim Yingst ]