@Test
public void testSplitBytesByNewLines()
{

  final Logger log = Logger.getLogger("TestTrailFilePositionSetter.testSplitBytesByNewLines");
  log.info("starting");
  String s = "abc\r\ndef\r\n";
  List<String> l = new ArrayList<String>();
  List<Integer> endPos = new ArrayList<Integer>();
  String lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1, null, l, endPos);
  Assert.assertNull(lastLineStr,"Returned String should be null");
  Assert.assertEquals(2,l.size(),"Length");
  Assert.assertEquals(endPos.size(),2,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"def","Second in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),5,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),10,"Second in " + endPos);
  s = "abc\r\ndef\r\n";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), true, 0, null, l, endPos);
  Assert.assertNull(lastLineStr,"Returned String should be null");
  Assert.assertEquals(3,l.size(),"Length");
  Assert.assertEquals(endPos.size(),3,"Length");
  Assert.assertEquals(l.get(0),"","First in " + l);
  Assert.assertEquals(l.get(1),"abc","Second in " + l);
  Assert.assertEquals(l.get(2),"def","Third in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),-1,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),5,"First in " + endPos);
  Assert.assertEquals(endPos.get(2).intValue(),10,"Second in " + endPos);
  s = "abc\r\ndef\r\n";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), true, 1, null, l, endPos);
  Assert.assertNull(lastLineStr,"Returned String should be null");
  Assert.assertEquals(3,l.size(),"Length");
  Assert.assertEquals(endPos.size(),3,"Length");
  Assert.assertEquals(l.get(0),"a","First in " + l);
  Assert.assertEquals(l.get(1),"bc","Second in " + l);
  Assert.assertEquals(l.get(2),"def","Third in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),-1,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),5,"First in " + endPos);
  Assert.assertEquals(endPos.get(2).intValue(),10,"Second in " + endPos);
  s = "abc\r\ndef\r\n";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), true, 2, null, l, endPos);
  Assert.assertNull(lastLineStr,"Returned String should be null");
  Assert.assertEquals(3,l.size(),"Length");
  Assert.assertEquals(endPos.size(),3,"Length");
  Assert.assertEquals(l.get(0),"ab","First in " + l);
  Assert.assertEquals(l.get(1),"c","Second in " + l);
  Assert.assertEquals(l.get(2),"def","Third in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),-1,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),5,"First in " + endPos);
  Assert.assertEquals(endPos.get(2).intValue(),10,"Second in " + endPos);
  s = "abc\r\ndef\r\n";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), true, 3, null, l, endPos);
  Assert.assertNull(lastLineStr,"Returned String should be null");
  Assert.assertEquals(2,l.size(),"Length");
  Assert.assertEquals(endPos.size(),2,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"def","Second in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),5,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),10,"Second in " + endPos);
  s = "abc\r\ndef\r\n";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), true, 5, null, l, endPos);
  Assert.assertNull(lastLineStr,"Returned String should be null");
  Assert.assertEquals(l.size(),3,"Length");
  Assert.assertEquals(endPos.size(),3,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"","Second in " + l);
  Assert.assertEquals(l.get(2),"def","Third in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),5,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),-1,"First in " + endPos);
  Assert.assertEquals(endPos.get(2).intValue(),10,"Second in " + endPos);
  s = "abc\r\ndef\r\n";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), true, 6, null, l, endPos);
  Assert.assertNull(lastLineStr,"Returned String should be null");
  Assert.assertEquals(l.size(),3,"Length");
  Assert.assertEquals(endPos.size(),3,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"d","Second in " + l);
  Assert.assertEquals(l.get(2),"ef","Third in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),5,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),-1,"First in " + endPos);
  Assert.assertEquals(endPos.get(2).intValue(),10,"Second in " + endPos);
  s = "abc\r\ndef\r\n";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  //log.info("Starting !!");
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), true, 7, null, l, endPos);
  Assert.assertNull(lastLineStr,"Returned String should be null");
  Assert.assertEquals(l.size(),3,"Length");
  Assert.assertEquals(endPos.size(),3,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"de","Second in " + l);
  Assert.assertEquals(l.get(2),"f","Third in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),5,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),-1,"Second in " + endPos);
  Assert.assertEquals(endPos.get(2).intValue(),10,"Third in " + endPos);
  s = "abc\r\ndef\r\n";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), true, 4, null, l, endPos);
  Assert.assertNull(lastLineStr,"Returned String should be null");
  Assert.assertEquals(2,l.size(),"Length");
  Assert.assertEquals(endPos.size(),2,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"def","Second in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),5,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),10,"Second in " + endPos);
  s = "abc\r\ndef\r\n";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), true, 8, null, l, endPos);
  Assert.assertNull(lastLineStr,"Returned String should be null");
  Assert.assertEquals(2,l.size(),"Length");
  Assert.assertEquals(endPos.size(),2,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"def","Second in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),5,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),10,"Second in " + endPos);
  s = "abc\ndef\n";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), true, 3, null, l, endPos);
  Assert.assertNull(lastLineStr,"Returned String should be null");
  Assert.assertEquals(2,l.size(),"Length");
  Assert.assertEquals(endPos.size(),2,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"def","Second in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),4,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),8,"Second in " + endPos);
  s = "abc\ndef\n";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1, null, l,endPos);
  Assert.assertNull(lastLineStr,"Returned String should be null");
  Assert.assertEquals(2,l.size(),"Length");
  Assert.assertEquals(endPos.size(),2,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"def","Second in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),4,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),8,"Second in " + endPos);
  s = "abc\r\ndef\n";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1, null, l,endPos);
  Assert.assertNull(lastLineStr,"Returned String should be null");
  Assert.assertEquals(2,l.size(),"Length");
  Assert.assertEquals(endPos.size(),2,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"def","Second in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),5,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),9,"Second in " + endPos);
  s = "abc\r\ndef";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1, null, l, endPos);
  Assert.assertEquals( lastLineStr,"def","Returned String");
  Assert.assertEquals(l.size(),1,"Length");
  Assert.assertEquals(endPos.size(),1,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),5,"First in " + endPos);
  s = "abc\r\ndef\r";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1, null, l,endPos);
  //log.info("Length: " + lastLineStr.length());
  Assert.assertEquals( lastLineStr,"def\r","Returned String");
  Assert.assertEquals(l.size(),1,"Length");
  Assert.assertEquals(endPos.size(),1,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),5,"First in " + endPos);
  lastLineStr = "abc";
  s = "def\r\nghi";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1, lastLineStr, l, endPos);
  Assert.assertEquals( lastLineStr,"ghi","Returned String");
  Assert.assertEquals(l.size(),1,"Length");
  Assert.assertEquals(endPos.size(),1,"Length");
  Assert.assertEquals(l.get(0),"abcdef","First in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),8,"First in " + endPos);
  lastLineStr = "abc\r";
  s = "def\r\nghi";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1, lastLineStr, l,endPos);
  Assert.assertEquals( lastLineStr,"ghi","Returned String");
  Assert.assertEquals(l.size(),2,"Length: " + l);
  Assert.assertEquals(endPos.size(),2,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"def","First in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),4,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),9,"Second in " + endPos);
  lastLineStr = "abc\r";
  s = "\ndef\r\nghi";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1,lastLineStr, l,endPos);
  Assert.assertEquals( lastLineStr,"ghi","Returned String");
  Assert.assertEquals(l.size(),2,"Length: " + l);
  Assert.assertEquals(endPos.size(),2,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"def","First in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),5,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),10,"Second in " + endPos);
  lastLineStr = "abc";
  s = "\ndef\r\nghi";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1, lastLineStr, l, endPos);
  Assert.assertEquals( lastLineStr,"ghi","Returned String");
  Assert.assertEquals(l.size(),2,"Length");
  Assert.assertEquals(endPos.size(),2,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"def","First in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),4,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),9,"Second in " + endPos);
  lastLineStr = "abc";
  s = "\rdef\r\nghi";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1, lastLineStr, l,endPos);
  Assert.assertEquals( lastLineStr,"ghi","Returned String");
  Assert.assertEquals(l.size(),2,"Length");
  Assert.assertEquals(endPos.size(),2,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"def","Second in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),4,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),9,"Second in " + endPos);
  lastLineStr = "xyz";
  s = "abcdef";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1, lastLineStr, l,endPos);
  Assert.assertEquals( lastLineStr,"xyzabcdef","Returned String");
  Assert.assertEquals(l.size(),0,"Length");
  Assert.assertEquals(endPos.size(),0,"Length");
  s = "abcdef";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1, null, l,endPos);
  Assert.assertEquals( lastLineStr,"abcdef","Returned String");
  Assert.assertEquals(l.size(),0,"Length");
  Assert.assertEquals(endPos.size(),0,"Length");
  s = "abc\n\n  def";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1, null, l,endPos);
  Assert.assertEquals( lastLineStr,"  def","Returned String");
  Assert.assertEquals(l.size(),2,"Length");
  Assert.assertEquals(endPos.size(),2,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"","Second in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),4,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),5,"Second in " + endPos);
  s = "abc\n\n  def\n   ghi";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), true, 4, null, l,endPos);
  Assert.assertEquals( lastLineStr,"   ghi","Returned String");
  Assert.assertEquals(l.size(),3,"Length");
  Assert.assertEquals(endPos.size(),3,"Length");
  Assert.assertEquals(l.get(0),"abc","First in " + l);
  Assert.assertEquals(l.get(1),"","Second in " + l);
  Assert.assertEquals(l.get(2),"  def","Third in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),4,"First in " + endPos);
  Assert.assertEquals(endPos.get(1).intValue(),5,"Second in " + endPos);
  Assert.assertEquals(endPos.get(2).intValue(),11,"Third in " + endPos);
  s = "\n";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1, null, l,endPos);
  Assert.assertEquals( lastLineStr,null,"Returned String");
  Assert.assertEquals(l.size(),1,"Length");
  Assert.assertEquals(endPos.size(),1,"Length");
  Assert.assertEquals(l.get(0),"","First in " + l);
  Assert.assertEquals(endPos.get(0).intValue(),1,"First in " + endPos);
  s = " ";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1, null, l,endPos);
  Assert.assertEquals( lastLineStr," ","Returned String");
  Assert.assertEquals(l.size(),0,"Length");
  Assert.assertEquals(endPos.size(),0,"Length");
  s = "";
  l = new ArrayList<String>();
  endPos = new ArrayList<Integer>();
  lastLineStr = TrailFilePositionSetter.splitBytesByNewLines(s.getBytes(Charset.defaultCharset()), s.length(), false, -1, null, l,endPos);
  Assert.assertEquals( lastLineStr,null,"Returned String");
  Assert.assertEquals(l.size(),0,"Length");
  Assert.assertEquals(endPos.size(),0,"Length");
  log.info(DONE_STRING);
}