Useful bits of code

AlphaNumeric Sorting

TFTP Server

Natural Sorting in Java

A while ago, I had a need for more human friendly sorting within various software I maintain. Basically, what I wanted was a natural sort. One implementation that I found was the Alphanum algorithm, as discussed here: http://www.davekoelle.com/alphanum.html

But, it turned out his implementation didn't handle leading 0's properly, among other problems. I fixed the leading 0's problem, and a few other issues. He never responded when I tried to send him code updates, so apparently his page is dormant. I figured I might as well share my solution in case someone else needs it.

AlphanumComparator.java
AlphanumComparatorTest.java

Unsorted

192.168.26.4
192.168.26.3
192.168.26.203
192.168.26.202
192.168.26.201
192.168.26.2
192.168.26.102
192.168.26.101
192.168.26.1

Alphanum sorted

192.168.26.1
192.168.26.2
192.168.26.3
192.168.26.4
192.168.26.101
192.168.26.102
192.168.26.201
192.168.26.202
192.168.26.203

 

Java Default Sorted

z1.doc
z10.doc
z100.doc
z101.doc
z102.doc
z11.doc
z12.doc
z13.doc
z14.doc
z15.doc
z16.doc
z17.doc
z18.doc
z19.doc
z2.doc
z20.doc
z3.doc
z4.doc
z5.doc
z6.doc
z7.doc
z8.doc
z9.doc

Alphanum sorted

z1.doc
z2.doc
z3.doc
z4.doc
z5.doc
z6.doc
z7.doc
z8.doc
z9.doc
z10.doc
z11.doc
z12.doc
z13.doc
z14.doc
z15.doc
z16.doc
z17.doc
z18.doc
z19.doc
z20.doc
z100.doc
z101.doc
z102.doc

 
 

Multithreaded Multiclient Java TFTP Server

At another point in time, I needed a TFTP server I could embed into some java software. I couldn't find one anywhere... at least, not one that worked, and had a reasonable license.

So, I wrote my own. Below is a single-class implementation of a fully multithreaded TFTP server. This server will handle multiple clients at the same time. This implements RFC 1350 and wrapping block numbers for large file support (I don't know if this is in a RFC, or if it is just something that most clients do)

This implementation depends on Apache Commons Net and Log4J.

To launch, just create an instance of the class. An IOException will be thrown if the server fails to start for reasons such as port in use, port denied, etc.
To stop, use the shutdown method.
To check to see if the server is still running (or if it stopped because of an error), call the isRunning() method.

TFTPServer.java

I tried to give this code back to Apache Commons Net, but the powers that be there refuse to allow any server-like code to be part of the release. They only want to be client code. (Maybe you should change your name to Apache Commons Client Net then...)

Never mind the fact that their own TFTP client was horribly broken for years because they didn't have any automated tests, because they didn't have a server. In the end, the powers that be took the code, and buried it in their test path. I don't know if they have maintained it. I recommend you use my implementation. It is part of commercial software in use every day, so it is tested and maintained as necessary.

Note, I also have JUnit tests for this code - but I'll just point you at the apache test code for those.

Path Tests
Transfer Tests