package ntestserver;

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


/**
 ************************************
 * Network File Transfer Java class *
 ************************************
 * 
 * Pavel Novák 2oo8 [cz]
 * http://pavel-novak.net
 *  
 */
public class NetFileTransfer {

    //promenne a objekty
    private int i;
    private byte[] pole = new byte[16384];
    private File soubor;
    private BufferedInputStream vstup = null;
    private BufferedOutputStream vystup = null;
    private Socket soket = null;
    
    //konstruktor
    public NetFileTransfer(Socket sok) {
        
        this.soket = sok;
    }
     
    /*
     ***************************
     * Metoda poslat(String s) *
     ***************************
     * __________________________________________________________
     * -> Vstupni parametr String s by melo byt jmeno souboru,
     *    nebo absolutni cesta k souboru, ktery chceme prenaset.
     * __________________________________________________________
     */
    public void poslat(String s) {
        
        this.soubor = new File(s);
        try {
            vstup = new BufferedInputStream(new FileInputStream(soubor));
            vystup = new BufferedOutputStream(soket.getOutputStream());
            while((i = vstup.read(pole)) != -1) {
                vystup.write(pole, 0, i);
            }
            vystup.close();
            vstup.close();
        } catch(FileNotFoundException ex) {
            System.err.println(ex);
        } catch(IOException ex) {
            System.err.println(ex);
        }
    }
    
    /*
     *****************************
     * Metoda prijmout(String s) *
     *****************************
     * ____________________________________________________________
     * -> Tato metoda se stara o prijeti souboru. Vstupni parametr
     *    by mel byt nazev prijateho souboru, nebo absolutni cesta
     *    k prijatemu souboru.
     * ____________________________________________________________
     */
    public void prijmout(String s) {
        
        this.soubor = new File(s);
        try {
            vstup = new BufferedInputStream(soket.getInputStream());
            vystup = new BufferedOutputStream(new FileOutputStream("prijatysoubor"));
            while((i = vstup.read(pole)) != -1) {
                vystup.write(pole, 0, i);
            }
            vystup.close();
            vstup.close();
        } catch(FileNotFoundException ex) {
            System.err.println(ex);
        } catch(IOException ex) {
            System.err.println(ex);
        }
    }
}
