1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.doxia.module.markdown;
20
21 import java.io.IOException;
22 import java.io.Writer;
23
24 import org.apache.commons.lang3.StringUtils;
25
26
27
28
29
30
31 public class LastTwoLinesBufferingWriter extends Writer {
32
33 private final Writer out;
34 private String previousLine;
35 private StringBuilder currentLine;
36 private final String lineSeparator;
37
38 public LastTwoLinesBufferingWriter(Writer out) {
39
40 this(out, System.getProperty("line.separator"));
41 }
42
43 LastTwoLinesBufferingWriter(Writer out, String lineSeparator) {
44 super();
45 this.out = out;
46 this.previousLine = "";
47 this.currentLine = new StringBuilder();
48 this.lineSeparator = lineSeparator;
49 }
50
51 public boolean isWriterAtStartOfNewLine() {
52 return currentLine.length() == 0;
53 }
54
55 public boolean isWriterAfterBlankLine() {
56 return StringUtils.isAllBlank(currentLine) && StringUtils.isAllBlank(previousLine);
57 }
58
59 @Override
60 public void write(char[] cbuf, int off, int len) throws IOException {
61 int offsetWrittenInLineBuffer = off;
62 int index = 0;
63 while (index < len) {
64
65 if (cbuf[off + index] == '\r' || cbuf[off + index] == '\n') {
66 int lenToWrite = index + 1 - (offsetWrittenInLineBuffer - off);
67 flushLine(cbuf, offsetWrittenInLineBuffer, lenToWrite);
68 offsetWrittenInLineBuffer += lenToWrite;
69 }
70 index++;
71 }
72 flushLine(cbuf, offsetWrittenInLineBuffer, index - (offsetWrittenInLineBuffer - off));
73 out.write(cbuf, off, len);
74 }
75
76 private void flushLine(char[] cbuf, int off, int len) {
77 this.currentLine.append(cbuf, off, len);
78
79 if (currentLine.toString().endsWith(lineSeparator)) {
80 previousLine = currentLine.toString();
81 currentLine.setLength(0);
82 }
83 }
84
85 @Override
86 public void flush() throws IOException {
87 out.flush();
88 }
89
90 @Override
91 public void close() throws IOException {
92 out.close();
93 }
94 }