KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > core > CacheResponseStream


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.core;
24
25 import java.io.*;
26 import javax.servlet.*;
27 import javax.servlet.http.*;
28
29 /**
30  * Writes the page to both the browser and the cache.
31  *
32  * @see CacheResponseWrapper
33  */

34 public class CacheResponseStream extends ServletOutputStream {
35   ServletOutputStream output;
36   OutputStream cacheOutput;
37
38   /**
39    * Creates a new Stream to write to the original output stream of the response
40    * and to the passed output stream.
41    *
42    * @param response the original response
43    * @param cacheOutput the output for the cache
44    *
45    * @throws IOException if an input or output exception occurred
46    */

47   public CacheResponseStream(HttpServletResponse response,
48       OutputStream cacheOutput) throws IOException {
49     super();
50     this.cacheOutput = cacheOutput;
51     output = response.getOutputStream();
52   }
53
54   /**
55    * Writes to both streams.
56    *
57    * @param b the byte to write
58    * @throws IOException if an I/O error occurs
59    */

60   public void write(int b) throws IOException {
61     output.write(b);
62
63     try {
64       cacheOutput.write(b);
65     } catch (IOException ex) {}
66   }
67
68   /**
69    * Flushes both streams.
70    *
71    * @throws IOException if an I/O error occurs
72    */

73   public void flush() throws IOException {
74     output.flush();
75
76     try {
77       cacheOutput.flush();
78     } catch (IOException ex) {}
79   }
80
81   /**
82    * Closes both streams.
83    *
84    * @throws IOException if an I/O error occurs
85    */

86   public void close() throws IOException {
87     output.close();
88
89     try {
90       cacheOutput.close();
91     } catch (IOException ex) {}
92   }
93 }
94
Popular Tags