Mercurial Hosting > luan
comparison src/goodjava/webserver/examples/Chunked.java @ 2008:bba3e529e346 default tip
chunked encoding
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 27 Aug 2025 01:14:17 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2007:408f7dd7e503 | 2008:bba3e529e346 |
---|---|
1 package goodjava.webserver.examples; | |
2 | |
3 import java.io.Writer; | |
4 import java.io.OutputStreamWriter; | |
5 import java.io.IOException; | |
6 import java.util.Date; | |
7 import goodjava.webserver.Handler; | |
8 import goodjava.webserver.Request; | |
9 import goodjava.webserver.Response; | |
10 import goodjava.webserver.ChunkedOutputStream; | |
11 import goodjava.webserver.Server; | |
12 | |
13 | |
14 public class Chunked implements Handler { | |
15 | |
16 public Response handle(Request request) { | |
17 Response response = new Response(); | |
18 response.headers.put( "Content-Type", "text/html; charset=utf-8" ); | |
19 final Writer writer = new OutputStreamWriter( new ChunkedOutputStream(response) ); | |
20 new Thread(new Runnable(){public void run(){ | |
21 try { | |
22 String s = new Date().toString(); | |
23 for( int i=1; i<=10; i++ ) { | |
24 writer.write(s+" "+i+"<br>\n"); | |
25 writer.flush(); | |
26 Thread.sleep(1000); | |
27 } | |
28 writer.close(); | |
29 } catch(IOException e) { | |
30 throw new RuntimeException(e); | |
31 } catch(InterruptedException e) { | |
32 throw new RuntimeException(e); | |
33 } | |
34 }}).start(); | |
35 return response; | |
36 } | |
37 | |
38 } |