Josh
Native console access wrapper for Java

com.yifanlu.Josh
Class JoshOutBuffer

java.lang.Object
  extended by com.yifanlu.Josh.JoshBuffer
      extended by com.yifanlu.Josh.JoshOutBuffer

public class JoshOutBuffer
extends JoshBuffer

Provides native access to the output buffer. This allows direct manipulation of the console screen buffer.
Objects from this class are immutable.

Since:
0.1
Version:
1.3, 05/29/10
Author:
Yifan Lu

Field Summary
static int ENABLE_ECHO_INPUT
          Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.
static int ENABLE_INSERT_MODE
          Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.
static int ENABLE_LINE_INPUT
          Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.
static int ENABLE_MOUSE_INPUT
          Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.
static int ENABLE_PROCESSED_INPUT
          Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.
static int ENABLE_PROCESSED_OUTPUT
          Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.
static int ENABLE_QUICK_EDIT_MODE
          Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.
static int ENABLE_WINDOW_INPUT
          Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.
static int ENABLE_WRAP_AT_EOL_OUTPUT
          Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.
 
Constructor Summary
JoshOutBuffer(ConsoleHandle handle)
          Creates a new output buffer for direct manipulation of the output buffer.
 
Method Summary
 void fillConsoleOutputAttribute(ConsoleCoord coord, int length, ConsoleAttribute attribute)
          Sets the character attributes for a specified number of character cells, beginning at the specified coordinates in a screen buffer.
 void fillConsoleOutputCharacter(ConsoleCoord coord, int length, char character)
          Writes a character to the console screen buffer a specified number of times, beginning at the specified coordinates.
 ConsoleCursorInfo getConsoleCursorInfo()
          Retrieves information about the size and visibility of the cursor for the specified console screen buffer.
 ConsoleCoord getConsoleFontSize()
          XP Only: Retrieves the size of the font used by the specified console screen buffer.
 int getConsoleMode()
          Retrieves the current input mode of a console's input buffer or the current output mode of a console screen buffer.
 ConsoleScreenBufferInfo getConsoleScreenBufferInfo()
          Retrieves information about the specified console screen buffer.
 ConsoleFontInfo getCurrentConsoleFont(boolean forMaximumWindowSize)
          XP Only: Retrieves information about the current console font.
 ConsoleCoord getLargestConsoleWindowSize()
          Retrieves the size of the largest possible console window, based on the current font and the size of the display.
 ConsoleCharInfo[] readConsoleOutput(ConsoleCoord size, ConsoleCoord coord, ConsoleSmallRect readRegion)
          Reads character and color attribute data from a rectangular block of character cells in a console screen buffer, and the function writes the data to a rectangular block at a specified location in the destination buffer.
 ConsoleAttribute[] readConsoleOutputAttribute(ConsoleCoord coord, int length)
          Reads a specified number of character attributes from consecutive cells of a console screen buffer, beginning at a specified location.
 char[] readConsoleOutputChar(ConsoleCoord coord, int length)
          Reads a number of characters from consecutive cells of a console screen buffer, beginning at a specified location.
 void scrollConsoleScreenBuffer(ConsoleSmallRect block, ConsoleCoord destination)
          Moves a block of data in a screen buffer without a clip block or intersection fill.
 void scrollConsoleScreenBuffer(ConsoleSmallRect block, ConsoleCoord destination, ConsoleCharInfo fill)
          Moves a block of data in a screen buffer without a clip block.
 void scrollConsoleScreenBuffer(ConsoleSmallRect block, ConsoleSmallRect clip, ConsoleCoord destination, ConsoleCharInfo fill)
          Moves a block of data in a screen buffer.
 void setConsoleCursorInfo(ConsoleCursorInfo info)
          Sets the size and visibility of the cursor for the specified console screen buffer.
 void setConsoleCursorPosition(ConsoleCoord cursorPosition)
          Sets the cursor position in the specified console screen buffer.
 void setConsoleMode(int flags)
          Sets the input mode of a console's input buffer or the output mode of a console screen buffer.
 void setConsoleScreenBufferInfo(ConsoleScreenBufferInfo info)
          Vista Only: Sets extended information about the specified console screen buffer.
 void setConsoleTextAttribute(ConsoleAttribute attribute)
          Sets the attributes of characters written or echoed to the console screen buffer.
 void setConsoleWindowInfo(boolean absolute, ConsoleSmallRect newSize)
          Sets the current size and position of a console screen buffer's window.
 void setCurrentConsoleFont(int fontIndex)
          XP Only: Sets the font index.
 void setCurrentConsoleFontEx(ConsoleFontInfo font, boolean forMaximumWindowSize)
          Vista Only: Sets extended information about the current console font.
 void setFullScreen(boolean fullScreen)
          XP Only: Sets the display mode of the specified console screen buffer to fullscreen.
 void writeConsoleOutput(ConsoleCharInfo[] buffer, ConsoleCoord size, ConsoleCoord coord, ConsoleSmallRect writeRegion)
          Writes character and color attribute data to a specified rectangular block of character cells in a console screen buffer.
 void writeConsoleOutputAttribute(ConsoleAttribute[] attributes, ConsoleCoord coord, int length)
          Writes a number of character attributes to consecutive cells of a console screen buffer, beginning at a specified location.
 void writeConsoleOutputChar(char[] chars, ConsoleCoord coord, int length)
          Writes a number of characters to consecutive cells of a console screen buffer, beginning at a specified location.
 
