
import java.net.*;
import java.io.*;
import java.util.*;

public class TextFileReader extends Object
{
  private int iNumberOfLinesRead;  
  private boolean bReadComplete;
  private ArrayList aaLineList;
  private String sFilePath;
  private boolean bFileIsRemote;
  private boolean bReadSuccessful;


  //--------------------------------------------
  public TextFileReader(String filePath, boolean fileIsRemote)
  {
    this.sFilePath = filePath;
    this.bFileIsRemote = fileIsRemote;
    iNumberOfLinesRead = 0;
    bReadComplete = false;
    bReadSuccessful = false;
    aaLineList = new ArrayList();

  } //-- constr: (String, boolean)


  //--------------------------------------------
  public String getFilePath()
    { return this.sFilePath; }

  //--------------------------------------------
  public boolean isRemote()
    { return this.bFileIsRemote; }

  //--------------------------------------------
  public int getLineCardinality()
    { return this.iNumberOfLinesRead; }

  //--------------------------------------------
  public boolean isComplete()
    { return this.bReadComplete; }


  //-- TO DO. Add proper exception handling
  //--  when the file is not a file or is
  //--  not a text file.

  //--------------------------------------------
  public int read() 
  {
    if (bFileIsRemote)
    {
      try
      {
        URL urlTest = new URL(this.sFilePath);
      }
      catch (MalformedURLException e)
      {
        if (e.getMessage().indexOf("no protocol") != -1)
        {
          this.sFilePath = "http://" + this.sFilePath;
        }
        //System.out.println(e.getMessage());
      }

      try
      {
        URL urlRemoteTextFile =
          new URL(this.sFilePath);

        URLConnection urlcConnectToFile =
          urlRemoteTextFile.openConnection();
        BufferedReader brReader =
          new BufferedReader(new InputStreamReader(
            urlcConnectToFile.getInputStream()));

        String sInputLine;
        sInputLine = brReader.readLine();
        while (sInputLine != null)
        {
          aaLineList.add(sInputLine);
          sInputLine = brReader.readLine();
          iNumberOfLinesRead++;
        } //-- while
        bReadComplete = true;
        brReader.close();
        return 0;
      }
      catch (IOException e)
      {
        if (iNumberOfLinesRead == 0)
          { return -1; }
        else
          { return iNumberOfLinesRead; }

      } //-- try, catch [remote file]
    }
    else
    {
       try
       {
         FileReader firTheFile =
           new FileReader(this.sFilePath);
         BufferedReader buffrTextFile =
           new BufferedReader(firTheFile);

         String sTextLine;

         sTextLine = buffrTextFile.readLine();
         while (sTextLine != null)
         {
           aaLineList.add(sTextLine);
           sTextLine = buffrTextFile.readLine();
           iNumberOfLinesRead++;
         }
         bReadComplete = true;
         return 0;
       }
       catch (IOException e)
       {
         if (iNumberOfLinesRead == 0)
           { return -1; }
         else
           { return iNumberOfLinesRead; }
       } //-- try,catch [local file]
    } //-- if, else 
  } //-- method: read


  //--------------------------------------------
  public void threadedRead()
  {
    if (bFileIsRemote)
    {
      new RemoteTextFileReaderThread("Remote").start();
    }
    else
    {
      new LocalTextFileReaderThread("Local").start();
      System.out.println("After local thread");
    }
  } // method: threadedRead


  //--------------------------------------------
  public String[] getStringArray()
  {
    String[] aaReturn = new String[this.iNumberOfLinesRead];
    for (int ii = 0; ii < this.iNumberOfLinesRead; ii++)
    {
      aaReturn[ii] = (String)aaLineList.get(ii);
    }
    return aaReturn;
  } //-- method: getStringArray


  //--------------------------------------------
  public StringBuffer[] getStringBufferArray()
  {
    StringBuffer[] aaReturn = new StringBuffer[this.iNumberOfLinesRead];
    for (int ii = 0; ii < this.iNumberOfLinesRead; ii++)
    {
      aaReturn[ii] = (StringBuffer)aaLineList.get(ii);
    }
    return aaReturn;    
  } //-- method: getStringBufferArray()


  //--------------------------------------------
  public StringBuffer getStringBuffer()
  {
    StringBuffer sbReturn = new StringBuffer();
    for (int ii = 0; ii < this.iNumberOfLinesRead; ii++)
    {
      sbReturn.append(this.aaLineList.get(ii) + "\n");
    }

    return sbReturn;
  } //-- method: getStringBuffer


