Skip to content

Commit

Permalink
Extract common parts to AbstractWindowsConsoleWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
stephan-gh committed Sep 14, 2017
1 parent 96c5e0f commit 4344091
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,23 @@
package org.jline.terminal.impl.jansi.win;

import org.fusesource.jansi.internal.WindowsSupport;
import org.jline.terminal.impl.AbstractWindowsConsoleWriter;

import java.io.IOException;
import java.io.Writer;

import static org.fusesource.jansi.internal.Kernel32.GetStdHandle;
import static org.fusesource.jansi.internal.Kernel32.STD_OUTPUT_HANDLE;
import static org.fusesource.jansi.internal.Kernel32.WriteConsoleW;

class JansiWinConsoleWriter extends Writer {
class JansiWinConsoleWriter extends AbstractWindowsConsoleWriter {

private static final long console = GetStdHandle(STD_OUTPUT_HANDLE);

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
char[] text = cbuf;
if (off != 0) {
text = new char[len];
System.arraycopy(cbuf, off, text, 0, len);
}

protected void writeConsole(char[] text, int len) throws IOException {
if (WriteConsoleW(console, text, len, new int[1], 0) == 0) {
throw new IOException("Failed to write to console: " + WindowsSupport.getLastErrorMessage());
}
}

@Override
public void flush() throws IOException {
}

@Override
public void close() throws IOException {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import com.sun.jna.LastErrorException;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import org.jline.terminal.impl.AbstractWindowsConsoleWriter;

import java.io.IOException;
import java.io.Writer;

class JnaWinConsoleWriter extends Writer {
class JnaWinConsoleWriter extends AbstractWindowsConsoleWriter {

private final Pointer consoleHandle;

Expand All @@ -24,26 +24,12 @@ class JnaWinConsoleWriter extends Writer {
}

@Override
public synchronized void write(char[] cbuf, int off, int len) throws IOException {
char[] text = cbuf;
if (off != 0) {
text = new char[len];
System.arraycopy(cbuf, off, text, 0, len);
}

protected void writeConsole(char[] text, int len) throws IOException {
try {
Kernel32.INSTANCE.WriteConsoleW(this.consoleHandle, text, len, new IntByReference(), null);
} catch (LastErrorException e) {
throw new IOException("Failed to write to console", e);
}
}

@Override
public void flush() {
}

@Override
public void close() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2002-2017, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* http://www.opensource.org/licenses/bsd-license.php
*/
package org.jline.terminal.impl;

import java.io.IOException;
import java.io.Writer;

public abstract class AbstractWindowsConsoleWriter extends Writer {

protected abstract void writeConsole(char[] text, int len) throws IOException;

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
char[] text = cbuf;
if (off != 0) {
text = new char[len];
System.arraycopy(cbuf, off, text, 0, len);
}

synchronized (this.lock) {
writeConsole(text, len);
}
}

@Override
public void flush() {
}

@Override
public void close() {
}

}

0 comments on commit 4344091

Please sign in to comment.