Methods inherited from class com.yifanlu.Josh.JoshBuffer
equals, getHandle, hashCode, setHandle
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENABLE_ECHO_INPUT

public static final int ENABLE_ECHO_INPUT
Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.

See Also:
Constant Field Values

ENABLE_INSERT_MODE

public static final int ENABLE_INSERT_MODE
Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.

See Also:
Constant Field Values

ENABLE_LINE_INPUT

public static final int ENABLE_LINE_INPUT
Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.

See Also:
Constant Field Values

ENABLE_MOUSE_INPUT

public static final int ENABLE_MOUSE_INPUT
Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.

See Also:
Constant Field Values

ENABLE_PROCESSED_INPUT

public static final int ENABLE_PROCESSED_INPUT
Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.

See Also:
Constant Field Values

ENABLE_QUICK_EDIT_MODE

public static final int ENABLE_QUICK_EDIT_MODE
Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.

See Also:
Constant Field Values

ENABLE_WINDOW_INPUT

public static final int ENABLE_WINDOW_INPUT
Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.

See Also:
Constant Field Values

ENABLE_PROCESSED_OUTPUT

public static final int ENABLE_PROCESSED_OUTPUT
Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.

See Also:
Constant Field Values

ENABLE_WRAP_AT_EOL_OUTPUT

public static final int ENABLE_WRAP_AT_EOL_OUTPUT
Used in getConsoleMode() and setConsoleMode(int) to modify the console mode.

See Also:
Constant Field Values
Constructor Detail

JoshOutBuffer

public JoshOutBuffer(ConsoleHandle handle)
Creates a new output buffer for direct manipulation of the output buffer.

Parameters:
handle - A memory location that this buffer represents.
Method Detail

getConsoleCursorInfo

public ConsoleCursorInfo getConsoleCursorInfo()
Retrieves information about the size and visibility of the cursor for the specified console screen buffer.

Returns:
Information about the console's cursor.
See Also:
Windows' GetConsoleCursorInfo function

setConsoleCursorInfo

public void setConsoleCursorInfo(ConsoleCursorInfo info)
Sets the size and visibility of the cursor for the specified console screen buffer.

Parameters:
info - Provides the new specifications for the console screen buffer's cursor.
See Also:
Windows' SetConsoleCursorInfo function

setConsoleCursorPosition

