Formats a packet for text output to an {@link java.lang.Appendable} output.Appendable interface is implemented by standard {@link java.io.PrintStream}(i.e. System.out) and standard {@link java.lang.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 {@link PacketFormatter#getPrintFormatter()} and{@link PacketFormatter#getStringFormatter()} static methods. The{@link Packet.format} and {@link Packet.toString} methods each use one of the2 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 {@link FormatType} enumdefinition.
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 {@value PacketFormatter#DEFAULT_FORMAT_TYPE}, default output is System.out. The method {@link Packet#toString()} uses the default platformFormatter asretrieved using the static {@link PacketFormatter#getPrintFormatter()} methodcall and to the values the deafault platformFormatter is initialized to. @author Mark Bednarczyk @author Sly Technologies, Inc.
|
|
|
|
|
|
|
|