Sunday, August 7, 2011

Test File & Serialization


package com.evs.objava33.class11;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TestFile {

public static void main(String[] args) {
File myFile = new File("d:/abc.txt");
// File myDir = new File("c:/abc");
// File myDirFile = new File(myDir, "File.java");
// File myDirFile1 = new File("c:/abc", "File.java");
//
// try {
// File remoteFile = new File(new URI("http://www.yahoo.com/abc.html"));
// } catch (URISyntaxException e) {
// e.printStackTrace();
// }

String userDir = System.getProperty("user.dir");

// File dDrive = new File("D:" + File.pathSeparator + "temp-read");
File dDrive = new File("D:/temp-read");
String[] files = dDrive.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith("txt");
}
});
File[] files1 = dDrive.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return true;
}
});
for (String s : files) {
System.out.println(s);
/*
* abc.txt Solaris10_9-10_VM.zip WorkFlowDAOImpl.class
*/
}

// try {
// BufferedOutputStream out = new BufferedOutputStream(
// new FileOutputStream(myFile, true));
// out.write("this is my first file".getBytes());
// out.close();
//
// // RandomAccessFile raf = new RandomAccessFile(myFile, "rw");
// // raf.write("This is my first File".getBytes());
// // raf.close();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }

// try {
// Process p = Runtime.getRuntime().exec("ping 192.168.0.1");
// } catch (IOException e) {
// e.printStackTrace();
// }

ObjectOutputStream fout = null;
try {
fout = new ObjectOutputStream(new FileOutputStream(myFile));
fout.writeObject(new TestObject(1, "One"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

ObjectInputStream fin = null;
try {
fin = new ObjectInputStream(new FileInputStream(myFile));
Object obj = fin.readObject();
if (obj instanceof TestObject) {
TestObject testObj = (TestObject) obj;
System.out.println(testObj);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

private static class TestObject implements Serializable {
private static final long serialVersionUID = -4314877130551524995L;

private String value;
private Integer key;
private transient String anotherString;

public TestObject(Integer key, String value) {
this.key = key;
this.value = value;
this.anotherString = "Unknown";
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "TestObject [value=" + value + ", key=" + key
+ ", anotherString=" + anotherString + "]";
}

/**
* @return the value
*/
public String getValue() {
return value;
}

/**
* @param value
*            the value to set
*/
public void setValue(String value) {
this.value = value;
}

/**
* @return the key
*/
public Integer getKey() {
return key;
}

/**
* @param key
*            the key to set
*/
public void setKey(Integer key) {
this.key = key;
}
}
}

Thursday, August 4, 2011

Assignment - Week 5

Create one chat solution, which will be used to chat between two users.

- Server component will be used to connect multiple users
- Client Component will be used to connect to server.
- Multiple Client Components will be able to send message to each other via Server.

Server Start Up Flow
- Server Listen to a port (6666)

Connect Flow
- Client Open Socket with Server and then Send a message 'LOGON' which will ensure that this user is connected with Server

Chat with Server
- Client Open Socket with Server
- Client Send 'LOGON' Message to Server
- Client Send Message to 'Chat Message'

Chat with Another Client
- Client Open Socket with Server
- Client Send 'LOGIN' Message to Server
- Client Send Message 'CLIENT1 == Message for Client'
- Server check if CLIENT1 is connected or not.
- If Client1 is connected send this message to him/her, otherwise respond user is not present

Chat Client


package com.evs.objava33.class10;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;

public class ChatClient {
public static void main(String[] args) {

try {
Socket client = new Socket("127.0.0.1", 6666);
// Here you are connected
BufferedOutputStream out = new BufferedOutputStream(
client.getOutputStream());
BufferedInputStream in = new BufferedInputStream(
client.getInputStream());
BufferedInputStream keyboard = new BufferedInputStream(System.in);

byte[] bKey = new byte[1024];
// byte[] bIn = new byte[1024];
// byte[] bOut = new byte[1024];

while (true) {
int readKey = keyboard.read(bKey); // Read from Keyboard
out.write(bKey, 0, readKey); // Write to client
out.flush();
}
// int readClient = in.read(bIn); // Input Buffer from Client
// System.out.println("Client: " + new String(bIn, 0, readClient));
} catch (IOException e) {
e.printStackTrace();
}

}

}

Chat Server


package com.evs.objava33.class10;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatServer {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(6666);
// You ensured that there isn't any server socket
Socket client = server.accept();
System.out.println("New Client: " + client);
// Here you are connected
BufferedOutputStream out = new BufferedOutputStream(
client.getOutputStream());
BufferedInputStream in = new BufferedInputStream(
client.getInputStream());
BufferedInputStream keyboard = new BufferedInputStream(System.in);

byte[] bKey = new byte[1024];
byte[] bIn = new byte[1024];
byte[] bOut = new byte[1024];

// int readKey = keyboard.read(bKey); // Read from Keyboard
// out.write(bKey, 0, readKey); // Write to client
// out.flush();
while (true) {
int readClient = in.read(bIn); // Input Buffer from Client
System.out.println("Client: ("
+ new java.util.Date(System.currentTimeMillis())
+ "): " + new String(bIn, 0, readClient));
}
} catch (IOException e) {
e.printStackTrace();
}
}

}

Try Streams


package com.evs.objava33.class10;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class TryStream {

// BufferredInputStream = Memory Buffer
// DataINputStream = Primitive Data Types Read / Write
// CheckedInputStreamjj = Checksum maintenance
// CiperInputSteam = ENcypt- decyrtion
// DigestInputStream = One way encryption
// InflatorINputStream = Compression (91-97%), Image (4-9%), Video (1-3%).
// LineNUmberINputStream = Read Data and keep track of line#
// ProgressMonitorInputStream = Keep track of progress
// PushBackInputStream = Send it to designated resource

public static void main(String[] args) {
// BufferedInputStream in = new BufferedInputStream(System.in);
// try {
// byte[] b = new byte[100];
// int readBytes = in.read(b);
// String str = new String(b, 0, readBytes);
// System.out.println(str);
// } catch (IOException e) {
// e.printStackTrace();
// }

BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
try {
char[] b = new char[100];
int readBytes = reader.read(b);
String str = new String(b, 0, readBytes);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}

}
}

MyExceptionClass


package com.evs.objava33.class9;

public class MyExceptionClass extends Exception {

public MyExceptionClass() {
// TODO Auto-generated constructor stub
}

public MyExceptionClass(String message) {
super(message);
// TODO Auto-generated constructor stub
}

public MyExceptionClass(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}

public MyExceptionClass(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}

}

Test Exception


package com.evs.objava33.class9;

public class TestException {

private static int[] intArray = { 1, 2, 3, 4, 5, 6 };

public static void main(String[] args) {
try {
// Catchable Code
// System.out.println(3 / 0);
System.out.println(getValue(3, 0.0));
System.out.println(getArrayValue(7));
} catch (Exception e) {
System.out.println("Exception occur: Please contact ");
e.printStackTrace();
} finally {
// Clean up
}
}

public static int getValue(final Integer i, final Double j)
throws Exception {
return (int) (i / j);
}

private static int getArrayValue(int i) throws MyExceptionClass {
try {
return intArray[i];
} catch (Exception e) {
e.printStackTrace();
throw new MyExceptionClass(e);
}
}

}