@Test public void testSplitTwoDocuments() throws IOException { StringBuilder docsString = new StringBuilder(); for (int i = 0; i < 2; i++) { docsString.append("<DOC>\n"); docsString.append("test document #").append(i).append("\n"); docsString.append("</DOC>\n"); } try (ObjectStream<String> docs = new DocumentSplitterStream( ObjectStreamUtils.createObjectStream(docsString.toString()))) { String doc1 = docs.read(); Assert.assertEquals(docsString.length() / 2, doc1.length() + 1); Assert.assertTrue(doc1.contains("#0")); String doc2 = docs.read(); Assert.assertEquals(docsString.length() / 2, doc2.length() + 1); Assert.assertTrue(doc2.contains("#1")); Assert.assertNull(docs.read()); Assert.assertNull(docs.read()); } }
public String read() throws IOException { if (docs.isEmpty()) { String newDocs = samples.read(); if (newDocs != null) { int docStartOffset = 0; while (true) { int startDocElement = newDocs.indexOf(DOC_START_ELEMENT, docStartOffset); int endDocElement = newDocs.indexOf(DOC_END_ELEMENT, docStartOffset); if (startDocElement != -1 && endDocElement != -1) { if (startDocElement < endDocElement) { docs.add(newDocs.substring(startDocElement, endDocElement + DOC_END_ELEMENT.length())); docStartOffset = endDocElement + DOC_END_ELEMENT.length(); } else { throw new InvalidFormatException("<DOC> element is not closed!"); } } else if (startDocElement != endDocElement) { throw new InvalidFormatException("Missing <DOC> or </DOC> element!"); } else { break; } } } } if (docs.size() > 0) { return docs.remove(0); } else { return null; } }
DocumentSplitterStream extends FilterObjectStream<String, String> { public String read() throws IOException { if (docs.isEmpty()) { String newDocs = samples.read(); if (newDocs != null) { int docStartOffset = 0; while (true) { int startDocElement = newDocs.indexOf(DOC_START_ELEMENT, docStartOffset); int endDocElement = newDocs.indexOf(DOC_END_ELEMENT, docStartOffset); if (startDocElement != -1 && endDocElement != -1) { if (startDocElement < endDocElement) { docs.add(newDocs.substring(startDocElement, endDocElement + DOC_END_ELEMENT.length())); docStartOffset = endDocElement + DOC_END_ELEMENT.length(); } else { throw new InvalidFormatException("<DOC> element is not closed!"); } } else if (startDocElement != endDocElement) { throw new InvalidFormatException("Missing <DOC> or </DOC> element!"); } else { break; } } } } if (docs.size() > 0) { return docs.remove(0); } else { return null; } } }
DocumentSplitterStream extends FilterObjectStream<String, String> { public String read() throws IOException { if (docs.isEmpty()) { String newDocs = samples.read(); if (newDocs != null) { int docStartOffset = 0; while (true) { int startDocElement = newDocs.indexOf(DOC_START_ELEMENT, docStartOffset); int endDocElement = newDocs.indexOf(DOC_END_ELEMENT, docStartOffset); if (startDocElement != -1 && endDocElement != -1) { if (startDocElement < endDocElement) { docs.add(newDocs.substring(startDocElement, endDocElement + DOC_END_ELEMENT.length())); docStartOffset = endDocElement + DOC_END_ELEMENT.length(); } else { throw new InvalidFormatException("<DOC> element is not closed!"); } } else if (startDocElement != endDocElement) { throw new InvalidFormatException("Missing <DOC> or </DOC> element!"); } else { break; } } } } if (docs.size() > 0) { return docs.remove(0); } else { return null; } } DocumentSplitterStream(ObjectStream<String> samples); }
DocumentSplitterStream extends FilterObjectStream<String, String> { public String read() throws IOException { if (docs.isEmpty()) { String newDocs = samples.read(); if (newDocs != null) { int docStartOffset = 0; while (true) { int startDocElement = newDocs.indexOf(DOC_START_ELEMENT, docStartOffset); int endDocElement = newDocs.indexOf(DOC_END_ELEMENT, docStartOffset); if (startDocElement != -1 && endDocElement != -1) { if (startDocElement < endDocElement) { docs.add(newDocs.substring(startDocElement, endDocElement + DOC_END_ELEMENT.length())); docStartOffset = endDocElement + DOC_END_ELEMENT.length(); } else { throw new InvalidFormatException("<DOC> element is not closed!"); } } else if (startDocElement != endDocElement) { throw new InvalidFormatException("Missing <DOC> or </DOC> element!"); } else { break; } } } } if (docs.size() > 0) { return docs.remove(0); } else { return null; } } DocumentSplitterStream(ObjectStream<String> samples); String read(); }
DocumentSplitterStream extends FilterObjectStream<String, String> { public String read() throws IOException { if (docs.isEmpty()) { String newDocs = samples.read(); if (newDocs != null) { int docStartOffset = 0; while (true) { int startDocElement = newDocs.indexOf(DOC_START_ELEMENT, docStartOffset); int endDocElement = newDocs.indexOf(DOC_END_ELEMENT, docStartOffset); if (startDocElement != -1 && endDocElement != -1) { if (startDocElement < endDocElement) { docs.add(newDocs.substring(startDocElement, endDocElement + DOC_END_ELEMENT.length())); docStartOffset = endDocElement + DOC_END_ELEMENT.length(); } else { throw new InvalidFormatException("<DOC> element is not closed!"); } } else if (startDocElement != endDocElement) { throw new InvalidFormatException("Missing <DOC> or </DOC> element!"); } else { break; } } } } if (docs.size() > 0) { return docs.remove(0); } else { return null; } } DocumentSplitterStream(ObjectStream<String> samples); String read();  }