Move writer to util.io package.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / util / io / DuplicateLineSuppressingWriter.java
1 /*
2  * XdccDownloader - DuplicateLineSuppressingWriter.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.xdcc.util.io;
19
20 import java.io.IOException;
21 import java.io.Writer;
22
23 /**
24  * {@link Writer} implementation that suppreses multiple identical lines written to it.
25  *
26  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27  */
28 public class DuplicateLineSuppressingWriter extends Writer {
29
30         private final Writer writer;
31         private String lastLine;
32         private final StringBuilder currentLine = new StringBuilder();
33
34         public DuplicateLineSuppressingWriter(Writer writer) {
35                 this.writer = writer;
36         }
37
38         @Override
39         public void write(char[] cbuf) throws IOException {
40                 write(cbuf, 0, cbuf.length);
41         }
42
43         @Override
44         public void write(String str) throws IOException {
45                 write(str, 0, str.length());
46         }
47
48         @Override
49         public Writer append(CharSequence csq) throws IOException {
50                 write(String.valueOf(csq));
51                 return this;
52         }
53
54         @Override
55         public Writer append(CharSequence csq, int start, int end) throws IOException {
56                 write(String.valueOf(csq).substring(start, end));
57                 return this;
58         }
59
60         @Override
61         public Writer append(char c) throws IOException {
62                 write(c);
63                 return this;
64         }
65
66         @Override
67         public void flush() throws IOException {
68                 writer.flush();
69         }
70
71         @Override
72         public void close() throws IOException {
73                 writer.close();
74         }
75
76         @Override
77         public void write(int c) throws IOException {
78                 currentLine.append((char) c);
79                 writeCollectedLines();
80         }
81
82         @Override
83         public void write(char[] cbuf, int off, int len) throws IOException {
84                 currentLine.append(cbuf, off, len);
85                 writeCollectedLines();
86         }
87
88         @Override
89         public void write(String str, int off, int len) throws IOException {
90                 currentLine.append(str, off, len);
91                 writeCollectedLines();
92         }
93
94         private void writeCollectedLines() throws IOException {
95                 while (currentLineContainsLineBreak()) {
96                         String nextLine = cutNextLine();
97                         maybeWriteNextLine(nextLine);
98                 }
99         }
100
101         private void maybeWriteNextLine(String nextLine) throws IOException {
102                 if (!nextLine.equals(lastLine)) {
103                         writer.write(nextLine);
104                         lastLine = nextLine;
105                 }
106         }
107
108         private String cutNextLine() {
109                 int indexOfLineBreak = currentLine.indexOf("\n");
110                 String nextLine = currentLine.substring(0, indexOfLineBreak + 1);
111                 currentLine.delete(0, indexOfLineBreak + 1);
112                 return nextLine;
113         }
114
115         private boolean currentLineContainsLineBreak() {
116                 return currentLine.indexOf("\n") > -1;
117         }
118
119 }