// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 30.05.2007 00:10:07
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   Network.java

package Network;

import java.io.*;
import java.net.Socket;

// Referenced classes of package Network:
//            NetworkListener, J, F

public class Network
    implements Runnable
{

    public void disconnect()
    {
        try
        {
            connected = false;
            nl.connectionLost(this);
            outStream.close();
            is.close();
        }
        catch(Exception exception) { }
    }

    public void setVerbose(boolean b)
    {
        verbose = b;
    }

    public Network(NetworkListener nl)
    {
        code = new J();
        connected = false;
        verbose = true;
        f = new F();
        this.nl = nl;
        try
        {
            Socket socket = new Socket("h-213.61.5.150.host.de.colt.net", 2710);
            is = socket.getInputStream();
            outStream = socket.getOutputStream();
            (new Thread(this)).start();
            sendString("t\000V8.6\000 \0000", 1);
            connected = true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            connected = false;
            nl.connectionLost(this);
        }
    }

    public void run()
    {
        while(connected) 
        {
            String s1 = f.I(destroy(is));
            nl.stringRecieved(this, s1);
        }
    }

    private byte[] destroy(InputStream inputstream)
    {
        try
        {
            int i;
            while(i > 0x19000) 
            {
                i = 0;
                byte abyte0[] = new byte[5];
                abyte0[0] = (byte)inputstream.read();
                if(abyte0[0] >= 0)
                {
                    i = abyte0[0] + 1;
                } else
                {
                    i = (abyte0[0] & 0x1f) + 1;
                    int j = ((abyte0[0] & 0x60) >>> 5) + 1;
                    for(int k = 1; k < j; k++)
                        i += inputstream.read() << (k - 1) * 8 + 5;

                }
            }
            byte abyte1[] = new byte[i];
            for(; i > 0; i--)
                abyte1[abyte1.length - i] = (byte)inputstream.read();

            return abyte1;
        }
        catch(Throwable t)
        {
            connected = false;
        }
        nl.connectionLost(this);
        return new byte[2];
    }

    public void sendString(String string, int i)
    {
        if(verbose)
            nl.stringSend(this, string);
        try
        {
            byte abyte0[] = code.I(string);
            byte abyte1[] = currentTimeMillis(abyte0.length);
            byte abyte2[] = new byte[abyte1.length + abyte0.length + i];
            System.arraycopy(abyte1, 0, abyte2, i, abyte1.length);
            System.arraycopy(abyte0, 0, abyte2, abyte1.length + i, abyte0.length);
            outStream.write(abyte2);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    private byte[] currentTimeMillis(int i)
        throws IOException
    {
        if(i > 0x19000)
            throw new IOException("Too long Msg: " + i);
        byte abyte0[] = (byte[])null;
        if(--i >= 0)
            if(i < 128)
            {
                abyte0 = new byte[1];
                abyte0[0] = (byte)i;
            } else
            {
                byte byte0;
                for(byte0 = 0; 32 << (byte0 + 1) * 8 <= i; byte0++);
                abyte0 = new byte[byte0 + 2];
                abyte0[0] = (byte)(byte0++ << 5 | 0x80 | i & 0x1f);
                for(int j = 1; j < abyte0.length; j++)
                    abyte0[j] = (byte)(i >>> 8 * (j - 1) + 5);

            }
        return abyte0;
    }

    BufferedReader breader;
    OutputStream outStream;
    InputStream is;
    PrintWriter pwriter;
    J code;
    NetworkListener nl;
    public boolean connected;
    boolean verbose;
    F f;
}