Class EscPosPrintStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- totalcross.io.device.escpos.EscPosPrintStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public class EscPosPrintStream extends java.io.FilterOutputStreamAdds 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
EscPosPrintStreamare converted into bytes using the platform's default character encoding.- Since:
- TotalCross 4.2.0
-
-
Constructor Summary
Constructors Constructor Description EscPosPrintStream(java.io.OutputStream out)Creates an output stream filter for ESC/POS commands built on top of the specified underlying output stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description EscPosPrintStreamabsolutePrintPosition(int n)Set absolute print position.EscPosPrintStreamalign(byte alignment)Select alignment.EscPosPrintStreambel()Sounds the buzzer.EscPosPrintStreambuzzer()Sounds the buzzer.EscPosPrintStreamcarriageReturn()Print and carriage return.EscPosPrintStreamcharacterSpacing(int n)Set right-side character spacing.EscPosPrintStreamcharset(java.lang.String charsetName)Sets the default charset to be used when printing CharSequence to the underlying stream.EscPosPrintStreamcharset(java.nio.charset.Charset charset)Sets the default charset to be used when printing CharSequence to the underlying stream.EscPosPrintStreamcr()Print and carriage return.EscPosPrintStreamcut(byte cut)Select cut mode and cut paper.EscPosPrintStreamdoubleStrike(boolean enable)Turn double-strike mode on/off.EscPosPrintStreamemphasize(boolean enable)Turn emphasized mode on/off.EscPosPrintStreamfeed()Print and return to standard mode (in page mode).EscPosPrintStreamfeedLines(int n)Prints the data in the print buffer and feeds n lines.EscPosPrintStreamfeedPaper(int n)Print and feed paper.EscPosPrintStreamff()Print and return to standard mode (in page mode).EscPosPrintStreamhorizontalTab()Horizontal tab.EscPosPrintStreamhorizontalTabPosition(int... tabPositions)Set horizontal tab positions.EscPosPrintStreamht()Horizontal tab.EscPosPrintStreaminitialize()Initialize printer.EscPosPrintStreamleftMargin(int n)Set left margin.EscPosPrintStreamlf()Print and line feed.EscPosPrintStreamlineFeed()Print and line feed.EscPosPrintStreamlineSpacing(int n)Set line spacing.EscPosPrintStreamprint(byte b)Writes the specified byte to the underlying stream.EscPosPrintStreamprint(byte... b)Writes the given bytes to the underlying output stream.EscPosPrintStreamprint(byte[] b, int off, int len)Writeslenbytes from the specified byte array starting at offsetoffto the underlying output stream.EscPosPrintStreamprint(int b)Writes the specified byte to the underlying stream.EscPosPrintStreamprint(java.lang.CharSequence text)Writes the given text to the underlying stream.EscPosPrintStreamprint(EscPosPrintObject printStruct)Writes an EscPosPrintObject to the underlying stream.EscPosPrintStreamprint(EscPosPrintObject... commands)Writes a set of EscPosPrintObject to the underlying stream.EscPosPrintStreamprintln()Print and line feed.EscPosPrintStreamprintLogo(byte mode)Print downloaded bit image using the specified mode (size).EscPosPrintStreamresetLineSpacing()Select default line spacing.EscPosPrintStreamreverseWhiteBlack(boolean enable)Turn white/black reverse print mode on/off.EscPosPrintStreamrotate(boolean enable)Turn 90° clockwise rotation mode on/off.EscPosPrintStreamrotate(byte value)Sets 90° clockwise rotation with given value.EscPosPrintStreamselfTest()Prints test page and self-diagnostic information.EscPosPrintStreamshortSelfTest()Prints current printer parameters, including intensity, temperature of the print head, battery voltage, speed in case of serial connection, etc.EscPosPrintStreamtextPrintMode(int textPrintMode)Select print mode(s).EscPosPrintStreamtextSize(byte characterSize)Select character sizeEscPosPrintStreamturnOff()Switch OFF the printer.EscPosPrintStreamunderline(boolean enable)Turn underline mode on/off.
-
-
-
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, ornullif 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.
-
textPrintMode
public EscPosPrintStream textPrintMode(int textPrintMode) throws java.io.IOException
Select print mode(s).The argument textPrintMode must be one of the EscPosConstants.TEXTPRINTMODE constants defined, or any valid combination of them ored together.
Other values in the range [0,255] may be supported by selected printers, refer to your printer documentation for more details.
- Parameters:
textPrintMode- one of the EscPosConstants.TEXTPRINTMODE constants, or any valid combination of them ored together.- Returns:
- This EscPosPrinter.
- Throws:
java.lang.IllegalArgumentException- if the value of argument textPrintMode is outside the range [0,255]java.io.IOException- if an I/O error occurs.- See Also:
EscPosConstants.TEXTPRINTMODE_FONT_1,EscPosConstants.TEXTPRINTMODE_FONT_2,EscPosConstants.TEXTPRINTMODE_EMPHASIZED,EscPosConstants.TEXTPRINTMODE_DOUBLE_HEIGHT,EscPosConstants.TEXTPRINTMODE_DOUBLE_WIDTH,EscPosConstants.TEXTPRINTMODE_UNDERLINE
-
textSize
public EscPosPrintStream textSize(byte characterSize) throws java.io.IOException
Select character sizeThe 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.
-
resetLineSpacing
public EscPosPrintStream resetLineSpacing() throws java.io.IOException
Select default line spacing.- Returns:
- This EscPosPrinter.
- Throws:
java.io.IOException- if an I/O error occurs.
-
lineSpacing
public EscPosPrintStream lineSpacing(int n) throws java.io.IOException
Set line spacing.Sets the line spacing to n × (vertical or horizontal motion unit).
A motion unit usually measures 1/203 inches or 0.125 mm.
- Parameters:
n- the size of the line spacing in motion units, which 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.
-
characterSpacing
public EscPosPrintStream characterSpacing(int n) throws java.io.IOException
Set right-side character spacing.Sets the right-side character spacing to n × (horizontal or vertical motion unit).
A motion unit usually measures 1/203 inches or 0.125 mm.
- Parameters:
n- the size of the line spacing in motion units, which 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.
-
align
public EscPosPrintStream align(byte alignment) throws java.io.IOException
Select alignment.Sets the printing alignment to either ALIGN_LEFT, ALIGN_CENTER or ALIGN_RIGHT.
- Parameters:
alignment- the printing alignment, which must be either ALIGN_LEFT, ALIGN_CENTER or ALIGN_RIGHT.- Returns:
- This EscPosPrinter.
- Throws:
java.lang.IllegalArgumentException- if the value of argument alignment is not ALIGN_LEFT, ALIGN_CENTER or ALIGN_RIGHT.java.io.IOException- if an I/O error occurs.- See Also:
EscPosConstants.ALIGN_LEFT,EscPosConstants.ALIGN_CENTER,EscPosConstants.ALIGN_RIGHT
-
leftMargin
public EscPosPrintStream leftMargin(int n) throws java.io.IOException
Set left margin.In standard mode, sets the left margin to n × (horizontal motion unit) from the left edge of the printable area.
A motion unit usually measures 1/203 inches or 0.125 mm.
- Parameters:
n- the motion units value to set the left margin, must be inside the range [0,65535]- Returns:
- This EscPosPrinter.
- Throws:
java.lang.IllegalArgumentException- if the value of argument n is outside the range [0,65535]java.io.IOException- if an I/O error occurs.
-
cut
public EscPosPrintStream cut(byte cut) throws java.io.IOException
Select cut mode and cut paper.- Parameters:
cut- the cut mode, which must be either CUT_FULL or CUT_PART.- Returns:
- This EscPosPrinter.
- Throws:
java.lang.IllegalArgumentException- if the value of argument cut is not either CUT_FULL or CUT_PARTjava.io.IOException- if an I/O error occurs.
-
turnOff
public EscPosPrintStream turnOff() throws java.io.IOException
Switch OFF the printer.This command is not listed in the original ESC/POS Manual, refer to the printer's manual to verify support for this command.
Known supported devices:
- Datecs DPP-350
- Returns:
- This EscPosPrinter.
- Throws:
java.io.IOException- if an I/O error occurs.
-
selfTest
public EscPosPrintStream selfTest() throws java.io.IOException
Prints test page and self-diagnostic information. The self-diagnostic information includes print density, print head temperature, battery voltage, baud rate in case of work via RS232 and others.This command is not listed in the original ESC/POS Manual, refer to the printer's manual to verify support for this command.
Known supported devices:
- Datecs DPP-350
- Returns:
- This EscPosPrinter.
- Throws:
java.io.IOException- if an I/O error occurs.
-
shortSelfTest
public EscPosPrintStream shortSelfTest() throws java.io.IOException
Prints current printer parameters, including intensity, temperature of the print head, battery voltage, speed in case of serial connection, etc.This command is not listed in the original ESC/POS Manual, refer to the printer's manual to verify support for this command.
Known supported devices:
- Datecs DPP-350
- Returns:
- This EscPosPrinter.
- Throws:
java.io.IOException- if an I/O error occurs.
-
charset
public EscPosPrintStream charset(java.nio.charset.Charset charset) throws java.lang.IllegalArgumentException
Sets the default charset to be used when printing CharSequence to the underlying stream.- Parameters:
charset- TheCharsetto be used to encode the data- Returns:
- This EscPosPrinter.
- Throws:
java.lang.IllegalArgumentException- If the given charset is null
-
charset
public EscPosPrintStream charset(java.lang.String charsetName) throws java.lang.IllegalArgumentException, java.io.UnsupportedEncodingException
Sets the default charset to be used when printing CharSequence to the underlying stream.- Parameters:
charsetName- The name of a supportedcharset- Returns:
- This EscPosPrinter.
- Throws:
java.lang.IllegalArgumentException- If the given charsetName is nulljava.io.UnsupportedEncodingException- If no support for the named charset is available
-
printLogo
public EscPosPrintStream printLogo(byte mode) throws java.io.IOException
Print downloaded bit image using the specified mode (size).The value of argument mode must be either LOGO_NORMAL, LOGO_DOUBLE_WIDTH, LOGO_DOUBLE_HEIGHT or LOGO_QUADRUPLE.
- Parameters:
mode- the printing mode, which must be either LOGO_NORMAL, LOGO_DOUBLE_WIDTH, LOGO_DOUBLE_HEIGHT or LOGO_QUADRUPLE.- Returns:
- This EscPosPrinter.
- Throws:
java.lang.IllegalArgumentException- if the value of argument mode is not LOGO_NORMAL, LOGO_DOUBLE_WIDTH, LOGO_DOUBLE_HEIGHT or LOGO_QUADRUPLE.java.io.IOException- if an I/O error occurs.- See Also:
EscPosConstants.LOGO_NORMAL,EscPosConstants.LOGO_DOUBLE_WIDTH,EscPosConstants.LOGO_DOUBLE_HEIGHT,EscPosConstants.LOGO_QUADRUPLE
-
print
public EscPosPrintStream print(byte b) throws java.io.IOException
Writes the specified byte to the underlying stream.- Parameters:
b- thebyte.- Throws:
java.io.IOException- if an I/O error occurs.
-
print
public EscPosPrintStream print(int b) throws java.io.IOException
Writes the specified byte to the underlying stream. The general contract forwriteis that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argumentb. The 24 high-order bits ofbare ignored.- Parameters:
b- thebyte.- Throws:
java.io.IOException- if an I/O error occurs.
-
print
public EscPosPrintStream print(byte[] b, int off, int len) throws java.io.IOException
Writeslenbytes from the specified byte array starting at offsetoffto the underlying output stream.If
bisnull, aNullPointerExceptionis thrown.If
offis negative, orlenis negative, oroff+lenis greater than the length of the arrayb, then an IndexOutOfBoundsException is thrown.- Parameters:
b- the data.off- the start offset in the data.len- the number of bytes to write.- Throws:
java.io.IOException- if an I/O error occurs.
-
print
public EscPosPrintStream print(byte... b) throws java.io.IOException
Writes the given bytes to the underlying output stream.- Parameters:
b- the data.- Throws:
java.io.IOException- if an I/O error occurs.
-
print
public EscPosPrintStream print(java.lang.CharSequence text) throws java.io.IOException
Writes the given text to the underlying stream.All characters are converted into bytes using the platform's default character encoding.
- Parameters:
text- the data- Returns:
- This EscPosPrinter.
- Throws:
java.io.IOException- if an I/O error occurs.
-
print
public EscPosPrintStream print(EscPosPrintObject printStruct) throws java.io.IOException
Writes an EscPosPrintObject to the underlying stream.- Parameters:
printStruct- an object that writes its own set of ESC/POS commands- Returns:
- This EscPosPrinter.
- Throws:
java.io.IOException- if an I/O error occurs.
-
print
public EscPosPrintStream print(EscPosPrintObject... commands) throws java.io.IOException
Writes a set of EscPosPrintObject to the underlying stream.- Parameters:
commands- a set of EscPosPrintStruct- Returns:
- This EscPosPrinter.
- Throws:
java.io.IOException- if an I/O error occurs.
-
-