Skip to content

Commit ea3d2b5

Browse files
Fix unit test logging
1 parent e4af70b commit ea3d2b5

File tree

1 file changed

+12
-70
lines changed

1 file changed

+12
-70
lines changed

src/test/java/com/github/markusbernhardt/xmldoclet/AbstractTestParent.java

Lines changed: 12 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@ public void log(Logger log, String message) {
161161

162162
private static class LoggingOutputStream extends java.io.OutputStream {
163163

164-
protected static final String LINE_SEPERATOR = System.getProperty("line.separator");
165-
166164
protected Logger log;
167165
protected LoggingLevelEnum loggingLevel;
168166

@@ -174,25 +172,7 @@ private static class LoggingOutputStream extends java.io.OutputStream {
174172
/**
175173
* The internal buffer where data is stored.
176174
*/
177-
protected byte[] buffer;
178-
179-
/**
180-
* The number of valid bytes in the buffer. This value is always in the
181-
* range <tt>0</tt> through <tt>buf.length</tt>; elements
182-
* <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid byte
183-
* data.
184-
*/
185-
protected int count;
186-
187-
/**
188-
* Remembers the size of the buffer for speed.
189-
*/
190-
private int bufferLength;
191-
192-
/**
193-
* The default number of bytes in the buffer. =2048
194-
*/
195-
public static final int DEFAULT_BUFFER_LENGTH = 2048;
175+
protected StringBuffer buffer = new StringBuffer();
196176

197177
/**
198178
* Creates the LoggingOutputStream to flush to the given Category.
@@ -213,9 +193,6 @@ public LoggingOutputStream(Logger log, LoggingLevelEnum loggingLevel) throws Ill
213193

214194
this.loggingLevel = loggingLevel;
215195
this.log = log;
216-
bufferLength = DEFAULT_BUFFER_LENGTH;
217-
buffer = new byte[DEFAULT_BUFFER_LENGTH];
218-
count = 0;
219196
}
220197

221198
/**
@@ -246,25 +223,14 @@ public void write(final int b) throws IOException {
246223
throw new IOException("The stream has been closed.");
247224
}
248225

249-
// don't log nulls
250-
if (b == 0) {
251-
return;
252-
}
253-
254-
// would this be writing past the buffer?
255-
if (count == bufferLength) {
256-
// grow the buffer
257-
final int newBufLength = bufferLength + DEFAULT_BUFFER_LENGTH;
258-
final byte[] newBuf = new byte[newBufLength];
259-
260-
System.arraycopy(buffer, 0, newBuf, 0, bufferLength);
261-
262-
buffer = newBuf;
263-
bufferLength = newBufLength;
226+
byte[] bytes = new byte[1];
227+
bytes[0] = (byte) (b & 0xff);
228+
String s = new String(bytes);
229+
if (s.equals("\n")) {
230+
flush();
231+
} else {
232+
buffer.append(s);
264233
}
265-
266-
buffer[count] = (byte) b;
267-
count++;
268234
}
269235

270236
/**
@@ -276,39 +242,15 @@ public void write(final int b) throws IOException {
276242
*/
277243
@Override
278244
public void flush() {
279-
280-
if (count == 0) {
281-
return;
245+
String message = buffer.toString().trim();
246+
if (message.length() > 0) {
247+
loggingLevel.log(log, message);
282248
}
283-
284-
// don't print out blank lines; flushing from PrintStream puts out
285-
// these
286-
if (count == LINE_SEPERATOR.length()) {
287-
if (((char) buffer[0]) == LINE_SEPERATOR.charAt(0) && ((count == 1) || // <-
288-
// Unix
289-
// &
290-
// Mac,
291-
// ->
292-
// Windows
293-
((count == 2) && ((char) buffer[1]) == LINE_SEPERATOR.charAt(1)))) {
294-
reset();
295-
return;
296-
}
297-
}
298-
299-
final byte[] theBytes = new byte[count];
300-
301-
System.arraycopy(buffer, 0, theBytes, 0, count);
302-
303-
loggingLevel.log(log, new String(theBytes));
304-
305249
reset();
306250
}
307251

308252
private void reset() {
309-
// not resetting the buffer -- assuming that if it grew that it
310-
// will likely grow similarly again
311-
count = 0;
253+
buffer = new StringBuffer();
312254
}
313255
}
314256
}

0 commit comments

Comments
 (0)