IO: Local and Remote


Classes

class  FileInputStream
class  HTTPInputStream
class  InputStream
class  URLInputStream

Detailed Description

The Local/Remote IO Module is a set of classes that make accessing local files and remote files (through HTTP) transparent. They provide support for reading one byte at a time (InputStream::Read), one character look ahead (InputStream::Peek) and for retrieving the absolute location of the inpout source (InputStream::GetLocation)

The URLInputStream class takes care of creating the correct type of input stream (FileInputStream or HTTPInputStream)for you. For Example, the following code will print all data from a URL, wether it is a file:// or http:// URL.

    URLInputStream stream (url);
     
    while(! stream).IsDone()) 
        std::cout << stream.Read() << std::endl;

There are many kinds of error conditions that can occur when a program accesses documents on the web. The methods on the web access classes throw exceptions when errors occur. Examples of exceptions that might be thrown are: InvalidURLException, FileException, NetworkException, and IllegalStateException. Your code must be prepared to handle these exceptions when they occur, or your program will terminate abnormally.

The following is a more in depth example of using the URLInputSteam class.

#include <iostream>
#include "URLInputStream.h"
#include "CS240Exception.h"


void print_contents (InputStream &stream);

/**
 * printer takes a list of urls from he command line and prints their contents to std::cout
 * 
 * usage: printer [files]
 * 
 * ex. 
 *     printer http://google.com file:/home/somefile.txt
 * */
int main (int argc, char* argv [])
{
    
        
    

        for(int i = 1;i<argc;++i)
        {
            std::string path=argv[i];
            //Print source URL
            std::cout << "Printing: " << path << std::endl;
            std::cout << "=======================================" << std::endl;
            
            try
            {
                URLInputStream stream (path);
                print_contents (stream);

                //Handle redirections (Find out where we redirected to)
                path = stream.GetLocation();
                std::cout << "=======================================" << std::endl;
                std::cout << "Actual Location: " << path << std::endl;
            }
            catch (std::exception &e)
            {
                std::cout << "Exception Occurred:" << e.what() << std::endl;
            }
            catch (CS240Exception &e) 
            {
                std::cout << "Exception Occurred:" << e.GetMessage() << std::endl;
            }
            catch (...)
            {
                std::cout << "Unknown Exception Occurred" << std::endl;
            }
        }
                
    
    return 0;
}

//!Note that this function can accept any kind of InputStream, not just URLInputStream
void print_contents (InputStream &stream)
{
    
    while (!stream.IsDone())
    {
        std::cout << stream.Read();
    }
}


Generated on Wed Jul 7 16:30:29 2010 for CS240Utils by  doxygen 1.5.8