Mercurial Hosting > nabble
comparison src/cachingfilter/ProxyServletOutputStream.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 package cachingfilter; | |
2 | |
3 import java.io.IOException; | |
4 import java.io.OutputStream; | |
5 import java.io.ByteArrayOutputStream; | |
6 import javax.servlet.ServletOutputStream; | |
7 | |
8 | |
9 public abstract class ProxyServletOutputStream extends ServletOutputStream { | |
10 private static final int BUFFER_SIZE = 100; | |
11 | |
12 private OutputStream out = new OutputStream() { | |
13 private final ByteArrayOutputStream buf = new ByteArrayOutputStream(); | |
14 | |
15 public void write(int b) throws IOException { | |
16 buf.write(b); | |
17 check(); | |
18 } | |
19 | |
20 public void write(byte b[]) throws IOException { | |
21 buf.write(b); | |
22 check(); | |
23 } | |
24 | |
25 public void write(byte b[], int off, int len) throws IOException { | |
26 buf.write(b,off,len); | |
27 check(); | |
28 } | |
29 | |
30 public void flush() throws IOException { | |
31 setOut(); | |
32 out.flush(); | |
33 } | |
34 | |
35 public void close() throws IOException { | |
36 setOut(); | |
37 out.close(); | |
38 } | |
39 | |
40 void check() throws IOException { | |
41 if( buf.size() >= BUFFER_SIZE ) | |
42 setOut(); | |
43 } | |
44 | |
45 private void setOut() throws IOException { | |
46 out = newOutputStream(); | |
47 out.write(buf.toByteArray()); | |
48 } | |
49 }; | |
50 | |
51 protected abstract OutputStream newOutputStream() throws IOException; | |
52 | |
53 public void write(int b) throws IOException { | |
54 out.write(b); | |
55 } | |
56 | |
57 public void write(byte b[]) throws IOException { | |
58 out.write(b); | |
59 } | |
60 | |
61 public void write(byte b[], int off, int len) throws IOException { | |
62 out.write(b,off,len); | |
63 } | |
64 | |
65 public void flush() throws IOException { | |
66 out.flush(); | |
67 } | |
68 | |
69 public void close() throws IOException { | |
70 out.close(); | |
71 } | |
72 } |