1 |
/* |
/* |
2 |
* $Id$ |
* GopherConnection.java |
3 |
* Copyright (C) 2003 The Free Software Foundation |
* Copyright (C) 2003 The Free Software Foundation |
4 |
* |
* |
5 |
* This file is part of GNU inetlib, a library. |
* This file is part of GNU inetlib, a library. |
50 |
* A gopher client. |
* A gopher client. |
51 |
* |
* |
52 |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
|
* @version $Revision$ $Date$ |
|
53 |
*/ |
*/ |
54 |
public class GopherConnection |
public class GopherConnection |
55 |
{ |
{ |
68 |
* hostname. |
* hostname. |
69 |
* @param hostname the hostname |
* @param hostname the hostname |
70 |
*/ |
*/ |
71 |
public GopherConnection (String host) throws IOException |
public GopherConnection(String host) |
72 |
{ |
throws IOException |
73 |
this(host, DEFAULT_PORT); |
{ |
74 |
} |
this(host, DEFAULT_PORT); |
75 |
|
} |
76 |
|
|
77 |
/** |
/** |
78 |
* Creates a new connection to the gopher server at the specified |
* Creates a new connection to the gopher server at the specified |
80 |
* @param hostname the hostname |
* @param hostname the hostname |
81 |
* @param port the non-standard port to use |
* @param port the non-standard port to use |
82 |
*/ |
*/ |
83 |
public GopherConnection (String host, int port) throws IOException |
public GopherConnection(String host, int port) |
84 |
{ |
throws IOException |
85 |
if (port <= 0) |
{ |
86 |
{ |
if (port <= 0) |
87 |
port = DEFAULT_PORT; |
{ |
88 |
} |
port = DEFAULT_PORT; |
89 |
|
} |
90 |
socket = new Socket (host, port); |
|
91 |
in = socket.getInputStream (); |
socket = new Socket(host, port); |
92 |
out = socket.getOutputStream (); |
in = socket.getInputStream(); |
93 |
} |
out = socket.getOutputStream(); |
94 |
|
} |
95 |
|
|
96 |
/** |
/** |
97 |
* Returns the directory listing for this gopher server. |
* Returns the directory listing for this gopher server. |
98 |
* When all entries have been read from the listing, the connection will |
* When all entries have been read from the listing, the connection will |
99 |
* be closed. |
* be closed. |
100 |
*/ |
*/ |
101 |
public DirectoryListing list () throws IOException |
public DirectoryListing list() |
102 |
{ |
throws IOException |
103 |
byte[] CRLF = { 0x0d, 0x0a }; |
{ |
104 |
out.write (CRLF); |
byte[] CRLF = { 0x0d, 0x0a }; |
105 |
out.flush (); |
out.write(CRLF); |
106 |
InputStream listStream = new CRLFInputStream (in); |
out.flush(); |
107 |
listStream = new MessageInputStream (listStream); |
InputStream listStream = new CRLFInputStream(in); |
108 |
return new DirectoryListing (listStream); |
listStream = new MessageInputStream(listStream); |
109 |
} |
return new DirectoryListing(listStream); |
110 |
|
} |
111 |
|
|
112 |
/** |
/** |
113 |
* Returns the resource identified by the specified selector. |
* Returns the resource identified by the specified selector. |
114 |
* 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 |
115 |
* CRLFInputStream. |
* CRLFInputStream. |
116 |
*/ |
*/ |
117 |
public InputStream get (String selector) throws IOException |
public InputStream get(String selector) |
118 |
{ |
throws IOException |
119 |
byte[] chars = selector.getBytes ("US-ASCII"); |
{ |
120 |
byte[] line = new byte[chars.length + 2]; |
byte[] chars = selector.getBytes("US-ASCII"); |
121 |
System.arraycopy (chars, 0, line, 0, chars.length); |
byte[] line = new byte[chars.length + 2]; |
122 |
line[chars.length] = 0x0d; |
System.arraycopy(chars, 0, line, 0, chars.length); |
123 |
line[chars.length + 1] = 0x0a; |
line[chars.length] = 0x0d; |
124 |
out.write (line); |
line[chars.length + 1] = 0x0a; |
125 |
out.flush (); |
out.write(line); |
126 |
return in; |
out.flush(); |
127 |
} |
return in; |
128 |
|
} |
129 |
|
|
130 |
} |
} |
131 |
|
|