43 |
*/ |
*/ |
44 |
public class GopherConnection |
public class GopherConnection |
45 |
{ |
{ |
46 |
|
|
47 |
/** |
/** |
48 |
* The default gopher port. |
* The default gopher port. |
49 |
*/ |
*/ |
50 |
public static final int DEFAULT_PORT = 80; |
public static final int DEFAULT_PORT = 80; |
51 |
|
|
52 |
protected Socket socket; |
protected Socket socket; |
53 |
protected InputStream in; |
protected InputStream in; |
54 |
protected OutputStream out; |
protected OutputStream out; |
55 |
|
|
56 |
/** |
/** |
57 |
* Creates a new connection to the gopher server at the specified |
* Creates a new connection to the gopher server at the specified |
58 |
* hostname. |
* hostname. |
59 |
* @param hostname the hostname |
* @param hostname the hostname |
60 |
*/ |
*/ |
61 |
public GopherConnection(String host) |
public GopherConnection(String host) throws IOException |
62 |
throws IOException |
{ |
63 |
{ |
this(host, DEFAULT_PORT); |
64 |
this(host, DEFAULT_PORT); |
} |
65 |
} |
|
66 |
|
/** |
|
/** |
|
67 |
* Creates a new connection to the gopher server at the specified |
* Creates a new connection to the gopher server at the specified |
68 |
* hostname with the specified non-standard port. |
* hostname with the specified non-standard port. |
69 |
* @param hostname the hostname |
* @param hostname the hostname |
70 |
* @param port the non-standard port to use |
* @param port the non-standard port to use |
71 |
*/ |
*/ |
72 |
public GopherConnection(String host, int port) |
public GopherConnection(String host, int port) throws IOException |
73 |
throws IOException |
{ |
74 |
{ |
if (port <= 0) |
75 |
if (port<=0) |
port = DEFAULT_PORT; |
|
port = DEFAULT_PORT; |
|
|
|
|
|
socket = new Socket(host, port); |
|
|
in = socket.getInputStream(); |
|
|
out = socket.getOutputStream(); |
|
|
} |
|
76 |
|
|
77 |
/** |
socket = new Socket(host, port); |
78 |
|
in = socket.getInputStream(); |
79 |
|
out = socket.getOutputStream(); |
80 |
|
} |
81 |
|
|
82 |
|
/** |
83 |
* Returns the directory listing for this gopher server. |
* Returns the directory listing for this gopher server. |
84 |
* When all entries have been read from the listing, the connection will |
* When all entries have been read from the listing, the connection will |
85 |
* be closed. |
* be closed. |
86 |
*/ |
*/ |
87 |
public DirectoryListing list() |
public DirectoryListing list() throws IOException |
88 |
throws IOException |
{ |
89 |
{ |
byte[] CRLF = |
90 |
byte[] CRLF = { 0x0d, 0x0a }; |
{ |
91 |
out.write(CRLF); |
0x0d, 0x0a}; |
92 |
out.flush(); |
out.write(CRLF); |
93 |
InputStream listStream = new CRLFInputStream(in); |
out.flush(); |
94 |
listStream = new MessageInputStream(listStream); |
InputStream listStream = new CRLFInputStream(in); |
95 |
return new DirectoryListing(listStream); |
listStream = new MessageInputStream(listStream); |
96 |
} |
return new DirectoryListing(listStream); |
97 |
|
} |
98 |
|
|
99 |
/** |
/** |
100 |
* Returns the resource identified by the specified selector. |
* Returns the resource identified by the specified selector. |
101 |
* If the resource is text-based, it will need to be wrapped in a |
* If the resource is text-based, it will need to be wrapped in a |
102 |
* CRLFInputStream. |
* CRLFInputStream. |
103 |
*/ |
*/ |
104 |
public InputStream get(String selector) |
public InputStream get(String selector) throws IOException |
105 |
throws IOException |
{ |
106 |
{ |
byte[] chars = selector.getBytes("US-ASCII"); |
107 |
byte[] chars = selector.getBytes("US-ASCII"); |
byte[] line = new byte[chars.length + 2]; |
108 |
byte[] line = new byte[chars.length+2]; |
System.arraycopy(chars, 0, line, 0, chars.length); |
109 |
System.arraycopy(chars, 0, line, 0, chars.length); |
line[chars.length] = 0x0d; |
110 |
line[chars.length] = 0x0d; |
line[chars.length + 1] = 0x0a; |
111 |
line[chars.length+1] = 0x0a; |
out.write(line); |
112 |
out.write(line); |
out.flush(); |
113 |
out.flush(); |
return in; |
114 |
return in; |
} |
115 |
} |
|
|
|
|
116 |
} |
} |