Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS | NEXT CLASS FRAMES  | NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD DETAIL:  FIELD | CONSTR | METHOD

Class ncsa.horizon.util.MessageBuffer

java.lang.Object
  |
  +--java.io.Writer
        |
        +--ncsa.horizon.util.MessageBuffer

public class MessageBuffer
extends java.io.Writer
implements java.lang.Cloneable, java.io.Serializable
a buffer holding text that grows as necessary but can be optionally limited to a certain maximum size.

The limit of the buffer is set when the buffer is constructed. This limit can be set either as a number of characters/bytes (default) or a number of lines (as delimited by \n characters). Clients write messages to this writer which are appended to the end of the buffer. If the size of the buffer plus the size of the new message (either in number of lines or characters) exceeds the buffer limit, text at the beginning of the buffer will be trimmed off.

How much text gets automatically trimmed depends on the property accessed via is/setTrimmedByLines(). If isTrimmedByLines() returns true (which is the default), whole lines will be trimmed from the beginning of the buffer as necessary to keep the buffer size below its limit.

This class also supports listeners who are notified whenever the buffer is flushed or cleared. (Flushing does not cause any data to be thrown away.) This allows such clients as gui components (like a TextArea) to update their displays, especially when new messages are sent. In this case, it is convenient to wrap the buffer with a PrintWriter or MultiPrintWriter with auto-flushing; this will cause listeners to be alerted automatically after every complete line (ending in a newline character).

See Also:
MultiPrintWriter, MessageCenter, Serialized Form

Inner Class Summary
MessageBuffer.Enum
           
static  MessageBuffer.Listener
          an interface for objects that wish to listen for changes to this buffer
 
Field Summary
int capacity
          the maximum capacity.
boolean capIsByLines
          true if capacity refers to a number of lines
boolean trimByLines
          true if the buffer should be automatically trimmed at line boundaries
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
MessageBuffer(int maxcap, boolean byLines, java.lang.String str)
          create a MessageBuffer with a given maximum capacity and given contents
MessageBuffer(int maxcap, boolean byLines)
          create a MessageBuffer with a given maximum capacity
MessageBuffer(int maxchar)
          create a MessageBuffer with a given maximum character capacity
MessageBuffer()
          create a MessageBuffer with unlimited capacity
 
Method Summary
void addListener(MessageBuffer.Listener l)
          add a listener of this buffer
boolean capacityIsByLines()
          return true if the capacity of the buffer has been set by the number of lines
void clear()
          clear contents of buffer
java.lang.Object clone()
           
void close()
          close this writer.
static int countLines(char[] str, int offset, int count)
          count the number of lines in a given string
void flush()
          flush this writer.
int getLineCount()
          return the number of lines in the buffer
boolean isTrimmedByLines()
          return true if this buffer will be trimmed by whole lines.
int length()
          return length of contents
MessageBuffer.Enum lines()
          get an enumerator of the lines in the buffer
static void main(java.lang.String[] argv)
           
int maxCapacity()
          return the maximum this buffer can hold.
void removeListener(MessageBuffer.Listener l)
          remove a listener of this buffer
void setTrimmedByLines(boolean yes)
          set whether this buffer will be trimmed by whole lines.
java.lang.String toString()
          convert contents to a String
void trimChars(int nchars)
          trim characters from the beginning of the buffer
void trimLines(int nlines)
          trim whole lines from the beginning of the buffer
void write(char[] str, int offset, int count)
          append a message to the buffer
 
Methods inherited from class java.io.Writer
close, flush, write, write, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notifyAll, notify, toString, wait, wait, wait
 

Field Detail

trimByLines

protected boolean trimByLines
true if the buffer should be automatically trimmed at line boundaries

capacity

protected int capacity
the maximum capacity. A value of zero or less means no limit.

capIsByLines

protected boolean capIsByLines
true if capacity refers to a number of lines
Constructor Detail

MessageBuffer

public MessageBuffer(int maxcap,
                     boolean byLines,
                     java.lang.String str)
create a MessageBuffer with a given maximum capacity and given contents
Parameters:
maxcap - the maximum size of the buffer; there is no maximum limit if maxcap <= 0
byLines - if true, the maxcap refers to the number of lines; otherwise, it is the number of characters.
str - string to initialize buffer with (ignored if null)

MessageBuffer

public MessageBuffer(int maxcap,
                     boolean byLines)
create a MessageBuffer with a given maximum capacity
Parameters:
maxcap - the maximum size of the buffer; there is no maximum limit if maxcap <= 0
byLines - if true, the maxcap refers to the number of lines; otherwise, it is the number of characters.

MessageBuffer

public MessageBuffer(int maxchar)
create a MessageBuffer with a given maximum character capacity
Parameters:
maxchar - the maximum number of characters in the buffer; there is no maximum limit if maxcap <= 0

MessageBuffer

public MessageBuffer()
create a MessageBuffer with unlimited capacity
Method Detail

isTrimmedByLines

public boolean isTrimmedByLines()
return true if this buffer will be trimmed by whole lines. Lines delimited by newline characters

setTrimmedByLines

public void setTrimmedByLines(boolean yes)
set whether this buffer will be trimmed by whole lines. Lines delimited by newline characters

write

public void write(char[] str,
                  int offset,
                  int count)
append a message to the buffer
Overrides:
write in class java.io.Writer

trimChars

public void trimChars(int nchars)
trim characters from the beginning of the buffer
Parameters:
nchars - the number of characters to trim from beginning of buffer.

trimLines

public void trimLines(int nlines)
trim whole lines from the beginning of the buffer

countLines

public static int countLines(char[] str,
                             int offset,
                             int count)
count the number of lines in a given string

capacityIsByLines

public boolean capacityIsByLines()
return true if the capacity of the buffer has been set by the number of lines

maxCapacity

public int maxCapacity()
return the maximum this buffer can hold. If capacityIsByLines() is true, then the return value is the maximum number of lines; otherwise, it is the maximum number of characters.

toString

public java.lang.String toString()
convert contents to a String
Overrides:
toString in class java.lang.Object

length

public int length()
return length of contents

getLineCount

public int getLineCount()
return the number of lines in the buffer

clear

public void clear()
clear contents of buffer

lines

public MessageBuffer.Enum lines()
get an enumerator of the lines in the buffer

flush

public void flush()
flush this writer. This implementation does nothing.
Overrides:
flush in class java.io.Writer

close

public void close()
close this writer. This implementation does nothing.
Overrides:
close in class java.io.Writer

addListener

public void addListener(MessageBuffer.Listener l)
add a listener of this buffer

removeListener

public void removeListener(MessageBuffer.Listener l)
remove a listener of this buffer

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

main

public static void main(java.lang.String[] argv)

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS | NEXT CLASS FRAMES  | NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD DETAIL:  FIELD | CONSTR | METHOD