public void setConsoleCursorPosition(ConsoleCoord cursorPosition)
Sets the cursor position in the specified console screen buffer.

Parameters:
cursorPosition - The new cursor position, in characters. The coordinates are the column and row of a screen buffer character cell. The coordinates must be within the boundaries of the console screen buffer.
See Also:
Windows' SetConsoleCursorPosition function

getConsoleFontSize

public ConsoleCoord getConsoleFontSize()
                                throws OSNotSupportedException
XP Only: Retrieves the size of the font used by the specified console screen buffer. This method only works with Windows XP or higher.

Returns:
The width and height of each character in the font, in logical units. The X member contains the width, while the Y member contains the height.
Throws:
OSNotSupportedException - If the current OS is not XP or higher.
See Also:
Windows' GetConsoleFontSize function

getConsoleMode

public int getConsoleMode()
Retrieves the current input mode of a console's input buffer or the current output mode of a console screen buffer. Use the constants defined in this class to check the console mode, mix and match with the OR operator. When a console is created, all input modes except ENABLE_WINDOW_INPUT are enabled by default.

Returns:
The mode of the console.
See Also:
Windows' GetConsoleMode function

setConsoleMode

public void setConsoleMode(int flags)
Sets the input mode of a console's input buffer or the output mode of a console screen buffer. When a console is created, all input modes except ENABLE_WINDOW_INPUT are enabled by default.

Parameters:
flags - Use the constants defined in this class to set the console mode, mix and match with the OR operator.
See Also:
Windows' SetConsoleMode function

getConsoleScreenBufferInfo

public ConsoleScreenBufferInfo getConsoleScreenBufferInfo()
Retrieves information about the specified console screen buffer.

Returns:
The console screen buffer information.
See Also:
Windows' GetConsoleScreenBufferInfo function

setConsoleScreenBufferInfo

public void setConsoleScreenBufferInfo(ConsoleScreenBufferInfo info)
                                throws OSNotSupportedException
Vista Only: Sets extended information about the specified console screen buffer. This method only works with Windows Vista or higher.

Parameters:
info - The console screen buffer information.
Throws:
OSNotSupportedException - If the current OS is not Vista or higher.
See Also:
Windows' SetConsoleScreenBufferInfoEx function

setConsoleTextAttribute

public void setConsoleTextAttribute(ConsoleAttribute attribute)
Sets the attributes of characters written or echoed to the console screen buffer. This function affects text written after the function call.

Parameters:
attribute - The character attributes.
See Also:
Windows' SetConsoleTextAttribute function

getCurrentConsoleFont

public ConsoleFontInfo getCurrentConsoleFont(boolean forMaximumWindowSize)
                                      throws OSNotSupportedException
XP Only: Retrieves information about the current console font. This method only works with Windows XP or higher.

Parameters:
forMaximumWindowSize - If this parameter is true, font information is retrieved for the maximum window size. If this parameter is false, font information is retrieved for the current window size.
Returns:
The requested font information.
Throws:
OSNotSupportedException - If the current OS is not XP or higher.
See Also:
Windows' GetCurrentConsoleFont function

setCurrentConsoleFontEx

public void setCurrentConsoleFontEx(ConsoleFontInfo font,
                                    boolean forMaximumWindowSize)
                             throws OSNotSupportedException
Vista Only: Sets extended information about the current console font. This method only works with Windows Vista or higher.

Parameters:
forMaximumWindowSize - If this parameter is true, font information is set for the maximum window size. If this parameter is false, font information is set for the current window size.
font - The font information.
Throws:
OSNotSupportedException - If the current OS is not Vista or higher.
See Also:
Windows' GetCurrentConsoleFont function

setCurrentConsoleFont

public void setCurrentConsoleFont(int fontIndex)
                           throws OSNotSupportedException
XP Only: Sets the font index. This method only works with Windows XP or higher.

