Fall Semester, 1998
Assignment 4
To Assignment Index
San Diego State University -- This page last updated 11/4/98

Due: November 25 in class for SDSU section,
November 24 in class for Qualcomm section

The goal of this assignment is to get some experience with files, streams, exceptions and threads.

In this assignment, you will write a program that processes text. The program reads input, processes the input text, then write the text to the output. The program has three different possible inputs: keyboard, file, or network socket. The text can be converted to uppercase, to lowercase, or the case of the characters left unchanged. The program has two possible outputs: to a file or to the screen. The input may be either normal text or text compressed using the gzip algorithm. If the input is compressed, then it must be uncompressed before you process it. The output either will be normal text or compressed via gzip. The converting and compression is to be done using streams and/or readers/writers. You can use the java.util.gzip streams.

The program has a number of options: convert to uppercase, convert to lowercase, no case conversion, output to screen or file, name of the output file, output compressed or not, and input compressed or not. These options are to be set in a configuration file. The values in the configuration file can be overridden by flags set in the command line when the program is started. The program needs to have a default name for the configuration file. You need to be able override the default configuration file name on the command line that starts the program. The following table contains the options that can occur in the configuration file. Use name-value pairs. You can select syntax of the configuration file.

output=file | screen
textCase=upper | lower |
compressedInput=true | false
compressedOutput=true | false
When a user runs the program, they will be prompted for source of the input. The program then starts a thread to process the text, and prompts the user for another input source. If the source is the screen, the rest of the text input from the keyboard goes to the output until the user types the end of file character (control d). If the input source is a file, the user is prompted for the name of the file. If the input source is a network socket, the program then prompts the user for the socket number to read.

Your program needs to handle all the exceptions. If there is an exception in doing IO, the files used need to be closed. The program should not stop. You can notify the user that an error occurred, but do not print out the exception.

Output file. To aid debugging all output files need to have a time stamp appended to the file name. The form of the time stamp is yy-DDD-HH:mm:ss, where yy = two digits for the current year ( for example 98), DDD = the day of the year (308 for example), HH = the hour of the day in the 24 hour system, mm = the minute in the hour, ss = the second in the minute. You might find java.text.SimpleDateFormat useful.

Sockets. Use socket numbers between 5000 and 65536. If a socket is in use by someone else, you will not be able to use it. The class below shows how to open a socket. The method getInputStream does the work. The main shows how to use getInputStream. To test the program on Rohan do the following. First, compile and run the NetworkSocket class. In a separate window or terminal type "telnet rohan 5050". You should see:

    eli 13-> telnet rohan 5050
    Connected to
    Escape character is '^]'.
You can now type. When you type return, the text will be sent to the NetworkSocket program and be displayed. To exit the telnet session you need to type the escape character and then type quit.


public class NetworkSocket {
    public static InputStream getInputStream( int portNumber ) throws IOException {
        ServerSocket acceptor = new ServerSocket( portNumber );
        Socket client = acceptor.accept();
        return client.getInputStream();

    public static void main( String[] args ) throws IOException {
        InputStream in = NetworkSocket.getInputStream( 5050 );
        int inputChar;
        System.out.println( "Waiting for input" );
        while ( (inputChar = != -1 )
            System.out.print( (char) inputChar );
        System.out.println( "\n The end" );
Optional. Create a stream (or reader/writer) that will process text using regular expressions. Use the gnu regular expression package that is part of the SDSU Java library to do the regular expression processing.

Copyright © 1998 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
All rights reserved.

visitors since 11/04/98