01: package vicazh.hyperpool.stream.net.http;
02:
03: import java.io.*;
04: import vicazh.hyperpool.Start;
05: import vicazh.hyperpool.stream.*;
06:
07: class ResourceStream extends ClientStream implements Runnable {
08:
09: ResourceStream(Session session) {
10: super (session, new NullStream());
11: }
12:
13: public void run() {
14: synchronized (connection) {
15: try {
16: session.getServer().head("HTTP/1.1", ServerStream.OK,
17: "");
18: String type = ((ResourceService) connection.element).map
19: .get(getFile().substring(
20: getFile().lastIndexOf('.') + 1));
21: session.getServer().field("Content-Type",
22: type == null ? "text/html" : type);
23: session.getServer().field("Server",
24: Start.NAME + "/" + Start.VERSION);
25: InputStream s = new BufferedInputStream(this .getClass()
26: .getResource(getFile()).openStream());
27: session.getServer().field("Content-Length",
28: String.valueOf(s.available()));
29: session.getServer().field("Connection", "close");
30: session.getServer().header();
31: int i;
32: while ((i = s.read()) != -1) {
33: Thread.yield();
34: connection.getServer().write(i);
35: }
36: s.close();
37: connection.getServer().flush();
38: String c = (String) getFields().get("connection");
39: if (c != null && c.equalsIgnoreCase("close"))
40: connection.getServer().close();
41: } catch (Exception e) {
42: try {
43: connection.getServer().flush();
44: } catch (Exception ex) {
45: }
46: try {
47: connection.getServer().close();
48: } catch (Exception ex) {
49: }
50: }
51: }
52: }
53:
54: public void end() {
55: super .end();
56: if (getFile() != null)
57: new Thread(this).start();
58: }
59: }
|