Parameters:
fontIndex - The index of the font in the system's console font table.
Throws:
OSNotSupportedException - If the current OS is not XP or higher.

getLargestConsoleWindowSize

public ConsoleCoord getLargestConsoleWindowSize()
Retrieves the size of the largest possible console window, based on the current font and the size of the display.

Returns:
The number of character cell rows (X member) and columns (Y member) in the largest possible console window.
See Also:
Windows' GetLargestConsoleWindowSize function

scrollConsoleScreenBuffer

public void scrollConsoleScreenBuffer(ConsoleSmallRect block,
                                      ConsoleCoord destination)
Moves a block of data in a screen buffer without a clip block or intersection fill.

Parameters:
block - The upper-left and lower-right coordinates of the console screen buffer rectangle to be moved.
destination - The upper-left corner of the new location of the block contents, in characters.
See Also:
Windows' ScrollConsoleScreenBuffer function

scrollConsoleScreenBuffer

public void scrollConsoleScreenBuffer(ConsoleSmallRect block,
                                      ConsoleCoord destination,
                                      ConsoleCharInfo fill)
Moves a block of data in a screen buffer without a clip block.

Parameters:
block - The upper-left and lower-right coordinates of the console screen buffer rectangle to be moved.
destination - The upper-left corner of the new location of the block contents, in characters.
fill - The character and color attributes to be used in filling the cells within the intersection of block and clip that were left empty as a result of the move.
See Also:
Windows' ScrollConsoleScreenBuffer function

scrollConsoleScreenBuffer

public void scrollConsoleScreenBuffer(ConsoleSmallRect block,
                                      ConsoleSmallRect clip,
                                      ConsoleCoord destination,
                                      ConsoleCharInfo fill)
Moves a block of data in a screen buffer. The effects of the move can be limited by specifying a clipping rectangle, so the contents of the console screen buffer outside the clipping rectangle are unchanged.

Parameters:
block - The upper-left and lower-right coordinates of the console screen buffer rectangle to be moved.
clip - The upper-left and lower-right coordinates of the console screen buffer rectangle that is affected by the scrolling.
destination - The upper-left corner of the new location of the block contents, in characters.
fill - The character and color attributes to be used in filling the cells within the intersection of block and clip that were left empty as a result of the move.
See Also:
Windows' ScrollConsoleScreenBuffer function

setFullScreen

public void setFullScreen(boolean fullScreen)
                   throws OSNotSupportedException
XP Only: Sets the display mode of the specified console screen buffer to fullscreen. This method only works with Windows XP or higher.

Parameters:
fullScreen - Sets screen buffer to full screen if true, windowed mode if false.
Throws:
OSNotSupportedException - If the current OS is not XP or higher.
See Also:
Windows' SetConsoleDisplayMode function

setConsoleWindowInfo

public void setConsoleWindowInfo(boolean absolute,
                                 ConsoleSmallRect newSize)
Sets the current size and position of a console screen buffer's window.

Parameters:
absolute - If this parameter is true, the coordinates specify the new upper-left and lower-right corners of the window. If it is false, the coordinates are relative to the current window-corner coordinates.
newSize - Specifies the new upper-left and lower-right corners of the window.
See Also:
Windows' SetConsoleWindowInfo function

fillConsoleOutputAttribute

public void fillConsoleOutputAttribute(ConsoleCoord coord,
                                       int length,
                                       ConsoleAttribute attribute)
Sets the character attributes for a specified number of character cells, beginning at the specified coordinates in a screen buffer.

Parameters:
coord - The character coordinates of the first cell whose attributes are to be set.
length - The number of character cells to be set to the specified color attributes.
attribute - The attributes to use when writing to the console screen buffer.
See Also:
Windows' FillConsoleOutputAttribute function

fillConsoleOutputCharacter

public void fillConsoleOutputCharacter(ConsoleCoord coord,
                                       int length,
                                       char character)