  //--------------------------------------------
  class RemoteTextFileReaderThread extends Thread
  {

    RemoteTextFileReaderThread(String sThreadName)
    {
      super(sThreadName);
      //setPriority(MIN_PRIORITY);
    }

    public void run()
    {

      try
      {
        URL urlTest = new URL(sFilePath);
      }
      catch (MalformedURLException e)
      {
        if (e.getMessage().indexOf("no protocol") != -1)
        {
          sFilePath = "http://" + sFilePath;
        }
      } //-- try, catch

      try
      {
        URL urlRemoteTextFile =
          new URL(sFilePath);

        URLConnection urlcConnectToFile =
          urlRemoteTextFile.openConnection();
        BufferedReader brReader =
          new BufferedReader(new InputStreamReader(
            urlcConnectToFile.getInputStream()));

        String sInputLine;
        sInputLine = brReader.readLine();
        while (sInputLine != null)
        {
          aaLineList.add(sInputLine);
          sInputLine = brReader.readLine();
          iNumberOfLinesRead++;
        } //-- while
        bReadComplete = true;
        brReader.close();
      }
      catch (IOException e)
      {
      } //-- try, catch [remote file]

    } //-- method: run
  } //-- inner class: RemoteTextFileReaderThread 


  //--------------------------------------------
  class LocalTextFileReaderThread extends Thread
  {

    LocalTextFileReaderThread(String sThreadName)
    {
      super(sThreadName);
      //setPriority(MIN_PRIORITY);
    }

    public void run()
    {
       try
       {
         FileReader firTheFile =
           new FileReader(sFilePath);
         BufferedReader buffrTextFile =
           new BufferedReader(firTheFile);

         String sTextLine;

         sTextLine = buffrTextFile.readLine();
         while (sTextLine != null)
         {
           aaLineList.add(sTextLine);
           sTextLine = buffrTextFile.readLine();
           iNumberOfLinesRead++;
         }
         bReadComplete = true;
       }
       catch (IOException e)
       {
       } //-- try,catch [local file]
    } //-- method: run 
  } //-- inner class: LocalTextFileReaderThread 

  //--------------------------------------------
  public static void main(String[] args) throws Exception
  {
    String sResource = "";
    boolean bIsRemoteFile = false;
    if (args.length == 1)
    {
      sResource = args[0];
      bIsRemoteFile = true;
    }
    else if (args.length == 2)
    {
      sResource = args[0];
      bIsRemoteFile = Boolean.valueOf(args[1]).booleanValue();
    }
    else
    {
      System.out.println(
        "TextFileReader:                                 \n" +
        "-------------------------------                 \n" +
        "  Command line and test usage:                  \n" +
        "  java TextFileReader ResourceToRead [IsURL]  \n\n" +
        "  The purpose of this class is to provide a mechanism     \n" +
        "  to read a text file into a Array like data structure.   \n" +
        "  The text file may either be a local file or a URL     \n\n" +
        "  The class also provides support for threaded            \n" +
        "  reading of a resource through the threadedRead() method \n" +
        "  Examples:              \n" +
        "  ---------              \n" +
        "    java TextFileReader  www.yahoo.com                   \n" +
        "    Down loads the default file from an internet site  \n\n" +
        "    java TextFileReader  d:\\SomeDir\\SomeFile false       \n" +
        "    Reads the local file                                   \n");

      System.exit(-1);
        
    } //-- if, else [Command Line Arguments]

    TextFileReader txtrRead =
      new TextFileReader(sResource, bIsRemoteFile);

    String sbMessage = new String(
      "     \n" +
      "TESTING CLASS: TextFileReader()  \n" +
      "---------------------------------------- \n" +
      " Properties     \n" +
      "   bFileIsRemote      = " + txtrRead.isRemote() + "\n" +
      "   sFilePath          = " + txtrRead.getFilePath() + "\n" +
      "   bIsComplete        = " + txtrRead.isComplete() + "\n" +
      "   iNumberOfLinesRead = " + txtrRead.getLineCardinality() + "\n");

    System.out.println(sbMessage);
    //txtrRead.read();
    txtrRead.threadedRead();
    while (!txtrRead.isComplete())
    {}
    System.out.println(txtrRead.getStringBuffer());
  } //-- main()
} //-- TextFileReader class
