// Path: src/main/java/me/legrange/mikrotik/ApiConnectionException.java
// public class ApiConnectionException extends MikrotikApiException {
// 
//     /** 
//      * Create a new exception. 
//      * 
//      * @param msg The message
//      */
//     public ApiConnectionException(String msg) {
//         super(msg);
//     }
// 
//     /** 
//      * Create a new exception 
//      * @param msg The message
//      * @param err The underlying cause 
//      */
//     public ApiConnectionException(String msg, Throwable err) {
//         super(msg, err);
//     }
//     
//     
//     
// }


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import me.legrange.mikrotik.ApiConnectionException;
package me.legrange.mikrotik.impl;


/**
 * Utility library that handles the low level encoding required by the Mikrotik
 * API.
 *
 * @author GideonLeGrange. Possibly some code by janisk left.
 */
final class Util {

    /**
     * write a command to the output stream
     */
    static void write(Command cmd, OutputStream out) throws UnsupportedEncodingException, IOException {
        encode(cmd.getCommand(), out);
        for (Parameter param : cmd.getParameters()) {
            encode(String.format("=%s=%s", param.getName(), param.hasValue() ? param.getValue() : ""), out);
        }
        String tag = cmd.getTag();
        if ((tag != null) && !tag.equals("")) {
            encode(String.format(".tag=%s", tag), out);
        }
        List<String> props = cmd.getProperties();
        if (!props.isEmpty()) {
            StringBuilder buf = new StringBuilder("=.proplist=");
            for (int i = 0; i < props.size(); ++i) {
                if (i > 0) {
                    buf.append(",");
                }
                buf.append(props.get(i));
            }
            encode(buf.toString(), out);
        }
        for (String query : cmd.getQueries()) {
            encode(query, out);
        }
        out.write(0);
    }

    /**
     * decode bytes from an input stream of Mikrotik protocol sentences into
     * text
     */
// Path: src/main/java/me/legrange/mikrotik/ApiConnectionException.java
// public class ApiConnectionException extends MikrotikApiException {
// 
//     /** 
//      * Create a new exception. 
//      * 
//      * @param msg The message
//      */
//     public ApiConnectionException(String msg) {
//         super(msg);
//     }
// 
//     /** 
//      * Create a new exception 
//      * @param msg The message
//      * @param err The underlying cause 
//      */
//     public ApiConnectionException(String msg, Throwable err) {
//         super(msg, err);
//     }
//     
//     
//     
// }


// Path: src/main/java/me/legrange/mikrotik/impl/Util.java
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import me.legrange.mikrotik.ApiConnectionException;

package me.legrange.mikrotik.impl;


/**
 * Utility library that handles the low level encoding required by the Mikrotik
 * API.
 *
 * @author GideonLeGrange. Possibly some code by janisk left.
 */
final class Util {

    /**
     * write a command to the output stream
     */
    static void write(Command cmd, OutputStream out) throws UnsupportedEncodingException, IOException {
        encode(cmd.getCommand(), out);
        for (Parameter param : cmd.getParameters()) {
            encode(String.format("=%s=%s", param.getName(), param.hasValue() ? param.getValue() : ""), out);
        }
        String tag = cmd.getTag();
        if ((tag != null) && !tag.equals("")) {
            encode(String.format(".tag=%s", tag), out);
        }
        List<String> props = cmd.getProperties();
        if (!props.isEmpty()) {
            StringBuilder buf = new StringBuilder("=.proplist=");
            for (int i = 0; i < props.size(); ++i) {
                if (i > 0) {
                    buf.append(",");
                }
                buf.append(props.get(i));
            }
            encode(buf.toString(), out);
        }
        for (String query : cmd.getQueries()) {
            encode(query, out);
        }
        out.write(0);
    }

    /**
     * decode bytes from an input stream of Mikrotik protocol sentences into
     * text
     */
    static String decode(InputStream in) throws ApiDataException, ApiConnectionException {