📄 gzipfilter.java
字号:
_contentLength=-1; } public void resetBuffer() { super.resetBuffer(); if (_gzStream!=null) _gzStream.resetBuffer(); _writer=null; _gzStream=null; } public void sendError(int sc, String msg) throws IOException { resetBuffer(); super.sendError(sc,msg); } public void sendError(int sc) throws IOException { resetBuffer(); super.sendError(sc); } public void sendRedirect(String location) throws IOException { resetBuffer(); super.sendRedirect(location); } public ServletOutputStream getOutputStream() throws IOException { if (_gzStream==null) { if (getResponse().isCommitted() || _noGzip) return getResponse().getOutputStream(); _gzStream=newGzipStream(_request,(HttpServletResponse)getResponse(),_contentLength,_bufferSize,_minGzipSize); } else if (_writer!=null) throw new IllegalStateException("getWriter() called"); return _gzStream; } public PrintWriter getWriter() throws IOException { if (_writer==null) { if (_gzStream!=null) throw new IllegalStateException("getOutputStream() called"); if (getResponse().isCommitted() || _noGzip) return getResponse().getWriter(); _gzStream=newGzipStream(_request,(HttpServletResponse)getResponse(),_contentLength,_bufferSize,_minGzipSize); String encoding = getCharacterEncoding(); _writer=encoding==null?new PrintWriter(_gzStream):new PrintWriter(new OutputStreamWriter(_gzStream,encoding)); } return _writer; } void noGzip() { _noGzip=true; if (_gzStream!=null) { try { _gzStream.doNotGzip(); } catch (IOException e) { throw new IllegalStateException(); } } } void finish() throws IOException { if (_writer!=null) _writer.flush(); if (_gzStream!=null) _gzStream.finish(); } protected GzipStream newGzipStream(HttpServletRequest request,HttpServletResponse response,long contentLength,int bufferSize, int minGzipSize) throws IOException { return new GzipStream(request,response,contentLength,bufferSize,minGzipSize); } } public static class GzipStream extends ServletOutputStream { protected HttpServletRequest _request; protected HttpServletResponse _response; protected OutputStream _out; protected ByteArrayOutputStream2 _bOut; protected GZIPOutputStream _gzOut; protected boolean _closed; protected int _bufferSize; protected int _minGzipSize; protected long _contentLength; public GzipStream(HttpServletRequest request,HttpServletResponse response,long contentLength,int bufferSize, int minGzipSize) throws IOException { _request=request; _response=response; _contentLength=contentLength; _bufferSize=bufferSize; _minGzipSize=minGzipSize; if (minGzipSize==0) doGzip(); } public void resetBuffer() { _closed=false; _out=null; _bOut=null; if (_gzOut!=null && !_response.isCommitted()) _response.setHeader("Content-Encoding",null); _gzOut=null; } public void setContentLength(long length) { _contentLength=length; } public void flush() throws IOException { if (_out==null || _bOut!=null) { if (_contentLength>0 && _contentLength<_minGzipSize) doNotGzip(); else doGzip(); } _out.flush(); } public void close() throws IOException { if (_request.getAttribute("javax.servlet.include.request_uri")!=null) flush(); else { if (_bOut!=null) { if (_contentLength<0) _contentLength=_bOut.getCount(); if (_contentLength<_minGzipSize) doNotGzip(); else doGzip(); } else if (_out==null) { doNotGzip(); } if (_gzOut!=null) _gzOut.finish(); _out.close(); _closed=true; } } public void finish() throws IOException { if (!_closed) { if (_out==null || _bOut!=null) { if (_contentLength>0 && _contentLength<_minGzipSize) doNotGzip(); else doGzip(); } if (_gzOut!=null) _gzOut.finish(); } } public void write(int b) throws IOException { checkOut(1); _out.write(b); } public void write(byte b[]) throws IOException { checkOut(b.length); _out.write(b); } public void write(byte b[], int off, int len) throws IOException { checkOut(len); _out.write(b,off,len); } protected boolean setContentEncodingGzip() { _response.setHeader("Content-Encoding", "gzip"); return _response.containsHeader("Content-Encoding"); } public void doGzip() throws IOException { if (_gzOut==null) { if (_response.isCommitted()) throw new IllegalStateException(); if (setContentEncodingGzip()) { _out=_gzOut=new GZIPOutputStream(_response.getOutputStream(),_bufferSize); if (_bOut!=null) { _out.write(_bOut.getBuf(),0,_bOut.getCount()); _bOut=null; } } else doNotGzip(); } } public void doNotGzip() throws IOException { if (_gzOut!=null) throw new IllegalStateException(); if (_out==null || _bOut!=null ) { _out=_response.getOutputStream(); if (_contentLength>=0) { if(_contentLength<Integer.MAX_VALUE) _response.setContentLength((int)_contentLength); else _response.setHeader("Content-Length",Long.toString(_contentLength)); } if (_bOut!=null) _out.write(_bOut.getBuf(),0,_bOut.getCount()); _bOut=null; } } private void checkOut(int length) throws IOException { if (_closed) { new Throwable().printStackTrace(); throw new IOException("CLOSED"); } if (_out==null) { if (_response.isCommitted() || (_contentLength>=0 && _contentLength<_minGzipSize)) doNotGzip(); else if (length>_minGzipSize) doGzip(); else _out=_bOut=new ByteArrayOutputStream2(_bufferSize); } else if (_bOut!=null) { if (_response.isCommitted() || (_contentLength>=0 && _contentLength<_minGzipSize)) doNotGzip(); else if (length>=(_bOut.size()-_bOut.getCount())) doGzip(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -