From 5ac2049c23e4d091bf48a531b450adfdb2d4ef7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Boutemy?= Date: Sat, 2 Dec 2017 02:07:01 +0100 Subject: [PATCH] #95 add comments on expected diffs between Print and Output Streams --- .../org/fusesource/jansi/AnsiConsole.java | 1 + .../fusesource/jansi/AnsiOutputStream.java | 39 ++++++++++--------- .../org/fusesource/jansi/AnsiPrintStream.java | 39 ++++++++++--------- .../jansi/WindowsAnsiOutputStream.java | 15 +++---- .../jansi/WindowsAnsiPrintStream.java | 15 +++---- 5 files changed, 57 insertions(+), 52 deletions(-) diff --git a/jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java b/jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java index 5f720d0a..90537ded 100644 --- a/jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java +++ b/jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java @@ -177,6 +177,7 @@ public void close() throws IOException { * @param ps original PrintStream to wrap * @param fileno file descriptor * @return wrapped PrintStream depending on OS and system properties + * @since 1.17 */ public static PrintStream wrapPrintStream(final PrintStream ps, int fileno) { diff --git a/jansi/src/main/java/org/fusesource/jansi/AnsiOutputStream.java b/jansi/src/main/java/org/fusesource/jansi/AnsiOutputStream.java index 784297f7..7a4d3fe2 100644 --- a/jansi/src/main/java/org/fusesource/jansi/AnsiOutputStream.java +++ b/jansi/src/main/java/org/fusesource/jansi/AnsiOutputStream.java @@ -15,9 +15,9 @@ */ package org.fusesource.jansi; -import java.io.FilterOutputStream; +import java.io.FilterOutputStream; // expected diff with AnsiPrintStream.java import java.io.IOException; -import java.io.OutputStream; +import java.io.OutputStream; // expected diff with AnsiPrintStream.java import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Iterator; @@ -37,16 +37,17 @@ * @author Hiram Chirino * @author Joris Kuipers * @since 1.0 + * @see AnsiPrintStream */ -public class AnsiOutputStream extends FilterOutputStream { +public class AnsiOutputStream extends FilterOutputStream { // expected diff with AnsiPrintStream.java - public static final byte[] RESET_CODE = "\033[0m".getBytes(); + public static final byte[] RESET_CODE = "\033[0m".getBytes(); // expected diff with AnsiPrintStream.java @Deprecated - public static final byte[] REST_CODE = RESET_CODE; + public static final byte[] REST_CODE = RESET_CODE; // expected diff with AnsiPrintStream.java - public AnsiOutputStream(OutputStream os) { - super(os); + public AnsiOutputStream(OutputStream os) { // expected diff with AnsiPrintStream.java + super(os); // expected diff with AnsiPrintStream.java } private final static int MAX_ESCAPE_SEQUENCE_LENGTH = 100; @@ -77,16 +78,16 @@ public AnsiOutputStream(OutputStream os) { private static final int SECOND_CHARSET1_CHAR = ')'; @Override - public synchronized void write(int data) throws IOException { + public synchronized void write(int data) throws IOException { // expected diff with AnsiPrintStream.java switch (state) { case LOOKING_FOR_FIRST_ESC_CHAR: if (data == FIRST_ESC_CHAR) { buffer[pos++] = (byte) data; state = LOOKING_FOR_SECOND_ESC_CHAR; - } else { - out.write(data); + } else { // expected diff with AnsiPrintStream.java + out.write(data); // expected diff with AnsiPrintStream.java } - break; + break; // expected diff with AnsiPrintStream.java case LOOKING_FOR_SECOND_ESC_CHAR: buffer[pos++] = (byte) data; @@ -220,9 +221,9 @@ public synchronized void write(int data) throws IOException { * @param skipBuffer if current buffer should be skipped or written to out * @throws IOException */ - private void reset(boolean skipBuffer) throws IOException { + private void reset(boolean skipBuffer) throws IOException { // expected diff with AnsiPrintStream.java if (!skipBuffer) { - out.write(buffer, 0, pos); + out.write(buffer, 0, pos); // expected diff with AnsiPrintStream.java } pos = 0; startOfValue = 0; @@ -251,7 +252,7 @@ private int getNextOptionInt(Iterator optionsIterator) throws IOExceptio * @param command * @return true if the escape command was processed. */ - private boolean processEscapeCommand(ArrayList options, int command) throws IOException { + private boolean processEscapeCommand(ArrayList options, int command) throws IOException { // expected diff with AnsiPrintStream.java try { switch (command) { case 'A': @@ -401,7 +402,7 @@ else if (arg2or5 == 5) { * @param options * @return true if the operating system command was processed. */ - private boolean processOperatingSystemCommand(ArrayList options) throws IOException { + private boolean processOperatingSystemCommand(ArrayList options) throws IOException { // expected diff with AnsiPrintStream.java int command = optionInt(options, 0); String label = (String) options.get(1); // for command > 2 label could be composed (i.e. contain ';'), but we'll leave @@ -670,7 +671,7 @@ protected void processCursorUpLine(int count) throws IOException { protected void processCursorDownLine(int count) throws IOException { // Poor mans impl.. for (int i = 0; i < count; i++) { - out.write('\n'); + out.write('\n'); // expected diff with AnsiPrintStream.java } } @@ -690,7 +691,7 @@ protected void processCursorLeft(int count) throws IOException { protected void processCursorRight(int count) throws IOException { // Poor mans impl.. for (int i = 0; i < count; i++) { - out.write(' '); + out.write(' '); // expected diff with AnsiPrintStream.java } } @@ -785,8 +786,8 @@ private int optionInt(ArrayList options, int index, int defaultValue) { } @Override - public void close() throws IOException { - write(RESET_CODE); + public void close() throws IOException { // expected diff with AnsiPrintStream.java + write(RESET_CODE); // expected diff with AnsiPrintStream.java flush(); super.close(); } diff --git a/jansi/src/main/java/org/fusesource/jansi/AnsiPrintStream.java b/jansi/src/main/java/org/fusesource/jansi/AnsiPrintStream.java index 0d1b7f45..d344f08a 100644 --- a/jansi/src/main/java/org/fusesource/jansi/AnsiPrintStream.java +++ b/jansi/src/main/java/org/fusesource/jansi/AnsiPrintStream.java @@ -16,7 +16,7 @@ package org.fusesource.jansi; import java.io.IOException; -import java.io.PrintStream; +import java.io.PrintStream; // expected diff with AnsiOutputStream.java import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Iterator; @@ -36,13 +36,14 @@ * @author Hiram Chirino * @author Joris Kuipers * @since 1.7 + * @see AnsiOutputStream */ -public class AnsiPrintStream extends FilterPrintStream { +public class AnsiPrintStream extends FilterPrintStream { // expected diff with AnsiOutputStream.java - public static final String RESET_CODE = "\033[0m"; + public static final String RESET_CODE = "\033[0m"; // expected diff with AnsiOutputStream.java - public AnsiPrintStream(PrintStream ps) { - super(ps); + public AnsiPrintStream(PrintStream ps) { // expected diff with AnsiOutputStream.java + super(ps); // expected diff with AnsiOutputStream.java } private final static int MAX_ESCAPE_SEQUENCE_LENGTH = 100; @@ -70,15 +71,15 @@ public AnsiPrintStream(PrintStream ps) { private static final int SECOND_ST_CHAR = '\\'; @Override - protected boolean filter(int data) { + protected boolean filter(int data) { // expected diff with AnsiOutputStream.java switch (state) { case LOOKING_FOR_FIRST_ESC_CHAR: if (data == FIRST_ESC_CHAR) { buffer[pos++] = (byte) data; state = LOOKING_FOR_SECOND_ESC_CHAR; - return false; + return false; // expected diff with AnsiOutputStream.java } - return true; + return true; // expected diff with AnsiOutputStream.java case LOOKING_FOR_SECOND_ESC_CHAR: buffer[pos++] = (byte) data; @@ -194,16 +195,16 @@ protected boolean filter(int data) { if (pos >= buffer.length) { reset(false); } - return false; + return false; // expected diff with AnsiOutputStream.java } /** * Resets all state to continue with regular parsing * @param skipBuffer if current buffer should be skipped or written to out */ - private void reset(boolean skipBuffer) { + private void reset(boolean skipBuffer) { // expected diff with AnsiOutputStream.java if (!skipBuffer) { - ps.write(buffer, 0, pos); + ps.write(buffer, 0, pos); // expected diff with AnsiOutputStream.java } pos = 0; startOfValue = 0; @@ -232,7 +233,7 @@ private int getNextOptionInt(Iterator optionsIterator) throws IOExceptio * @param command * @return true if the escape command was processed. */ - private boolean processEscapeCommand(ArrayList options, int command) { + private boolean processEscapeCommand(ArrayList options, int command) { // expected diff with AnsiOutputStream.java try { switch (command) { case 'A': @@ -373,8 +374,8 @@ else if (arg2or5 == 5) { return false; } } catch (IllegalArgumentException ignore) { - } catch (IOException ioe) { - setError(); + } catch (IOException ioe) { // expected diff with AnsiOutputStream.java + setError(); // expected diff with AnsiOutputStream.java } return false; } @@ -384,7 +385,7 @@ else if (arg2or5 == 5) { * @param options * @return true if the operating system command was processed. */ - private boolean processOperatingSystemCommand(ArrayList options) { + private boolean processOperatingSystemCommand(ArrayList options) { // expected diff with AnsiOutputStream.java int command = optionInt(options, 0); String label = (String) options.get(1); // for command > 2 label could be composed (i.e. contain ';'), but we'll leave @@ -651,7 +652,7 @@ protected void processCursorUpLine(int count) throws IOException { protected void processCursorDownLine(int count) throws IOException { // Poor mans impl.. for (int i = 0; i < count; i++) { - print('\n'); + print('\n'); // expected diff with AnsiOutputStream.java } } @@ -671,7 +672,7 @@ protected void processCursorLeft(int count) throws IOException { protected void processCursorRight(int count) throws IOException { // Poor mans impl.. for (int i = 0; i < count; i++) { - print(' '); + print(' '); // expected diff with AnsiOutputStream.java } } @@ -748,8 +749,8 @@ private int optionInt(ArrayList options, int index, int defaultValue) { } @Override - public void close() { - ps.print(RESET_CODE); + public void close() { // expected diff with AnsiOutputStream.java + ps.print(RESET_CODE); // expected diff with AnsiOutputStream.java flush(); super.close(); } diff --git a/jansi/src/main/java/org/fusesource/jansi/WindowsAnsiOutputStream.java b/jansi/src/main/java/org/fusesource/jansi/WindowsAnsiOutputStream.java index 42df829f..fe96e8ff 100644 --- a/jansi/src/main/java/org/fusesource/jansi/WindowsAnsiOutputStream.java +++ b/jansi/src/main/java/org/fusesource/jansi/WindowsAnsiOutputStream.java @@ -36,7 +36,7 @@ import static org.fusesource.jansi.internal.Kernel32.SetConsoleTitle; import java.io.IOException; -import java.io.OutputStream; +import java.io.OutputStream; // expected diff with WindowsAnsiPrintStream.java import org.fusesource.jansi.internal.WindowsSupport; import org.fusesource.jansi.internal.Kernel32.CONSOLE_SCREEN_BUFFER_INFO; @@ -49,8 +49,9 @@ * @since 1.0 * @author Hiram Chirino * @author Joris Kuipers + * @see WindowsAnsiPrintStream */ -public final class WindowsAnsiOutputStream extends AnsiOutputStream { +public final class WindowsAnsiOutputStream extends AnsiOutputStream { // expected diff with WindowsAnsiPrintStream.java private static final long console = GetStdHandle(STD_OUTPUT_HANDLE); @@ -95,14 +96,14 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream { private short savedX = -1; private short savedY = -1; - public WindowsAnsiOutputStream(OutputStream os) throws IOException { - super(os); + public WindowsAnsiOutputStream(OutputStream os) throws IOException { // expected diff with WindowsAnsiPrintStream.java + super(os); // expected diff with WindowsAnsiPrintStream.java getConsoleInfo(); originalColors = info.attributes; } private void getConsoleInfo() throws IOException { - out.flush(); + out.flush(); // expected diff with WindowsAnsiPrintStream.java if (GetConsoleScreenBufferInfo(console, info) == 0) { throw new IOException("Could not get the screen info: " + WindowsSupport.getLastErrorMessage()); } @@ -112,7 +113,7 @@ private void getConsoleInfo() throws IOException { } private void applyAttribute() throws IOException { - out.flush(); + out.flush(); // expected diff with WindowsAnsiPrintStream.java short attributes = info.attributes; if (negative) { attributes = invertAttributeColors(attributes); @@ -327,7 +328,7 @@ protected void processSaveCursorPosition() throws IOException { protected void processRestoreCursorPosition() throws IOException { // restore only if there was a save operation first if (savedX != -1 && savedY != -1) { - out.flush(); + out.flush(); // expected diff with WindowsAnsiPrintStream.java info.cursorPosition.x = savedX; info.cursorPosition.y = savedY; applyCursorPosition(); diff --git a/jansi/src/main/java/org/fusesource/jansi/WindowsAnsiPrintStream.java b/jansi/src/main/java/org/fusesource/jansi/WindowsAnsiPrintStream.java index 84c0dff2..a50b320d 100644 --- a/jansi/src/main/java/org/fusesource/jansi/WindowsAnsiPrintStream.java +++ b/jansi/src/main/java/org/fusesource/jansi/WindowsAnsiPrintStream.java @@ -36,7 +36,7 @@ import static org.fusesource.jansi.internal.Kernel32.SetConsoleTitle; import java.io.IOException; -import java.io.PrintStream; +import java.io.PrintStream; // expected diff with WindowsAnsiOutputStream.java import org.fusesource.jansi.internal.WindowsSupport; import org.fusesource.jansi.internal.Kernel32.CONSOLE_SCREEN_BUFFER_INFO; @@ -49,8 +49,9 @@ * @since 1.7 * @author Hiram Chirino * @author Joris Kuipers + * @see WindowsAnsiOutputStream */ -public final class WindowsAnsiPrintStream extends AnsiPrintStream { +public final class WindowsAnsiPrintStream extends AnsiPrintStream { // expected diff with WindowsAnsiOutputStream.java private static final long console = GetStdHandle(STD_OUTPUT_HANDLE); @@ -95,14 +96,14 @@ public final class WindowsAnsiPrintStream extends AnsiPrintStream { private short savedX = -1; private short savedY = -1; - public WindowsAnsiPrintStream(PrintStream ps) throws IOException { - super(ps); + public WindowsAnsiPrintStream(PrintStream ps) throws IOException { // expected diff with WindowsAnsiOutputStream.java + super(ps); // expected diff with WindowsAnsiOutputStream.java getConsoleInfo(); originalColors = info.attributes; } private void getConsoleInfo() throws IOException { - ps.flush(); + ps.flush(); // expected diff with WindowsAnsiOutputStream.java if (GetConsoleScreenBufferInfo(console, info) == 0) { throw new IOException("Could not get the screen info: " + WindowsSupport.getLastErrorMessage()); } @@ -112,7 +113,7 @@ private void getConsoleInfo() throws IOException { } private void applyAttribute() throws IOException { - ps.flush(); + ps.flush(); // expected diff with WindowsAnsiOutputStream.java short attributes = info.attributes; if (negative) { attributes = invertAttributeColors(attributes); @@ -323,7 +324,7 @@ protected void processSaveCursorPosition() throws IOException { protected void processRestoreCursorPosition() throws IOException { // restore only if there was a save operation first if (savedX != -1 && savedY != -1) { - ps.flush(); + ps.flush(); // expected diff with WindowsAnsiOutputStream.java info.cursorPosition.x = savedX; info.cursorPosition.y = savedY; applyCursorPosition();