/*************************************************************************** * Copyright (c) junmix 2009. All rights reserved. * Program Name : GZIPResponseStream.java * Description : GZIPレスポンス用ストリームクラス * Version : 1.00 * * Change Record * ------------- ----- --------------- ------------------------------------- * Date Ver. Editor Description * ------------- ----- --------------- ------------------------------------- * 2009-xx-xx 1.00 junmix 新規作成 * **************************************************************************/ package com.junmix.filter; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; /** *

* [概 要] GZIPレスポンス用ストリームクラス。 *

*

* [詳 細] 出力ストリームを拡張してGZIP対応させたストリームクラス。 *

*

* [備 考] *

*

* [環 境] JDK 1.5.0_11 *

*

* Copyright(c) junmix 2009 *

*

* $Revision$ *

* * @author junmix * @since 1.00 */ public class GZIPResponseStream extends ServletOutputStream implements GZIPFilterConst { /** * バイト配列による出力ストリーム。 * * @since 1.00 */ protected ByteArrayOutputStream byteArrayOutputStream = null; /** * GZIP用出力ストリーム。 * * @since 1.00 */ protected GZIPOutputStream gzipOutputStream = null; /** * ストリームのクローズ判定フラグ。 * * @since 1.00 */ protected boolean closed = false; /** * Servletがクライアントに返すレスポンス内容を含むHttpServletResponse。 * * @since 1.00 */ protected HttpServletResponse response = null; /** * 出力ストリーム。 * * @since 1.00 */ protected ServletOutputStream servletOutputStream = null; /** *

* [概 要] コンストラクタ。 *

*

* [詳 細] *

*

* [備 考] *

* * @param response * Servletがクライアントに返すレスポンス内容を含むHttpServletResponse * @throws IOException * 入出力エラー発生時 * @since 1.00 */ public GZIPResponseStream(HttpServletResponse response) throws IOException { super(); this.closed = false; this.response = response; this.servletOutputStream = response.getOutputStream(); this.byteArrayOutputStream = new ByteArrayOutputStream(); this.gzipOutputStream = new GZIPOutputStream(this.byteArrayOutputStream); } /** *

* [概 要] クローズ処理。 *

*

* [詳 細] 出力ストリームをクローズします。 *

*

* [備 考] *

* * @throws IOException * 入出力エラー発生時 * @since 1.00 */ public void close() throws IOException { if (this.closed) { throw new IOException(EXCEPTION_MESSAGE_ALREADY_CLOSED); } this.gzipOutputStream.finish(); byte[] bytes = this.byteArrayOutputStream.toByteArray(); this.response.addHeader(RESPONSE_HEADER_CONTENT_LENGTH, Integer.toString(bytes.length)); this.response.addHeader(RESPONSE_HEADER_CONTENT_ENCODING, ACCEPT_ENCODING_TYPE_GZIP); this.servletOutputStream.write(bytes); this.servletOutputStream.flush(); this.servletOutputStream.close(); this.closed = true; } /** *

* [概 要] フラッシュ処理。 *

*

* [詳 細] GZIP用出力ストリームをフラッシュします。 *

*

* [備 考] *

* * @throws IOException * 入出力エラー発生時 * @since 1.00 */ public void flush() throws IOException { if (this.closed) { throw new IOException(EXCEPTION_MESSAGE_STREAM_FLUSH_FAILURE); } this.gzipOutputStream.flush(); } /** *

* [概 要] 出力ストリームへの書き込み処理。 *

*

* [詳 細] 出力ストリームへの書き込みを行う。 *

*

* [備 考] *

* * @param b * byteデータ * @throws IOException * 入出力エラー発生時 * @since 1.00 */ public void write(int b) throws IOException { if (this.closed) { throw new IOException(EXCEPTION_MESSAGE_STREAM_CLOSE_FAILURE); } this.gzipOutputStream.write((byte)b); } /** *

* [概 要] 出力ストリームへの書き込み処理。 *

*

* [詳 細] 出力ストリームへの書き込みを行う。 *

*

* [備 考] *

* * @param b * データ * @throws IOException * 入出力エラー発生時 * @since 1.00 */ public void write(byte[] b) throws IOException { this.write(b, 0, b.length); } /** *

* [概 要] 出力ストリームへの書き込み処理。 *

*

* [詳 細] 出力ストリームへの書き込みを行う。 *

*

* [備 考] *

* * @param b * データ * @param off * データの開始オフセット * @param len * 書き込むバイト数 * @throws IOException * 入出力エラー発生時 * @since 1.00 */ public void write(byte[] b, int off, int len) throws IOException { if (this.closed) { throw new IOException(EXCEPTION_MESSAGE_STREAM_CLOSE_FAILURE); } this.gzipOutputStream.write(b, off, len); } /** *

* [概 要] ストリームのクローズ判定処理。 *

*

* [詳 細] ストリームがクローズされている場合にtrueを返す。 *

*

* [備 考] *

* * @since 1.00 */ public boolean closed() { return this.closed; } }