My suggestion: Instrument your code so that the program can tell you what it is seeing and what it is doing:



System.out.println("Size after wrap-around is " + buffer.size()); // Where...