Class JBBPTextWriter

java.lang.Object
java.io.Writer
java.io.FilterWriter
com.igormaznitsa.jbbp.utils.JBBPTextWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public class JBBPTextWriter extends FilterWriter
the Writer allows to make text describes some bin data, it supports output of commentaries and values, also it is possible to register own extras to process complex data and cases. The Class is not thread safe.
Since:
1.1
  • Constructor Details

    • JBBPTextWriter

      public JBBPTextWriter()
      The Default constructor. A StringWriter will be used inside.
    • JBBPTextWriter

      public JBBPTextWriter(Writer out)
      Constructor to wrap a writer,
      Parameters:
      out - a writer to be wrapped, must not be null.
    • JBBPTextWriter

      public JBBPTextWriter(Writer out, JBBPByteOrder byteOrder)
      Constructor to wrap a writer with defined byte order.
      Parameters:
      out - a writer to be wrapped, must not be null.
      byteOrder - a byte order to be used, it must not be null.
    • JBBPTextWriter

      public JBBPTextWriter(Writer out, JBBPByteOrder byteOrder, String lineSeparator, int radix, String valuePrefix, String startValueLinePrefix, String commentPrefix, String hrPrefix, String valueDelimiter)
      Constructor.
      Parameters:
      out - a writer to be wrapper, must not be null.
      byteOrder - byte order to be used for converting, must not be null.
      lineSeparator - line separator, must not be null.
      radix - radix, must be 2..36.
      valuePrefix - prefix before each value, can be null.
      startValueLinePrefix - prefix before the first value on line, can be null.
      commentPrefix - prefix before comments, can be null.
      hrPrefix - prefix for horizontal rule
      valueDelimiter - delimiter between values, can be null
  • Method Details

    • makeStrWriter

      public static JBBPTextWriter makeStrWriter()
      Auxiliary method allows to build writer over StringWriter with system-depended next line and hex radix. The Method allows fast instance create.
      Returns:
      the text writer instance, must not be null
      Since:
      1.4.0
    • makeFieldComment

      protected static String makeFieldComment(JBBPAbstractField field)
    • getWrappedWriter

      public Writer getWrappedWriter()
      Get the wrapped writer.
      Returns:
      the wrapped writer
    • getByteOrder

      public JBBPByteOrder getByteOrder()
      Get the current byte order.
      Returns:
      the current byte order.
    • getRadix

      public int getRadix()
      get the current radix.
      Returns:
      the current radix
    • getLineSeparator

      public String getLineSeparator()
      Get the current line separator.
      Returns:
      the current line separator as string
    • EnableComments

      public JBBPTextWriter EnableComments()
      Enable print comments.
      Returns:
      the context
    • DisableComments

      public JBBPTextWriter DisableComments()
      Disable print comments.
      Returns:
      the context
    • Str

      public JBBPTextWriter Str(String... str) throws IOException
      Print string values.
      Parameters:
      str - array of string values, must not be null but may contain nulls
      Returns:
      the context
      Throws:
      IOException - it will be thrown for error
    • Byte

      public JBBPTextWriter Byte(int value) throws IOException
      Print byte value.
      Parameters:
      value - a byte value
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
    • Byte

      public JBBPTextWriter Byte(String value) throws IOException
      Print byte array defined as string.
      Parameters:
      value - string which codes should be printed as byte array.
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport error
    • Byte

      public JBBPTextWriter Byte(byte[] values) throws IOException
      Print byte array.
      Parameters:
      values - a byte array, must not be null
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
    • Byte

      public JBBPTextWriter Byte(byte[] array, int off, int len) throws IOException
      Print values from byte array.
      Parameters:
      array - source byte array, must not be null
      off - the offset of the first element in array
      len - number of bytes to be printed
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
    • isLineStart

      public boolean isLineStart()
      Check that line start mode is active.
      Returns:
      true if mode is line start, false otherwise
    • isComments

      public boolean isComments()
      Check that comment mode is active.
      Returns:
      true if comment mode is active, false otherwise
    • isValues

      public boolean isValues()
      Check that value mode is active.
      Returns:
      true if value mode is active, false otherwise
    • SetValueLinePrefix

      public final JBBPTextWriter SetValueLinePrefix(String text)
      Set prefix to be printed before start of values on every line.
      Parameters:
      text - string to be used as value line prefix, can be null
      Returns:
      the context
    • SetValuePrefix

      public final JBBPTextWriter SetValuePrefix(String text)
      Set prefix to be printed before every value.
      Parameters:
      text - string to be used as value prefix, can be null
      Returns:
      the context
    • SetValuePostfix

      public final JBBPTextWriter SetValuePostfix(String text)
      Set postfix to be printed after every value.
      Parameters:
      text - string to be used as value postfix, can be null
      Returns:
      the context
    • SetCommentPrefix

      public final JBBPTextWriter SetCommentPrefix(String text)
      Set prefix to be printed as comment start.
      Parameters:
      text - string to be used as comment start, can be null
      Returns:
      the context
    • SetValueSeparator

      public final JBBPTextWriter SetValueSeparator(String text)
      Set delimiter to be printed between values.
      Parameters:
      text - string to be used as separator between values.
      Returns:
      the context
    • AddExtras

      public JBBPTextWriter AddExtras(JBBPTextWriter.Extra... extras)
      Add extras to context.
      Parameters:
      extras - extras to be added to context, must not be null
      Returns:
      the context
    • DelExtras

      public JBBPTextWriter DelExtras(JBBPTextWriter.Extra... extras)
      Remove extras from context
      Parameters:
      extras - extras to be removed, must not be null
      Returns:
      the context
    • SetMaxValuesPerLine

      public JBBPTextWriter SetMaxValuesPerLine(int value)
      Set max number of values to be printed in one line.
      Parameters:
      value - max number of values to be presented on line, 0 or negative to turn off checking
      Returns:
      the context
    • SetTabSpaces

      public JBBPTextWriter SetTabSpaces(int numberOfSpacesPerTab)
      Set number of spaces per tab.
      Parameters:
      numberOfSpacesPerTab - number of spaces, must be greater than zero
      Returns:
      the context
    • ByteOrder

      public final JBBPTextWriter ByteOrder(JBBPByteOrder order)
      Set byte order.
      Parameters:
      order - new byte order, must not be null
      Returns:
      the context
    • Radix

      public final JBBPTextWriter Radix(int radix)
      Set radix.
      Parameters:
      radix - new radix value, must be 2..36
      Returns:
      the context
    • Short

      public JBBPTextWriter Short(int value) throws IOException
      Print short value.
      Parameters:
      value - short value to be printed
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
    • Float

      public JBBPTextWriter Float(float value) throws IOException
      Print float value.
      Parameters:
      value - float value to be printed
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
      Since:
      1.4.0
    • Double

      public JBBPTextWriter Double(double value) throws IOException
      Print double value.
      Parameters:
      value - double value to be printed
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
      Since:
      1.4.0
    • Short

      public JBBPTextWriter Short(String value) throws IOException
      Print char codes of string as short array.
      Parameters:
      value - string which codes should be printed, must not be null
      Returns:
      the context
      Throws:
      IOException - it will be thrown if any transport error
    • Short

      public JBBPTextWriter Short(short[] values) throws IOException
      Print array of short values.
      Parameters:
      values - array of short values, must not be null
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
    • Float

      public JBBPTextWriter Float(float[] values) throws IOException
      Print array of float values.
      Parameters:
      values - array of float values, must not be null
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
      Since:
      1.4.0
    • Double

      public JBBPTextWriter Double(double[] values) throws IOException
      Print array of double values.
      Parameters:
      values - array of double values, must not be null
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
      Since:
      1.4.0
    • Short

      public JBBPTextWriter Short(short[] values, int off, int len) throws IOException
      Print values from short array
      Parameters:
      values - short value array, must not be null
      off - offset to the first element
      len - number of elements to print
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport error
    • Float

      public JBBPTextWriter Float(float[] values, int off, int len) throws IOException
      Print values from float array
      Parameters:
      values - float value array, must not be null
      off - offset to the first element
      len - number of elements to print
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport error
      Since:
      1.4.0
    • Double

      public JBBPTextWriter Double(double[] values, int off, int len) throws IOException
      Print values from double array
      Parameters:
      values - double value array, must not be null
      off - offset to the first element
      len - number of elements to print
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport error
      Since:
      1.4.0
    • Int

      public JBBPTextWriter Int(int value) throws IOException
      Print integer value
      Parameters:
      value - value to be printed
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport error
    • UInt

      public JBBPTextWriter UInt(int value) throws IOException
      Print unsigned integer value
      Parameters:
      value - value to be printed
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport error
      Since:
      2.0.4
    • Int

      public JBBPTextWriter Int(int[] values) throws IOException
      Print integer array.
      Parameters:
      values - integer array to be printed, must not be null
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport error
    • UInt

      public JBBPTextWriter UInt(int[] values) throws IOException
      Print unsigned integer array.
      Parameters:
      values - unsigned integer array to be printed, must not be null
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport error
      Since:
      2.0.4
    • Int

      public JBBPTextWriter Int(int[] values, int off, int len) throws IOException
      Print values from integer array.
      Parameters:
      values - integer array, must not be null
      off - offset to the first element in array
      len - number of elements to print
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport error
    • UInt

      public JBBPTextWriter UInt(int[] values, int off, int len) throws IOException
      Print values from unsigned integer array.
      Parameters:
      values - unsigned integer array, must not be null
      off - offset to the first element in array
      len - number of elements to print
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport error
      Since:
      2.0.4
    • IndentInc

      public JBBPTextWriter IndentInc()
      Increase indent.
      Returns:
      the context
    • IndentInc

      public JBBPTextWriter IndentInc(int count) throws IOException
      Increase indent.
      Parameters:
      count - number of indents
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport error
    • IndentDec

      public JBBPTextWriter IndentDec()
      Decrease indent.
      Returns:
      the context
    • IndentDec

      public JBBPTextWriter IndentDec(int count) throws IOException
      Decrease indent.
      Parameters:
      count - number of indents
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport error
    • Long

      public JBBPTextWriter Long(long value) throws IOException
      Print long value
      Parameters:
      value - value to be printed
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
    • Long

      public JBBPTextWriter Long(long[] values) throws IOException
      Print values from long array.
      Parameters:
      values - array to be printed, must not be null
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
    • Long

      public JBBPTextWriter Long(long[] values, int off, int len) throws IOException
      print values from long array.
      Parameters:
      values - array to be printed, must not be null
      off - offset to the first element to print
      len - number of elements to be printed
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
    • BR

      public JBBPTextWriter BR() throws IOException
      Make new line.
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
    • SetHR

      public JBBPTextWriter SetHR(String prefix, int length, char ch)
      Change parameters for horizontal rule.
      Parameters:
      prefix - the prefix to be printed before rule, it can be null
      length - the length in symbols.
      ch - symbol to draw
      Returns:
      the context
    • HR

      public JBBPTextWriter HR() throws IOException
      Print horizontal rule. If comments are disabled then only next line will be added.
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
      See Also:
    • Comment

      public JBBPTextWriter Comment(String... comment) throws IOException
      Print comments. Wilt aligning of line start for multi-line comment. Comments will be printed only if they are allowed.
      Parameters:
      comment - array of string to be printed as comment lines.
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • Obj

      public JBBPTextWriter Obj(int objId, Object... obj) throws IOException
      Print objects.
      Parameters:
      objId - object id which will be provided to a converter as extra info
      obj - objects to be converted and printed, must not be null
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
    • Obj

      public JBBPTextWriter Obj(int objId, Object[] array, int off, int len) throws IOException
      Print objects from array.
      Parameters:
      objId - object id which will be provided to a converter as extra info
      array - array of objects, must not be null
      off - offset to the first element to be printed
      len - number of elements to be printed
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
    • Bin

      public JBBPTextWriter Bin(Object... objs) throws IOException
      Print objects which marked by Bin annotation or successors of JBBPAbstractField. NB! Keep in mind that values of fields will be processed for their attributes before printing and for instance a bit field with inversion will be shown as inverted one.
      Parameters:
      objs - array of object marked by Bin annotation or successors of JBBPAbstractField
      Returns:
      the context
      Throws:
      IOException - it will be thrown if transport errors
    • printAbstractFieldObject

      protected void printAbstractFieldObject(String postText, JBBPAbstractField field) throws IOException
      Throws:
      IOException
    • Close

      public JBBPTextWriter Close() throws IOException
      Close the wrapped writer.
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterWriter
      Throws:
      IOException
    • Flush

      public JBBPTextWriter Flush() throws IOException
      Flush buffers in wrapped writer.
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
    • flush

      public void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class FilterWriter
      Throws:
      IOException
    • Tab

      public JBBPTextWriter Tab() throws IOException
      Print tab as space chars.
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
    • Space

      public JBBPTextWriter Space(int numberOfSpaces) throws IOException
      Print number of spaces.
      Parameters:
      numberOfSpaces - number of spaces to print
      Returns:
      the context
      Throws:
      IOException - it will be thrown for transport errors
    • getLine

      public int getLine()
      Get the current line number.
      Returns:
      the current line number, the first one is zero
    • getLinePosition

      public int getLinePosition()
      Get the current line position.
      Returns:
      the current line position, the first one is zero
    • write

      public void write(String str) throws IOException
      Overrides:
      write in class Writer
      Throws:
      IOException
    • write

      public void write(char[] cbuf) throws IOException
      Overrides:
      write in class Writer
      Throws:
      IOException
    • write

      public void write(String str, int off, int len) throws IOException
      Overrides:
      write in class FilterWriter
      Throws:
      IOException
    • write

      public void write(char[] cbuf, int off, int len) throws IOException
      Overrides:
      write in class FilterWriter
      Throws:
      IOException
    • write

      public void write(int c) throws IOException
      Overrides:
      write in class FilterWriter
      Throws:
      IOException
    • append

      public Writer append(char c) throws IOException
      Specified by:
      append in interface Appendable
      Overrides:
      append in class Writer
      Throws:
      IOException
    • append

      public Writer append(CharSequence csq, int start, int end) throws IOException
      Specified by:
      append in interface Appendable
      Overrides:
      append in class Writer
      Throws:
      IOException
    • append

      public Writer append(CharSequence csq) throws IOException
      Specified by:
      append in interface Appendable
      Overrides:
      append in class Writer
      Throws:
      IOException