/*
* Enhydra Java Application Server Project
*
* The contents of this file are subject to the Enhydra Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License on
* the Enhydra web site ( http://www.enhydra.org/ ).
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific terms governing rights and limitations
* under the License.
*
* The Initial Developer of the Enhydra Application Server is Lutris
* Technologies, Inc. The Enhydra Application Server and portions created
* by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
* All Rights Reserved.
*
* Contributor(s):
* Paul Mahar
*
*/
package org.enhydra.kelp.common;
// Kelp imports
import org.enhydra.kelp.common.event.WriteEvent;
import org.enhydra.kelp.common.event.WriteListener;
// Standard imports
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Class declaration
*
*/
public class Writer extends StringWriter {
private WriteListener[] writeListeners = new WriteListener[0];
private boolean buffered = false;
private StringBuffer lineBuffer = new StringBuffer();
/**
* Constructor declaration
*
*/
public Writer() {}
public void setLineBuffered(boolean b) {
buffered = b;
}
public boolean isLineBuffered() {
return buffered;
}
/**
* Method declaration
*
*
* @param c
*/
public synchronized void write(int c) {
char[] ca = new char[1];
ca[0] = (char) c;
String cs = new String(ca);
write(cs);
}
/**
* Method declaration
*
*
* @param str
*/
public synchronized void write(String str) {
// if (isLineBuffered()) {
// if (str != null && str.trim().length() > 0) {
// write(str, 0, str.length());
// }
// } else {
write(str, 0, str.length());
// }
}
/**
* Method declaration
*
*
* @param str
*/
public synchronized void writeln(String str) {
write(str + '\n');
}
/**
* Method declaration
*
*
* @param str
* @param off
* @param len
*/
public synchronized void write(String str, int off, int len) {
String out = str.substring(off, (off + len));
boolean writeLine = false;
if (isLineBuffered()) {
if (out.length() > 1) {
char end1 = out.charAt(out.length() - 2);
char end2 = out.charAt(out.length() - 1);
if ((int) end1 == 13 && (int) end2 == 10) {
if (out.length() < 3) {
out = new String();
} else {
out = out.substring(0, out.length() - 3);
}
writeLine = true;
}
if ((out.length() > 0)
&& (out.charAt(out.length() - 1) == '\n')) {
writeLine = true;
}
}
if (writeLine) {
notifyWriteListeners(lineBuffer.toString() + out);
lineBuffer = new StringBuffer();
} else {
lineBuffer.append(out);
}
} else {
notifyWriteListeners(out);
}
}
// event methods
/**
* Method declaration
*
*
* @param l
*/
public synchronized void addWriteListener(WriteListener l) {
ArrayList list = null;
list = new ArrayList(Arrays.asList(writeListeners));
if (! list.contains(l)) {
list.add(l);
}
list.trimToSize();
writeListeners = new WriteListener[list.size()];
writeListeners = (WriteListener[]) list.toArray(writeListeners);
list.clear();
}
/**
* Method declaration
*
*
* @param l
*/
public synchronized void removeWriteListener(WriteListener l) {
ArrayList list = null;
list = new ArrayList(Arrays.asList(writeListeners));
if (list.contains(l)) {
list.remove(l);
}
list.trimToSize();
writeListeners = new WriteListener[list.size()];
writeListeners = (WriteListener[]) list.toArray(writeListeners);
list.clear();
}
public synchronized WriteListener[] getWriteListeners() {
return writeListeners;
}
/**
* Method declaration
*
*
* @param s
*/
private synchronized void notifyWriteListeners(String s) {
WriteEvent event = new WriteEvent(this, WriteEvent.OUTPUT, s);
for (int i = 0; i < writeListeners.length; i++) {
writeListeners[i].onWrite(event);
}
}
}
|