// Decompiled by DJ v2.6.6.51 Copyright 2000 Atanas Neshkov  Date: 10/2/2001 11:02:32
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   com/macromedia/generator/protocol/fgftp/FgftpURLConnection

package com.macromedia.generator.protocol.fgftp;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.net.UnknownHostException;
import sun.net.ProgressData;
import sun.net.ProgressEntry;
import sun.net.TransferProtocolClient;
import sun.net.ftp.FtpClient;
import sun.net.www.MessageHeader;
import sun.net.www.MeteredStream;
import sun.net.www.URLConnection;

// Referenced classes of package com.macromedia.generator.protocol.fgftp:
//            FgftpDirectoryThread

public class FgftpURLConnection extends URLConnection
{

    public FgftpURLConnection(URL url)
    {
        super(url);
        is = null;
        _fld0171 = null;
    }

    public synchronized void connect()
        throws IOException
    {
        if(connected)
            return;
        String s = _mth0174(url.toString());
        String s1 = _mth0175(s);
        String s2 = _mth0177(s);
        String s3 = _mth0176(s);
        int i = url.getPort();
        if(i < 0)
            i = 21;
        try
        {
            _fld0171 = new FtpClient(s1, i);
        }
        catch(UnknownHostException unknownhostexception)
        {
            System.err.println(String.valueOf(unknownhostexception) + " - " + s1 + ": Is domain needed?");
            throw unknownhostexception;
        }
        if(s2 == null)
            s2 = "anonymous";
        if(s3 == null)
            s3 = System.getProperty("ftp.protocol.user", "Java" + System.getProperty("java.version") + "@");
        _fld0171.login(s2, s3);
    }

    public InputStream getInputStream()
        throws IOException
    {
        if(!connected)
            connect();
        if(is != null)
            return is;
        MessageHeader messageheader = new MessageHeader();
        try
        {
            _fld0171.binary();
            is = _fld0171.get(url.getFile());
            try
            {
                String s = _fld0171.getResponseString();
                int i;
                if((i = s.indexOf(" bytes)")) != -1)
                {
                    int j;
                    char c;
                    for(j = i; --j >= 0 && (c = s.charAt(j)) >= '0' && c <= '9';);
                    j = Integer.parseInt(s.substring(j + 1, i));
                    messageheader.add("content-length", String.valueOf(j));
                    if(j > 0)
                    {
                        ProgressEntry progressentry = new ProgressEntry(url.getFile(), null);
                        progressentry.update(0, j);
                        ProgressData.pdata.register(progressentry);
                        is = new MeteredStream(is, progressentry);
                    }
                }
            }
            catch(Exception exception)
            {
                exception.printStackTrace();
            }
            String s1 = java.net.URLConnection.guessContentTypeFromName(url.getFile());
            if(s1 == null && is.markSupported())
                s1 = java.net.URLConnection.guessContentTypeFromStream(is);
            if(s1 != null)
                messageheader.add("content-type", s1);
        }
        catch(FileNotFoundException _ex)
        {
            _fld0171.cd(url.getFile());
            _fld0171.ascii();
            PipedOutputStream pipedoutputstream = new PipedOutputStream();
            is = new PipedInputStream(pipedoutputstream);
            (new FgftpDirectoryThread(this, _fld0171, url, _fld0171.list(), pipedoutputstream)).start();
            messageheader.add("content-type", "text/html");
        }
        setProperties(messageheader);
        return is;
    }

    String _mth0173(String s)
    {
        return java.net.URLConnection.guessContentTypeFromName(s);
    }

    private static String _mth0174(String s)
    {
        String s1 = "";
        s = s.trim();
        if(s.regionMatches(true, 0, "fgftp://", 0, 8))
        {
            int i = s.indexOf(47, 8);
            if(i < 0)
                i = s.indexOf(63, 8);
            if(i < 0)
                i = s.indexOf(35, 8);
            if(i < 0)
                i = s.length();
            s1 = s.substring(8, i);
        }
        return s1;
    }

    private static String _mth0175(String s)
    {
        String s1 = s;
        int i = s1.indexOf(64);
        if(i > -1)
            s1 = s1.substring(i + 1, s1.length());
        i = s1.indexOf(58);
        if(i > -1)
            s1 = s1.substring(0, i);
        return s1;
    }

    private static String _mth0176(String s)
    {
        String s1 = null;
        int i = s.indexOf(64);
        if(i > -1)
        {
            String s2 = s.substring(0, i);
            i = s.indexOf(124);
            if(i > -1)
                s1 = s2.substring(i + 1, s2.length());
        }
        return s1;
    }

    private static String _mth0177(String s)
    {
        String s1 = null;
        int i = s.indexOf(64);
        if(i > -1)
        {
            s1 = s.substring(0, i);
            i = s.indexOf(124);
            if(i > -1)
                s1 = s1.substring(0, i);
        }
        return s1;
    }

    InputStream is;
    FtpClient _fld0171;
}