org.jnetstream.packet.format
Class PacketFormatter

java.lang.Object
  extended by org.jnetstream.packet.format.PacketFormatter
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class PacketFormatter
extends java.lang.Object
implements java.io.Closeable, java.io.Flushable

Formats a packet for text output to an Appendable output. Appendable interface is implemented by standard PrintStream (i.e. System.out) and standard StringBuilder, plus of course any custom objects. The packet contents are formatted according to the default PacketFormat definition and sent to the output (Appendable).

There are actually 2 separate instances of PacketFormatter to handle PrintStream type output and for formatting into a string buffer. You can retrieve each of these formatters using PacketFormatter#getPrintFormatter() and PacketFormatter#getStringFormatter() static methods. The Packet.format and Packet.toString methods each use one of the 2 formatters. Packet.format uses the platformFormatter that outputs to a PrintStream by default and the Packet.toString method uses the platformFormatter that output to an internal string buffer which content can be retrieved using the PacketFormatter.toString() method.

A number of predefined formats are supplied and can be set as default formatters or simply used directly to delagate packet content directly. The supplied formats can be accessed using the FormatType enum definition.

For example, lets switch the default delagate used from default FormatType.Plain to FormatType.Html:
     
     Packet packet = ... some packet we current have reference to
     PacketFormatter.setDefault(PacketFormatter.Type.Html);
     PacketFormatter.PacketFormat out = PacketFormatter.getDefault();
     out.format(packet);
     
 
How about switching the output from default System.out to a cutom StringBuilder buffer:
     
     StringBuilder b = new StringBuilder(1024); 
     PacketFormatter.setDefault(b);
     Packet packet = ... some packet 
     PacketFormatter.PacketFormat out = PacketFormatter.getDefault(); 
     out.format(packet);
     System.out.println("Our nicely platformFormatter packet:\n" + b.toString());
     
 
The defaults after initialization are as follows. Default delagate type is , default output is System.out. The method Packet#toString() uses the default platformFormatter as retrieved using the static PacketFormatter#getPrintFormatter() method call and to the values the deafault platformFormatter is initialized to.

Author:
Mark Bednarczyk, Sly Technologies, Inc.

Nested Class Summary
static interface PacketFormatter.Builder
          Builds a new instance of a PacketFormatter.
 
Constructor Summary
  PacketFormatter()
           Constructs a new packet formatter.
  PacketFormatter(java.lang.Appendable a)
           Constructs a new formatter with the specified destination.
  PacketFormatter(java.lang.Appendable a, java.util.Locale l)
          Constructs a new formatter with the specified destination and locale.
protected PacketFormatter(boolean init)
           Constructs a new packet formatter.
  PacketFormatter(FormatterType formatType)
           
  PacketFormatter(FormatterType formatType, java.lang.Appendable a)
           
  PacketFormatter(FormatterType formatType, java.lang.Appendable a, java.util.Locale l)
           
 
Method Summary
 void close()
           
 void flush()
           
 PacketFormatter format(Header header)
           
 PacketFormatter format(java.util.Locale l, Packet packet)
           
 java.util.Formatter format(java.util.Locale l, java.lang.String format, java.lang.Object... args)
           
 PacketFormatter format(Packet packet)
           
 java.util.Formatter format(java.lang.String format, java.lang.Object... args)
           
static boolean hasRegisteredBuilder(FormatterType formatType)
           
 java.io.IOException ioException()
           
 java.util.Locale locale()
           
 java.lang.Appendable out()
           
static void registerBuilder(FormatterType formatType, PacketFormatter.Builder builder)
           
protected  java.util.Locale setLocale(java.util.Locale l)
           
protected  void setOut(java.lang.Appendable o)
           
protected  void setPlatformFormatter(java.util.Formatter f)
           
 java.lang.String toString()
          Returns the contents of the internal string buffer and resets the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PacketFormatter

public PacketFormatter()

Constructs a new packet formatter.

The destination of the formatted output is a StringBuilder which may be retrieved by invoking out() and whose current content may be converted into a string by invoking toString(). The locale used is the default locale for this instance of the Java virtual machine.


PacketFormatter

public PacketFormatter(java.lang.Appendable a)

Constructs a new formatter with the specified destination.

The locale used is the default locale for this instance of the Java virtual machine.

Parameters:
a - Destination for the formatted output. If a is null then a StringBuilder will be created.

PacketFormatter

public PacketFormatter(java.lang.Appendable a,
                       java.util.Locale l)
Constructs a new formatter with the specified destination and locale.

Parameters:
a - Destination for the formatted output. If a is null then a StringBilder will be created.
l - The locale to apply during formatting. If l is null then no localization is applied.

PacketFormatter

protected PacketFormatter(boolean init)

Constructs a new packet formatter.

The destination of the formatted output is a StringBuilder which may be retrieved by invoking out() and whose current content may be converted into a string by invoking toString(). The locale used is the default locale for this instance of the Java virtual machine.


PacketFormatter

public PacketFormatter(FormatterType formatType)

PacketFormatter

public PacketFormatter(FormatterType formatType,
                       java.lang.Appendable a)

PacketFormatter

public PacketFormatter(FormatterType formatType,
                       java.lang.Appendable a,
                       java.util.Locale l)
Method Detail

hasRegisteredBuilder

public static boolean hasRegisteredBuilder(FormatterType formatType)
Parameters:
formatType -
Returns:

registerBuilder

public static void registerBuilder(FormatterType formatType,
                                   PacketFormatter.Builder builder)

close

public void close()
Specified by:
close in interface java.io.Closeable
See Also:
Formatter.close()

flush

public void flush()
Specified by:
flush in interface java.io.Flushable
See Also:
Formatter.flush()

format

public PacketFormatter format(java.util.Locale l,
                              Packet packet)

format

public java.util.Formatter format(java.util.Locale l,
                                  java.lang.String format,
                                  java.lang.Object... args)
Parameters:
l -
delagate -
args -
Returns:
See Also:
Formatter.format(java.util.Locale, java.lang.String, java.lang.Object[])

format

public PacketFormatter format(Header header)
                       throws java.io.IOException
Parameters:
header -
Returns:
Throws:
java.io.IOException
See Also:
org.jnetstream.packet.format.PacketFormat#format(MetaPacket, org.jnetstream.packet.MetaHeader)

format

public PacketFormatter format(Packet packet)
Parameters:
packet -
Returns:
Throws:
java.io.IOException
See Also:
org.jnetstream.packet.format.PacketFormat#format(org.jnetstream.packet.MetaPacket)

format

public java.util.Formatter format(java.lang.String format,
                                  java.lang.Object... args)
Parameters:
delagate -
args -
Returns:
See Also:
Formatter.format(java.lang.String, java.lang.Object[])

ioException

public java.io.IOException ioException()
Returns:
See Also:
Formatter.ioException()

locale

public java.util.Locale locale()
Returns:
See Also:
Formatter.locale()

out

public java.lang.Appendable out()
Returns:
See Also:
Formatter.out()

setLocale

protected java.util.Locale setLocale(java.util.Locale l)

setOut

protected void setOut(java.lang.Appendable o)

setPlatformFormatter

protected void setPlatformFormatter(java.util.Formatter f)

toString

public java.lang.String toString()
Returns the contents of the internal string buffer and resets the buffer. If the PacketFormatter output is not the internal string buffer then the current Appendable.toString() output is returned. That is the default toString invokation on the current output.

Overrides:
toString in class java.lang.Object
Returns:
contents of the internal string buffer