Class EscPosPrintStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public class EscPosPrintStream
    extends java.io.FilterOutputStream
    Adds functionality to another output stream, namely the ability to print representations of various data values conveniently through ESC/POS commands.

    All characters printed by a EscPosPrintStream are converted into bytes using the platform's default character encoding.

    Since:
    TotalCross 4.2.0
    • Constructor Detail

      • EscPosPrintStream

        public EscPosPrintStream​(java.io.OutputStream out)
        Creates an output stream filter for ESC/POS commands built on top of the specified underlying output stream.
        Parameters:
        out - the underlying output stream to be assigned to the field this.out for later use, or null if this instance is to be created without an underlying stream.
    • Method Detail

      • initialize

        public EscPosPrintStream initialize()
                                     throws java.io.IOException
        Initialize printer.

        Clears the data in the print buffer and resets the printer modes to the modes that were in effect when the power was turned on.

        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • feed

        public EscPosPrintStream feed()
                               throws java.io.IOException
        Print and return to standard mode (in page mode).

        In page mode, prints all the data in the print buffer collectively and switches from page mode to standard mode.

        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • ff

        public EscPosPrintStream ff()
                             throws java.io.IOException
        Print and return to standard mode (in page mode).

        In page mode, prints all the data in the print buffer collectively and switches from page mode to standard mode.

        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
        See Also:
        feed()
      • lineFeed

        public EscPosPrintStream lineFeed()
                                   throws java.io.IOException
        Print and line feed.

        Prints the data in the print buffer and feeds one line, based on the current line spacing.

        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • println

        public EscPosPrintStream println()
                                  throws java.io.IOException
        Print and line feed.

        Prints the data in the print buffer and feeds one line, based on the current line spacing.

        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
        See Also:
        lineFeed()
      • lf

        public EscPosPrintStream lf()
                             throws java.io.IOException
        Print and line feed.

        Prints the data in the print buffer and feeds one line, based on the current line spacing.

        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
        See Also:
        lineFeed()
      • carriageReturn

        public EscPosPrintStream carriageReturn()
                                         throws java.io.IOException
        Print and carriage return.
        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • cr

        public EscPosPrintStream cr()
                             throws java.io.IOException
        Print and carriage return.
        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
        See Also:
        carriageReturn()
      • horizontalTab

        public EscPosPrintStream horizontalTab()
                                        throws java.io.IOException
        Horizontal tab.

        Moves the print position to the next horizontal tab position.

        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • ht

        public EscPosPrintStream ht()
                             throws java.io.IOException
        Horizontal tab.

        Moves the print position to the next horizontal tab position.

        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
        See Also:
        horizontalTab()
      • buzzer

        public EscPosPrintStream buzzer()
                                 throws java.io.IOException
        Sounds the buzzer.
        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • bel

        public EscPosPrintStream bel()
                              throws java.io.IOException
        Sounds the buzzer.
        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
        See Also:
        buzzer()
      • feedPaper

        public EscPosPrintStream feedPaper​(int n)
                                    throws java.io.IOException
        Print and feed paper.

        Prints the data in the print buffer and feeds the paper n × (vertical or horizontal motion unit).

        A motion unit usually measures 1/203 inches or 0.125 mm.

        Parameters:
        n - the motion units value to feed, must be inside the range [0,255]
        Returns:
        This EscPosPrinter.
        Throws:
        java.lang.IllegalArgumentException - if the value of argument n is outside the range [0,255]
        java.io.IOException - if an I/O error occurs.
      • feedLines

        public EscPosPrintStream feedLines​(int n)
                                    throws java.io.IOException
        Prints the data in the print buffer and feeds n lines.
        Parameters:
        n - the number of lines to feed, must be inside the range [0,255]
        Returns:
        This EscPosPrinter.
        Throws:
        java.lang.IllegalArgumentException - if the value of argument n is outside the range [0,255]
        java.io.IOException - if an I/O error occurs.
      • textSize

        public EscPosPrintStream textSize​(byte characterSize)
                                   throws java.io.IOException
        Select character size

        The argument characterSize must be one of the EscPosConstants.CHARACTERSIZE constants, or a combination of a width constant with a height constant ored together.

        Most printers support only single or double width/height (constants 1 or 2), but other values in the range [0,255] may be supported by selected printers, refer to your printer documentation for more details.

        Parameters:
        characterSize - one of the EscPosConstants.CHARACTERSIZE constants, or a combination of a width constant with a height constant ored together.
        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
        See Also:
        CharacterSize, EscPosConstants.CHARACTERSIZE_WIDTH_1, EscPosConstants.CHARACTERSIZE_WIDTH_2, EscPosConstants.CHARACTERSIZE_HEIGHT_1, EscPosConstants.CHARACTERSIZE_HEIGHT_2
      • underline

        public EscPosPrintStream underline​(boolean enable)
                                    throws java.io.IOException
        Turn underline mode on/off.
        Parameters:
        enable - sets the command to enable or disable underline mode
        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • emphasize

        public EscPosPrintStream emphasize​(boolean enable)
                                    throws java.io.IOException
        Turn emphasized mode on/off.
        Parameters:
        enable - sets the command to enable or disable emphasize mode
        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • doubleStrike

        public EscPosPrintStream doubleStrike​(boolean enable)
                                       throws java.io.IOException
        Turn double-strike mode on/off.
        Parameters:
        enable - sets the command to enable or disable double-strike mode
        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • reverseWhiteBlack

        public EscPosPrintStream reverseWhiteBlack​(boolean enable)
                                            throws java.io.IOException
        Turn white/black reverse print mode on/off.
        Parameters:
        enable - sets the command to enable or disable white/black reverse print mode
        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • rotate

        public EscPosPrintStream rotate​(boolean enable)
                                 throws java.io.IOException
        Turn 90° clockwise rotation mode on/off.
        Parameters:
        enable - sets the command to enable or disable 90° clockwise rotation mode
        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • rotate

        public EscPosPrintStream rotate​(byte value)
                                 throws java.io.IOException
        Sets 90° clockwise rotation with given value.

        Provided for printers that define other values for rotation mode besides on/off, refer to your printer documentation for more information.

        Parameters:
        value - a valid value for the 'ESC V' command as defined by your printer documentation
        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • absolutePrintPosition

        public EscPosPrintStream absolutePrintPosition​(int n)
                                                throws java.io.IOException
        Set absolute print position.

        Moves the print position to n × (horizontal or vertical motion unit) from the left edge of the print area.

        A motion unit usually measures 1/203 inches or 0.125 mm.

        Parameters:
        n - the motion units value to set for the absolute print position from the left edge of the print area, which mus be in the range [0,65535]
        Returns:
        This EscPosPrinter.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • horizontalTabPosition

        public EscPosPrintStream horizontalTabPosition​(int... tabPositions)
                                                throws java.io.IOException
        Set horizontal tab positions.

        A maximum of 32 horizontal tab positions in ascending order can be set, with each value in the range [0,255].

        Executing this method with no arguments clears all the set tab positions.

        ATTENTION: The maximum amount of tab positions may be lower for some printers - usually any data past the accepted maximum of tab positions will be processed as general data. Refer to your printer documentation before using this command with more than 8 tab positions.

        Devices with known maximum number of tab positions:

      • Datecs DPP-350: 32
      • Leopardo A7: 8
Parameters:
tabPositions - A maximum of 32 tab positions in ascending orders, with each value in the range [0,255], or empty to clear all the set tab positions.
Returns:
This EscPosPrinter.
Throws:
java.lang.IllegalArgumentException - if the maximum number of tab positions is greater than 32, OR if any given tab position is outside the range [0,255], OR if any given tab position is less than or equal the preceding value.
java.io.IOException - if an I/O error occurs.
Returns:
This EscPosPrinter.
Throws:
java.io.IOException - if an I/O error occurs.