# Notes

# Object Serialization

# serialize

If an object contains other types of objects as fields, saving its contents can be complicated.

Java allows you to serialize objects, which is a simpler way of saving objects to a file.
Java 允许您序列化对象,这是将对象保存到文件的一种更简单的方法。

When an object is serialized, it is converted into a series of bytes that contain the object’s data.

If the object is set up properly, even the other objects that it might contain as fields are automatically serialized.

The resulting set of bytes can be saved to a file for later retrieval.

For an object to be serialized, its class must implement the Serializable interface.
对于要序列化的对象,其类必须实现 Serializable 接口。

The Serializable interface has no methods or fields.
Serializable 接口没有方法或字段。

It is used only to let the Java compiler know that objects of the class might be serialized.
它仅用于让 Java 编译器知道该类的对象可能已序列化。

If a class contains objects of other classes as fields, those classes must also implement the Serializable interface, in order to be serialized.
如果一个类包含其他类的对象作为字段,则这些类还必须实现 Serializable 接口才能进行序列化。

The String class, as many others in the Java API, implements the Serializable interface.
与 Java API 中的许多其他类一样, String 类实现了 Serializable 接口。

To write a serialized object to a file, you use an ObjectOutputStream object.
要将序列化的对象写入文件,请使用 ObjectOutputStream 对象。

The ObjectOutputStream class is designed to perform the serialization process.
ObjectOutputStream 类旨在执行序列化过程。

To write the bytes to a file, an output stream object is needed.

FileOutputStream outStream = new FileOutputStream("Objects.dat");
ObjectOutputStream objectOutputFile = new ObjectOutputStream(outStream);

To serialize an object and write it to the file, the ObjectOutputStream class's writeObject method is used.

InventoryItem2 item = new InventoryItem2("Wrench", 20);

The writeObject method throws an IOException if an error occurs.

# deserialization

The process of reading a serialized object's bytes and constructing an object from them is known as deserialization.

To desrialize an object an ObjectInputStream object is used in conjunction with a FileInputStream object.

FileInputStream inStream = new FileInputStream("Objects.dat");
ObjectInputStream objectInputFile = new ObjectInputStream(inStream);

To read a serialized object from the file, the ObjectInputStream class's readObject method is used.

InventoryItem2 item;
item = (InventoryItem2) objectInputFile.readObject();

The readObject method returns the deserialized object.

Notice that you must cast the return value to the desired class type.

The readObject method throws a number of different exceptions if an error occurs.

# demo

public class Employee implements java.io.Serializable
	public String name;
	public String address;
	public transient int SSN;
	public int number;
	public void mailCheck()
		System.out.println("Mailing a check to " + name
							+ " " + address);
import java.io.*;
public class SerializeDemo
	public static void main(String [] args)
		Employee e = new Employee();
		e.name = "James Papademas";
		e.address = "3300 S. Federal";
		e.SSN = 11122333;
		e.number = 100;
			FileOutputStream fileOut =
			new FileOutputStream("employee.ser");
			ObjectOutputStream out = new ObjectOutputStream(fileOut);
			System.out.printf("Serialized data is saved as employee.ser");
		} catch(IOException i)
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
//import java.io.*;
public class DeserializeDemo
	public static void main(String [] args)
		Employee e = null;
			FileInputStream fileIn = new FileInputStream("employee.ser");
			ObjectInputStream in = new ObjectInputStream(fileIn);
			e = (Employee) in.readObject();
		}catch(IOException i)
		}catch(ClassNotFoundException c)
			System.out.println("Employee class not found");
		System.out.println("Deserialized Employee...");
		System.out.println("Name: " + e.name);
		System.out.println("Address: " + e.address);
		System.out.println("SSN: " + e.SSN);
		System.out.println("Number: " + e.number);

# Network Programming

# Network Layers

  • Application Level
    (FTP, Telnet, etc.)
  • Transport Layer
    (TCP, UDP, sockets, etc.)
  • Network Layer
    (Low-level Protocol -- IP, datagrams, etc.)
  • Hardware Layer
    (Ethernet, TokenRing, X.25, etc.)

  • headers: http://nmap.org/book/tcpip-ref.html

  • ports: http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml

  • Sample Http Error Codes: http://www.w3schools.com/tags/ref_httpmessages.asp

# Networking in Java

The java.net package contains classes and interfaces that provide a powerful infrastructure for networking in Java.

Today, we see how networking in Java works.

We will check out the following: Load a document from the web

  1. Some packaged classes
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
  1. Constructing URL Objects
try {
	URL u = new URL("http://www.iit.edu/Sp18/grad.html#itm");
catch (MalformedURLException e) {}


Web browsers communicate with web servers through a standard protocol known as HTTP, an acronym for HyperText Transfer Protocol.
This protocol defines

  • how a browser requests a file from a web server
  • how a browser sends additional data along with the request (e.g. the data formats it can accept),
  • how the server sends data back to the client
  • response codes

Query Strings

  • CGI GET data is sent in URL encoded query strings
  • a query string is a set of name=value pairs separated by ampersands
    Author=Sadie, Julie&Title=Women Composers
  • separated from rest of URL by a question mark

# demo

import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
public class NetTest {
	public static void main(String[] args) throws UnsupportedEncodingException, URISyntaxException {
		StringBuilder builder = new StringBuilder();
		try {
		URL url = new URL(
			URLConnection conn = url.openConnection();
			String line;
			InputStreamReader in = new InputStreamReader(conn.getInputStream());
			BufferedReader data = new BufferedReader(in);
			while ((line = data.readLine()) != null) {
				//String splitter[] = line.split(","); //parse & present data option
		} catch (MalformedURLException mue) {
			System.out.println("Bad URL: " + mue.getMessage());
		} catch (IOException ioe) {
			System.out.println("IO Error:" + ioe.getMessage());
import java.io.PrintStream;
import java.net.InetAddress;
public class NetW
	public static void main(String a[]) throws Exception {
	InetAddress me = InetAddress.getByName("localhost");
	PrintStream o = System.out;	
	o.println("localhost by name =" + me );
	InetAddress me2 = InetAddress.getLocalHost();
	o.println("localhost by getLocalHost =" + me2 );
	InetAddress[] many = InetAddress.getAllByName("microsoft.com");
	for (int i=0; i<many.length; i++) 
		o.println( many[i] );