# 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); | |
objectOutputFile.writeObject(item); |
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; | |
try | |
{ | |
FileOutputStream fileOut = | |
new FileOutputStream("employee.ser"); | |
ObjectOutputStream out = new ObjectOutputStream(fileOut); | |
out.writeObject(e); | |
out.close(); | |
fileOut.close(); | |
System.out.printf("Serialized data is saved as employee.ser"); | |
} catch(IOException i) | |
{ | |
i.printStackTrace(); | |
} | |
} | |
} |
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; | |
try | |
{ | |
FileInputStream fileIn = new FileInputStream("employee.ser"); | |
ObjectInputStream in = new ObjectInputStream(fileIn); | |
e = (Employee) in.readObject(); | |
in.close(); | |
fileIn.close(); | |
}catch(IOException i) | |
{ | |
i.printStackTrace(); | |
return; | |
}catch(ClassNotFoundException c) | |
{ | |
System.out.println("Employee class not found"); | |
c.printStackTrace(); | |
return; | |
} | |
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
- 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; |
- Constructing
URL
Objects
try { | |
URL u = new URL("http://www.iit.edu/Sp18/grad.html#itm"); | |
} | |
catch (MalformedURLException e) {} |
# HTTP
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 ampersandsAuthor=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 { | |
//https://www.quandl.com/tools/api | |
StringBuilder builder = new StringBuilder(); | |
try { | |
URL url = new URL( | |
"https://www.quandl.com/api/v1/datasets/WIKI/AAPL.csv?"+ | |
"column=2&sort_order=asc&collapse=quarterly&trim_start=2012-01-01&trim_end=2013-12-31"); | |
URLConnection conn = url.openConnection(); | |
String line; | |
conn.connect(); | |
InputStreamReader in = new InputStreamReader(conn.getInputStream()); | |
BufferedReader data = new BufferedReader(in); | |
while ((line = data.readLine()) != null) { | |
//String splitter[] = line.split(","); //parse & present data option | |
builder.append(line); | |
builder.append("\n"); | |
} | |
} catch (MalformedURLException mue) { | |
System.out.println("Bad URL: " + mue.getMessage()); | |
} catch (IOException ioe) { | |
System.out.println("IO Error:" + ioe.getMessage()); | |
} | |
System.out.println(builder.toString()); | |
} | |
} |
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] ); | |
} | |
} |