HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Design Patterns: An Unavoidable Fact of Life"
Vol. 2, Issue 6, p. 24

	

Listing 1.

FileOutputStream fileOut = new FileOutputStream("super_secret.txt");
PrintStream printStream = new PrintStream(fileOut );
printStream.println("Code: The blue pig has no legs.  Reply: Corn grows in stone");
printStream.close();

Listing 2

class BitReverserOut extends FilterOutputStream {
	byte trueByte = (byte)255;

	public BitReverserOut(OutputStream out){
		super(out);
	}

	private byte flip(byte b){
	   return (byte)( ((b << 4) & 0xF0) + ((b >> 4) & 0x0F) );
	}

    public void write(byte b[], int off, int len) throws IOException {
        byte temp;
        for(int i = off; i < len ; i++) {
            temp = flip(b[i]);  //(byte)(b[i] ^ trueByte); // cause
           // System.out.print(temp);
            out.write(temp);
        }
    }
}

Listing 3.

FileOutputStream fileOut = new FileOutputStream("super_secret.txt");
BitReverserOut bitReverser = new BitReverserOut(fileOut);
PrintStream printStream = new PrintStream(bitReverser);
printStream.println("Code: The blue pig has no legs.  Reply: Corn grows in stone");
printStream.close();

Listing 4.

import java.io.*;

public class SuperSecretApp {

    public static void main(String [] args){

        try{

            if(args[0].equals("out"))
            {
                FileOutputStream fileOut = new FileOutputStream("super_secret.txt");
                BitReverserOut bitReverser = new BitReverserOut(fileOut);
                PrintStream printStream = new PrintStream(bitReverser);
                printStream.println("Code: The blue pig has no legs.  Reply: Corn grows in stone");
                printStream.close();
            }else if(args[0].equals("in")){
                FileInputStream fileIn = new FileInputStream("super_secret.txt");
                BitReverserIn bitReverser = new BitReverserIn(fileIn);
                DataInputStream dis = new DataInputStream(bitReverser);
                String str = dis.readLine( );
                System.out.println(str);
            }else
                System.out.println("wrong arguments");

        }catch(IOException e){System.out.println(e);}
    }
}


class BitReverserOut extends FilterOutputStream {
	byte trueByte = (byte)255;

	public BitReverserOut(OutputStream out){
		super(out);
	}

	private byte flip(byte b){
	   return (byte)( ((b << 4) & 0xF0) + ((b >> 4) & 0x0F) );
	}

    public void write(byte b[], int off, int len) throws IOException {
        byte temp;
        for(int i = off; i < len ; i++) {
            temp = flip(b[i]);  //(byte)(b[i] ^ trueByte); // cause
           // System.out.print(temp);
            out.write(temp);
        }
    }
}

class BitReverserIn extends FilterInputStream {
	byte trueByte = (byte)255;

	public BitReverserIn(InputStream in){
		super(in);
	}

	private int flip(int i){
	   byte b = (byte)i;
	   return ( ((b << 4) & 0xF0) + ((b >> 4) & 0x0F) );

	}

    public int read() throws IOException {
        int r;
	    r = in.read();
	   // System.out.println(r);
	    if(r != -1)
	    {
	        r = flip(r);//((byte)r ^ trueByte);
	    }
	    return r;

    }

    public int read(byte b[], int off, int len) throws IOException {
        int r;
	    r = in.read(b, off, len);

        for (int i = off; i < len ; i++) {
            System.out.println(b[i]);
            b[i] = (byte)(b[i] ^ trueByte); // cause

        }
        return r;
    }
}

Listing 5.

Driver drvr = new CompanyXPropretaryDriver( );
Connection cnct = drvr.connect(dbName,props);
...
public foo {
	Statement stmt  = cnct.createStatement( );
	ResultSet rslt = stmt.executeQuery("select * from employees");
	ResultSetMetaData = rslt.getMetaData( );
	....
}

Listing 6.

public class CompanyAConnection implements java.sql.Connection{
	...
	public Statement createStatement( ){
		return new CompanyAStatement( );
	}
...
}


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.