// Path: src/main/java/io/termd/core/io/TelnetCharset.java
// public class TelnetCharset extends Charset {
// 
//   public static final Charset INSTANCE = new TelnetCharset();
// 
//   private TelnetCharset() {
//     super("Telnet", new String[0]);
//   }
// 
//   @Override
//   public boolean contains(Charset cs) {
//     return cs.name().equals(name());
//   }
// 
//   @Override
//   public CharsetDecoder newDecoder() {
//     return new CharsetDecoder(this, 1.0f, 1.0f) {
//       private boolean prevCR;
//       @Override
//       protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
//         int pos = in.position();
//         int limit = in.limit();
//         try {
//           while (pos < limit) {
//             byte b = in.get(pos);
//             char c;
//             if (b >= 0) {
//               if (prevCR && (b == '\n' || b == 0)) {
//                 pos++;
//                 prevCR = false;
//                 continue;
//               }
//               c = (char) b;
//               prevCR = b == '\r';
//             } else {
//               c = (char)(256 + b);
//             }
//             if (out.position() >= out.limit()) {
//               return CoderResult.OVERFLOW;
//             }
//             pos++;
//             out.put(c);
//           }
//           return CoderResult.UNDERFLOW;
//         } finally {
//           in.position(pos);
//         }
//       }
//     };
//   }
// 
//   @Override
//   public CharsetEncoder newEncoder() {
//     throw new UnsupportedOperationException();
//   }
// }


import java.util.function.Consumer;
import io.termd.core.tty.ReadBuffer;
import io.termd.core.tty.TtyEvent;
import io.termd.core.tty.TtyEventDecoder;
import io.termd.core.tty.TtyOutputMode;
import io.termd.core.util.Vector;
import io.termd.core.io.BinaryDecoder;
import io.termd.core.io.BinaryEncoder;
import io.termd.core.io.TelnetCharset;
import io.termd.core.tty.TtyConnection;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
/*
 * Copyright 2015 Julien Viet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.termd.core.telnet;



/**
 * A telnet handler that implements {@link io.termd.core.tty.TtyConnection}.
 *
 * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
 */
public final class TelnetTtyConnection extends TelnetHandler implements TtyConnection {

  private final boolean inBinary;
  private final boolean outBinary;
  private boolean receivingBinary;
  private boolean sendingBinary;
  private boolean accepted;
  private Vector size;
  private String terminalType;
  private Consumer<Vector> sizeHandler;
  private Consumer<String> termHandler;
  private Consumer<Void> closeHandler;
  protected TelnetConnection conn;
  private final Charset charset;
  private final TtyEventDecoder eventDecoder = new TtyEventDecoder(3, 26, 4);
  private final ReadBuffer readBuffer = new ReadBuffer(this::execute);
// Path: src/main/java/io/termd/core/io/TelnetCharset.java
// public class TelnetCharset extends Charset {
// 
//   public static final Charset INSTANCE = new TelnetCharset();
// 
//   private TelnetCharset() {
//     super("Telnet", new String[0]);
//   }
// 
//   @Override
//   public boolean contains(Charset cs) {
//     return cs.name().equals(name());
//   }
// 
//   @Override
//   public CharsetDecoder newDecoder() {
//     return new CharsetDecoder(this, 1.0f, 1.0f) {
//       private boolean prevCR;
//       @Override
//       protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
//         int pos = in.position();
//         int limit = in.limit();
//         try {
//           while (pos < limit) {
//             byte b = in.get(pos);
//             char c;
//             if (b >= 0) {
//               if (prevCR && (b == '\n' || b == 0)) {
//                 pos++;
//                 prevCR = false;
//                 continue;
//               }
//               c = (char) b;
//               prevCR = b == '\r';
//             } else {
//               c = (char)(256 + b);
//             }
//             if (out.position() >= out.limit()) {
//               return CoderResult.OVERFLOW;
//             }
//             pos++;
//             out.put(c);
//           }
//           return CoderResult.UNDERFLOW;
//         } finally {
//           in.position(pos);
//         }
//       }
//     };
//   }
// 
//   @Override
//   public CharsetEncoder newEncoder() {
//     throw new UnsupportedOperationException();
//   }
// }


// Path: src/main/java/io/termd/core/telnet/TelnetTtyConnection.java
import java.util.function.Consumer;
import io.termd.core.tty.ReadBuffer;
import io.termd.core.tty.TtyEvent;
import io.termd.core.tty.TtyEventDecoder;
import io.termd.core.tty.TtyOutputMode;
import io.termd.core.util.Vector;
import io.termd.core.io.BinaryDecoder;
import io.termd.core.io.BinaryEncoder;
import io.termd.core.io.TelnetCharset;
import io.termd.core.tty.TtyConnection;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

/*
 * Copyright 2015 Julien Viet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.termd.core.telnet;



/**
 * A telnet handler that implements {@link io.termd.core.tty.TtyConnection}.
 *
 * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
 */
public final class TelnetTtyConnection extends TelnetHandler implements TtyConnection {

  private final boolean inBinary;
  private final boolean outBinary;
  private boolean receivingBinary;
  private boolean sendingBinary;
  private boolean accepted;
  private Vector size;
  private String terminalType;
  private Consumer<Vector> sizeHandler;
  private Consumer<String> termHandler;
  private Consumer<Void> closeHandler;
  protected TelnetConnection conn;
  private final Charset charset;
  private final TtyEventDecoder eventDecoder = new TtyEventDecoder(3, 26, 4);
  private final ReadBuffer readBuffer = new ReadBuffer(this::execute);
  private final BinaryDecoder decoder = new BinaryDecoder(512, TelnetCharset.INSTANCE, readBuffer);