@Test public void shouldReadPartToEndCharacterLeavingSufixUnchangedInStream() throws IOException { String text = "{ this is important } leave me alone please"; StringReader reader = new StringReader(text); String textPart = StreamPartReader.readPart(reader, '{', '}'); assertThat(textPart).isEqualTo("{ this is important }"); assertThat(reader).hasTextLeft(" leave me alone please"); }
public static String readPart(Reader reader, char openingChar, char closingChar) throws IOException { StringBuilder part = new StringBuilder(PART_BUFFER_SIZE); int characterCode = reader.read(); int pairCounter = 0; char character; boolean read = false; while (characterCode != -1) { character = (char) characterCode; if (character == openingChar) { read = true; pairCounter++; } if (read) { part.append(character); } if (character == closingChar) { pairCounter--; if (pairCounter == 0) { break; } } characterCode = reader.read(); } if (pairCounter != 0) { throw new IllegalStateException("Stream ended but no ending char found! Possibly because of dangling opening char."); } return part.toString(); }
StreamPartReader { public static String readPart(Reader reader, char openingChar, char closingChar) throws IOException { StringBuilder part = new StringBuilder(PART_BUFFER_SIZE); int characterCode = reader.read(); int pairCounter = 0; char character; boolean read = false; while (characterCode != -1) { character = (char) characterCode; if (character == openingChar) { read = true; pairCounter++; } if (read) { part.append(character); } if (character == closingChar) { pairCounter--; if (pairCounter == 0) { break; } } characterCode = reader.read(); } if (pairCounter != 0) { throw new IllegalStateException("Stream ended but no ending char found! Possibly because of dangling opening char."); } return part.toString(); } }
StreamPartReader { public static String readPart(Reader reader, char openingChar, char closingChar) throws IOException { StringBuilder part = new StringBuilder(PART_BUFFER_SIZE); int characterCode = reader.read(); int pairCounter = 0; char character; boolean read = false; while (characterCode != -1) { character = (char) characterCode; if (character == openingChar) { read = true; pairCounter++; } if (read) { part.append(character); } if (character == closingChar) { pairCounter--; if (pairCounter == 0) { break; } } characterCode = reader.read(); } if (pairCounter != 0) { throw new IllegalStateException("Stream ended but no ending char found! Possibly because of dangling opening char."); } return part.toString(); } private  StreamPartReader(); }
StreamPartReader { public static String readPart(Reader reader, char openingChar, char closingChar) throws IOException { StringBuilder part = new StringBuilder(PART_BUFFER_SIZE); int characterCode = reader.read(); int pairCounter = 0; char character; boolean read = false; while (characterCode != -1) { character = (char) characterCode; if (character == openingChar) { read = true; pairCounter++; } if (read) { part.append(character); } if (character == closingChar) { pairCounter--; if (pairCounter == 0) { break; } } characterCode = reader.read(); } if (pairCounter != 0) { throw new IllegalStateException("Stream ended but no ending char found! Possibly because of dangling opening char."); } return part.toString(); } private  StreamPartReader(); static String readPart(Reader reader, char openingChar, char closingChar); }
StreamPartReader { public static String readPart(Reader reader, char openingChar, char closingChar) throws IOException { StringBuilder part = new StringBuilder(PART_BUFFER_SIZE); int characterCode = reader.read(); int pairCounter = 0; char character; boolean read = false; while (characterCode != -1) { character = (char) characterCode; if (character == openingChar) { read = true; pairCounter++; } if (read) { part.append(character); } if (character == closingChar) { pairCounter--; if (pairCounter == 0) { break; } } characterCode = reader.read(); } if (pairCounter != 0) { throw new IllegalStateException("Stream ended but no ending char found! Possibly because of dangling opening char."); } return part.toString(); } private  StreamPartReader(); static String readPart(Reader reader, char openingChar, char closingChar);  }