TrailFilePositionSetter { protected static String splitBytesByNewLines(byte[] buf, int numBytes, boolean spanFile, int endOffset, String prevLine, List<String> lines, List<Integer> lineEndPos) { String s = (prevLine != null) ? prevLine + new String(buf,0,numBytes) : new String(buf,0,numBytes); if ( s.isEmpty()) return null; if ( prevLine != null) endOffset += prevLine.length(); String[] strArr = s.split(NEW_LINE_PATTERN_STR); if (strArr.length == 0) { strArr = new String[1]; strArr[0] = ""; } int pos = 0; for (int i = 0 ; i < strArr.length - 1; i++) { String l = strArr[i]; if ( spanFile && (pos <= endOffset) && ((pos + l.length()) > endOffset)) { int pivot = endOffset - pos; lines.add(l.substring(0,pivot)); pos += pivot; l = l.substring(pivot); lineEndPos.add(-1); spanFile = false; } lines.add(l); pos += l.length(); int newLineLen = 0; if (s.startsWith(DOUBLE_CHAR_NEWLINE, pos)) newLineLen = 2; else newLineLen = 1; pos += newLineLen; lineEndPos.add(pos); } char c = s.charAt(s.length() -1); String lastLine = null; String l = strArr[strArr.length - 1]; if ( spanFile && (pos <= endOffset) && ((pos + l.length()) > endOffset)) { int pivot = endOffset - pos; lines.add(l.substring(0,pivot)); pos += pivot; l = l.substring(pivot); lineEndPos.add(-1); } if ((c == '\r')) { lastLine = l + '\r'; } else if (c != '\n') { lastLine = l; } else { lines.add(l); pos += l.length(); if (s.startsWith(DOUBLE_CHAR_NEWLINE, pos)) pos += 2; else pos += 1; lineEndPos.add(pos); } return lastLine; } TrailFilePositionSetter(String dir, String filePrefix);  TrailFilePositionSetter(String dir, String filePrefix, String phySourceName); synchronized FilePositionResult locateFilePosition(long scn, TransactionSCNFinderCallback callback); public Logger _log; static final long USE_LATEST_SCN; static final long USE_EARLIEST_SCN; static final String NEW_LINE_PATTERN_STR; static final String DOUBLE_CHAR_NEWLINE; static final int NO_NEWLINE_LEN; }
@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>(); 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); 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); }