当前位置:网站首页>Period compression filter
Period compression filter
2022-07-06 19:49:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
1. Simple demonstration of compressed sampling points
public class ServletDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
// Find a memory buffer byte stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Compress the data into the buffered byte stream
GZIPOutputStream gout = new GZIPOutputStream(baos);
// Take out the data : Compressed
byte b[] = data.getBytes();// Raw bytes
System.out.println(" Original data size :"+b.length);
gout.write(b);
gout.close();// Ensure that all data enters the memory cache stream
// Take out the compressed data
b = baos.toByteArray();
System.out.println(" Compressed data size :"+b.length);
// Be sure to inform before output client Compression way
response.setHeader("Content-Encoding", "gzip");
response.setContentLength(b.length);// inform client The size of the body
// use server Response object output
ServletOutputStream out = response.getOutputStream();
out.write(b);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
2. A simple demonstration example of the compression filter of the whole station
// Total station compression filter
public class GzipFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
GzipHttpServletResponse gresponse = new GzipHttpServletResponse(response);
chain.doFilter(request, gresponse);// release
// The compression code is written here
// Find a memory buffer byte stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Compress the data into the buffered byte stream
GZIPOutputStream gout = new GZIPOutputStream(baos);
// Take out the data : Compressed
byte b[] = gresponse.getOldBytes();// Raw bytes
System.out.println(" Original data size :"+b.length);
gout.write(b);
gout.close();// Ensure that all data enters the memory cache stream
// Take out the compressed data
b = baos.toByteArray();
System.out.println(" Compressed data size :"+b.length);
// Be sure to inform before output client Compression way
response.setHeader("Content-Encoding", "gzip");
response.setContentLength(b.length);// inform client The size of the body
// use server Response object output
ServletOutputStream out = response.getOutputStream();
out.write(b);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
class GzipHttpServletResponse extends HttpServletResponseWrapper{
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
private PrintWriter pw;
public GzipHttpServletResponse(HttpServletResponse response){
super(response);
}
// Encapsulate the raw data into a buffer stream
@Override
public ServletOutputStream getOutputStream() throws IOException {
return new MyServletOutputStream(baos);
}
// Character stream : Encapsulate the raw data into a buffer stream
@Override
public PrintWriter getWriter() throws IOException {
pw = new PrintWriter(new OutputStreamWriter(baos, super.getCharacterEncoding()));// When characters flow into byte stream, the encoding will be lost
return pw;
}
// return baos Cache data in : original
public byte[] getOldBytes(){
try {
if(pw!=null){
pw.close();
}
baos.flush();
} catch (IOException e) {
e.printStackTrace();
}
return baos.toByteArray();
}
}
class MyServletOutputStream extends ServletOutputStream{
private ByteArrayOutputStream baos;
public MyServletOutputStream(ByteArrayOutputStream baos){
this.baos = baos;
}
@Override
public void write(int b) throws IOException {
baos.write(b);
}
}
Copyright notice : This article is an original blog article , Blog , Without consent , Shall not be reproduced .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117145.html Link to the original text :https://javaforall.cn
边栏推荐
- [translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
- [pytorch] yolov5 train your own data set
- Using clip path to draw irregular graphics
- PHP与EXCEL PHPExcel
- 数据的同步为每个站点创建触发器同步表
- HDU 1026 search pruning problem within the labyrinth of Ignatius and the prince I
- 技术分享 | 抓包分析 TCP 协议
- Logstash expressway entrance
- 测试用里hi
- Web开发小妙招:巧用ThreadLocal规避层层传值
猜你喜欢
Teach you to learn JS prototype and prototype chain hand in hand, a tutorial that monkeys can understand
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
信息系统项目管理师---第八章 项目质量管理
激进技术派 vs 项目保守派的微服务架构之争
Interview assault 63: how to remove duplication in MySQL?
【基础架构】Flink/Flink-CDC的部署和配置(MySQL / ES)
面试突击63:MySQL 中如何去重?
Zero foundation entry polardb-x: build a highly available system and link the big data screen
社招面试心得,2022最新Android高频精选面试题分享
Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法
随机推荐
Transformer model (pytorch code explanation)
Yyds dry goods inventory leetcode question set 751 - 760
力扣101题:对称二叉树
[玩转Linux] [Docker] MySQL安装和配置
Analysis of rainwater connection
MySQL information schema learning (I) -- general table
Systematic and detailed explanation of redis operation hash type data (with source code analysis and test results)
Configuration and simple usage of the EXE backdoor generation tool quasar
思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
Selenium advanced operations
Unbalance balance (dynamic programming, DP)
Spark foundation -scala
js实现力扣71题简化路径
MySQL information schema learning (II) -- InnoDB table
AsyncHandler
leetcode先刷_Maximum Subarray
Chic Lang: attributeerror: partially initialized module 'CV2' has no attribute 'GAPI_ wip_ gst_ GStreamerPipe
Blue Bridge Cup microbial proliferation C language
About image reading and processing, etc
2022年6月语音合成(TTS)和语音识别(ASR)论文月报