package fsModule;
import utility.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;

public class FsDataClientHandler  extends DataClientHandler
{


	public StringBuffer buffer = new StringBuffer();
	volatile boolean readingComplete = false;
	public FsDataClientHandler(InputStream in, OutputStream out)
	{
		super(in,out);
		System.out.println("starting nb data client handler");
	}
	public  boolean sendStream(String fileName) throws IOException
	{
		return sendStream(new File(fileName));
	}
	public  boolean sendStream(File fileName) throws IOException
  	{
    		boolean success = true;
    		Connection conn = getConnection();
    		if (conn != null)
		{
      			//System.out.println("Trying to lock on connection" + cid);
        		synchronized (conn.out)
			{
          			//System.out.println("Got lock on connection " + cid);
          			BufferedOutputStream buffOut = null;
				try
				{
					buffOut = new BufferedOutputStream(conn.out);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				try
				{
					//File f = new File(fileName);
					byte buff[] = new byte [40];
					FileInputStream inFileStream = new FileInputStream (fileName);
					BufferedInputStream inBuffStream = new BufferedInputStream(inFileStream);
					//BufferedOutputStream outBuffStream = new BufferedOutputStream (cli.getOutputStream());
					int next =0;
					while((next =( inBuffStream.read ( buff,0,buff.length ) )) != -1)
					{

						//System.out.print(new String(buff,0,next));
						buffOut.write(buff,0,next);
						buffOut.flush();
					}
					buffOut.close();
					removeConnection();
				}
				catch (Exception e)
				{
					success = false;
					e.printStackTrace();
				}
				//if(success)
				//	System.out.println("writeObject successfull");
			}
		}

		return success;
	}
	public boolean readStream() throws IOException
	{
    		boolean result = false;


		Connection conn = getConnection();
    		if (conn != null)
		{

			//ClientHeader receivedRequest = null;
			synchronized (conn.in)
			{
          			System.out.println("reading file from server ");
				BufferedInputStream buffIn = null;
				try
				{
					buffIn = new BufferedInputStream(conn.in);
					byte buff[] = new byte[40];
					int next =0;
					System.out.println("File input from server");
					while((next = buffIn.read(buff,0,buff.length)) != -1)
					{
						//System.out.println("got it");
						//System.out.print(new String(buff,0,next));
						buffer.append(new String(buff,0,next));
					}
					System.out.println("file finished");
					result = true;
					readingComplete = true;
				}
				catch (StreamCorruptedException e)
				{
					System.out.println(e + " : closing it(1)");
					removeConnection();
					result = false;
				}


				catch (java.lang.NullPointerException e)
				{
					System.out.println( e + " : closing it(3)");
					removeConnection();
					result = false;
				}
				catch (SocketException e)
				{
					e.printStackTrace();
					removeConnection();
				}
				catch (Exception e)
				{
					e.printStackTrace();
					removeConnection();
					result = false;
				}
			}
		}
		return result;
	}
	public StringBuffer getBuffer()
	{
		return buffer;
	}
}
