📄 whoisurlconnection.java
字号:
package com.wrox.whois;
import java.io.*;
import java.net.*;
// Represents a communication link with an RFC 954 WHOIS server
public class WhoisURLConnection extends URLConnection {
// Cached data retrieved at connect() time
protected byte[] data = null;
/* An array of dimensions [n][2] where n is the number of
headers. For the n-th value the header key is in [n][0]
while the header value is in [n][1] */
protected String[][] headers = null;
// Constructs a new WHOIS URL connection in an unconnected state
public WhoisURLConnection(URL url) throws IOException {
super(url); // don't forget!
if (url.getHost() == null) {
throw new UnknownHostException("URL has null host value");
}
if ( (url.getQuery() != null) || (url.getRef() != null) ) {
throw new MalformedURLException("whois URL cannot contain query " +
"or reference parts");
}
}
// Connects to the WHOIS server and retrieves the response
public synchronized void connect() throws IOException {
if (connected) {
return;
}
cacheData();
connected = true;
}
// Performs the WHOIS query and caches the data
protected synchronized void cacheData() throws IOException {
Socket socket;
if (url.getPort() == -1) {
socket = new Socket(url.getHost(), Handler.WHOIS_PORT);
} else {
socket = new Socket(url.getHost(), url.getPort());
}
try {
// Write whois request
BufferedWriter writer =
new BufferedWriter(new OutputStreamWriter
(socket.getOutputStream()));
// Query will be preceeded by the '/' character
String query = url.getFile();
if ( (query == null) || (query.length() < 2) ) {
writer.write("?\r\n");
} else {
// Decode any special characters encoded in the URL
query = URLDecoder.decode(query.substring(1, query.length()));
writer.write(query + "\r\n");
}
writer.flush();
// Copy data from socket input stream into a byte-array buffer
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
BufferedInputStream inStream =
new BufferedInputStream(socket.getInputStream());
byte[] buffer = new byte[4096];
int len = inStream.read(buffer);
while(len != -1) {
byteStream.write(buffer, 0, len);
len = inStream.read(buffer);
}
// Store the cached data and set the headers
data = byteStream.toByteArray();
headers = new String[][]
{ { "content-type", "text/plain" },
{ "content-length", Integer.toString(data.length) },
{ "content-encoding", "iso-8859-1" } };
} finally {
try { socket.close(); } catch (Throwable e) { }
}
}
// Returns an input stream for reading from this connection
public synchronized InputStream getInputStream()
throws IOException {
connect();
if (!useCaches) {
cacheData();
}
return(new ByteArrayInputStream(data));
}
// Returns the name of the specified header (may be null)
public synchronized String getHeaderField(String name) {
try {
connect();
} catch (IOException e) {
return(null);
}
for(int i=0; i<headers.length; i++) {
if (headers[i][0].equalsIgnoreCase(name)) {
return(headers[i][1]);
}
}
return(null);
}
/* Returns the value of the n-th header, or null if fewer
headers exist */
public synchronized String getHeaderFieldKey(int n) {
try {
connect();
} catch (IOException e) {
return(null);
}
if (n >= headers.length) {
return(null);
} else {
return(headers[n][0]);
}
}
/* Returns the key of the n-th header, or null if fewer
headers exist */
public synchronized String getHeaderField(int n) {
try {
connect();
} catch (IOException e) {
return(null);
}
if (n >= headers.length) {
return(null);
} else {
return(headers[n][1]);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -