telnet support 75%
[nikiroo-utils.git] / src / jexer / net / TelnetOutputStream.java
index b31cb03972a071cadda0b461b45ebf24a7a10f2b..98727277a8b47e6a5397d3ac3e77e131424edce8 100644 (file)
@@ -67,7 +67,10 @@ public final class TelnetOutputStream extends OutputStream {
      */
     @Override
     public void close() throws IOException {
-        // TODO
+        if (output != null) {
+            output.close();
+            output = null;
+        }
     }
 
     /**
@@ -76,6 +79,15 @@ public final class TelnetOutputStream extends OutputStream {
      */
     @Override
     public void flush() throws IOException {
+        if ((master.nvt.binaryMode == false) && (master.nvt.writeCR == true)) {
+            // The last byte sent to this.write() was a CR, which was never
+            // actually sent.  So send the CR in ascii mode, then flush.
+            // CR <anything> -> CR NULL
+            output.write(master.C_CR);
+            output.write(master.C_NUL);
+            master.nvt.writeCR = false;
+        }
+        output.flush();
     }
 
     /**
@@ -84,7 +96,7 @@ public final class TelnetOutputStream extends OutputStream {
      */
     @Override
     public void write(byte[] b) throws IOException {
-        // TODO
+        writeImpl(b, 0, b.length);
     }
 
     /**
@@ -93,7 +105,7 @@ public final class TelnetOutputStream extends OutputStream {
      */
     @Override
     public void write(byte[] b, int off, int len) throws IOException {
-        // TODO
+        writeImpl(b, off, len);
     }
 
     /**
@@ -101,7 +113,105 @@ public final class TelnetOutputStream extends OutputStream {
      */
     @Override
     public void write(int b) throws IOException {
-        // TODO
+        byte [] bytes = new byte[1];
+        bytes[0] = (byte)b;
+        writeImpl(bytes, 0, 1);
     }
 
+    /**
+     * Writes b.length bytes from the specified byte array to this output
+     * stream.  Note package private access.
+     */
+    void rawWrite(byte[] b) throws IOException {
+        output.write(b, 0, b.length);
+    }
+
+    /**
+     * Writes len bytes from the specified byte array starting at offset off
+     * to this output stream.
+     */
+    private void writeImpl(final byte[] b, final int off,
+        final int len) throws IOException {
+
+        byte [] writeBuffer = new byte[Math.max(len, 4)];
+        int writeBufferI = 0;
+
+        for (int i = 0; i < len; i++) {
+            if (writeBufferI >= writeBuffer.length - 4) {
+                // Flush what we have generated so far and reset the buffer,
+                // because the next byte could generate up to 4 output bytes
+                // (CR <something> <IAC> <IAC>).
+                super.write(writeBuffer, 0, writeBufferI);
+                writeBufferI = 0;
+            }
+
+            // Pull the next byte
+            byte ch = b[i + off];
+
+            if (master.nvt.binaryMode == true) {
+
+                if (ch == master.TELNET_IAC) {
+                    // IAC -> IAC IAC
+                    writeBuffer[writeBufferI++] = (byte)master.TELNET_IAC;
+                    writeBuffer[writeBufferI++] = (byte)master.TELNET_IAC;
+                } else {
+                    // Anything else -> just send
+                    writeBuffer[writeBufferI++] = ch;
+                }
+                continue;
+            }
+
+            // Non-binary mode: more complicated.  We use writeCR to handle
+            // the case that the last byte of b was a CR.
+
+            // Bare carriage return -> CR NUL
+            if (ch == master.C_CR) {
+                if (master.nvt.writeCR == true) {
+                    // Flush the previous CR to the stream.
+                    // CR <anything> -> CR NULL
+                    writeBuffer[writeBufferI++] = (byte)master.C_CR;
+                    writeBuffer[writeBufferI++] = (byte)master.C_NUL;
+                }
+                master.nvt.writeCR = true;
+            } else if (ch == master.C_LF) {
+                if (master.nvt.writeCR == true) {
+                    // CR LF -> CR LF
+                    writeBuffer[writeBufferI++] = (byte)master.C_CR;
+                    writeBuffer[writeBufferI++] = (byte)master.C_LF;
+                    master.nvt.writeCR = false;
+                } else {
+                    // Bare LF -> LF
+                    writeBuffer[writeBufferI++] = ch;
+                }
+            } else if (ch == master.TELNET_IAC) {
+                if (master.nvt.writeCR == true) {
+                    // CR <anything> -> CR NULL
+                    writeBuffer[writeBufferI++] = (byte)master.C_CR;
+                    writeBuffer[writeBufferI++] = (byte)master.C_NUL;
+                    master.nvt.writeCR = false;
+                }
+                // IAC -> IAC IAC
+                writeBuffer[writeBufferI++] = (byte)master.TELNET_IAC;
+                writeBuffer[writeBufferI++] = (byte)master.TELNET_IAC;
+            } else {
+                if (master.nvt.writeCR == true) {
+                    // CR <anything> -> CR NULL
+                    writeBuffer[writeBufferI++] = (byte)master.C_CR;
+                    writeBuffer[writeBufferI++] = (byte)master.C_NUL;
+                    master.nvt.writeCR = false;
+                } else {
+                    // Normal character */
+                    writeBuffer[writeBufferI++] = ch;
+                }
+            }
+
+        } // while (i < userbuf.length)
+
+        if (writeBufferI > 0) {
+            // Flush what we have generated so far and reset the buffer.
+            super.write(writeBuffer, 0, writeBufferI);
+        }
+    }
+
+
 }