am implementing a TFTP (RFC 1350) client and server in java.
Three modes of transfer are currently supported: netascii (This is
ascii as defined in “USA Standard Code for Information Interchange”
[1] with the modifications specified in “Telnet Protocol
Specification” [3].) Note that it is 8 bit ascii. The term
“netascii” will be used throughout this document to mean this
particular version of ascii.);
What does this mean in Java terms? and how can read/write to a file, or convert a String, in this mode?
netascii seems to be defined as an 8-bit extension of ASCII, but restricted to basic ASCII ranging from 0x20 to 0x7F, plus 8 control characters (see page 9): 0x00 (NUL), 0x10 (LF), 0x13 (CR), 0x07 (BEL), 0x08 (BS), 0x09 (HT), 0x11 (VT) and 0x12 (FF).
Besides making sure your output is pure ASCII only within this range, another requirement of netascii is that newlines are always CRLF, so you must convert accordingly. Any CR can only be followed either by an LF or a NUL.
You can use Apache Commons to handle this for you, but if you want to roll out your own implementation, just use the
US-ASCIIencoding in Java, then perform extra checks to make sure you don’t have any non allowed control characters from the 0x00–0x20 range and finally ensure that your newlines are CRLFs rather than just LFs.