Writes a character to the console screen buffer a specified number of times, beginning at the specified coordinates.

Parameters:
coord - The character coordinates of the first cell to which the character is to be written.
length - The number of character cells to which the character should be written.
character - The character to be written to the console screen buffer.
See Also:
Windows' FillConsoleOutputCharacter function

readConsoleOutput

public ConsoleCharInfo[] readConsoleOutput(ConsoleCoord size,
                                           ConsoleCoord coord,
                                           ConsoleSmallRect readRegion)
Reads character and color attribute data from a rectangular block of character cells in a console screen buffer, and the function writes the data to a rectangular block at a specified location in the destination buffer.

Parameters:
size - The size to read, in character cells. The X member is the number of columns; the Y member is the number of rows.
coord - The coordinates of the upper-left cell to start reading. The X member is the column, and the Y member is the row.
readRegion - The structure members specify the upper-left and lower-right coordinates of the console screen buffer rectangle from which the function is to read.
Returns:
An array that contains the character and it's attributes.
See Also:
Windows' ReadConsoleOutput function

writeConsoleOutput

public void writeConsoleOutput(ConsoleCharInfo[] buffer,
                               ConsoleCoord size,
                               ConsoleCoord coord,
                               ConsoleSmallRect writeRegion)
Writes character and color attribute data to a specified rectangular block of character cells in a console screen buffer. The data to be written is taken from a correspondingly sized rectangular block at a specified location in the source buffer.

Parameters:
buffer - The data to be written to the console screen buffer.
size - The size of the buffer, in character cells. The X member is the number of columns; the Y member is the number of rows.
coord - The coordinates of the upper-left cell in the buffer. The X member is the column, and the Y member is the row.
writeRegion - The upper-left and lower-right coordinates of the console screen buffer rectangle to write to.
See Also:
Windows' WriteConsoleOutput function

readConsoleOutputAttribute

public ConsoleAttribute[] readConsoleOutputAttribute(ConsoleCoord coord,
                                                     int length)
Reads a specified number of character attributes from consecutive cells of a console screen buffer, beginning at a specified location.

Parameters:
coord - The coordinates of the first cell in the console screen buffer from which to read, in characters. The X member is the column, and the Y member is the row.
length - The number of screen buffer character cells from which to read.
Returns:
An array of the attributes being used by the console screen buffer.
See Also:
Windows' ReadConsoleOutputAttribute function

writeConsoleOutputAttribute

public void writeConsoleOutputAttribute(ConsoleAttribute[] attributes,
                                        ConsoleCoord coord,
                                        int length)
Writes a number of character attributes to consecutive cells of a console screen buffer, beginning at a specified location.

Parameters:
attributes - An array that contains the character attributes.
coord - The character coordinates of the first cell in the console screen buffer to which the attributes will be written.
length - The number of screen buffer character cells to which the attributes will be copied.
See Also:
Windows' WriteConsoleOutputAttribute function

readConsoleOutputChar

public char[] readConsoleOutputChar(ConsoleCoord coord,
                                    int length)
Reads a number of characters from consecutive cells of a console screen buffer, beginning at a specified location.

Parameters:
coord - The coordinates of the first cell in the console screen buffer from which to read, in characters. The X member is the column, and the Y member is the row.
length - The number of screen buffer character cells from which to read.
Returns:
The characters read from the console screen buffer.
See Also:
Windows' ReadConsoleOutputCharacter function

writeConsoleOutputChar

public void writeConsoleOutputChar(char[] chars,
                                   ConsoleCoord coord,
                                   int length)
Writes a number of characters to consecutive cells of a console screen buffer, beginning at a specified location.

Parameters:
chars - The characters to write to the console screen buffer.
coord - The character coordinates of the first cell in the console screen buffer to which characters will be written.
See Also:
Windows' WriteConsoleOutputCharacter function

Copyright © 2010 Yifan Lu