当前位置:网站首页>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.
- 算法面试经典100题,Android程序员最新职业规划
- Appx代码签名指南
- JDBC details
- 广州首个数据安全峰会将在白云区开幕
- beegfs高可用模式探讨
- Pay attention to the partners on the recruitment website of fishing! The monitoring system may have set you as "high risk of leaving"
- Tensorflow2.0 自定义训练的方式求解函数系数
- 数据的同步为每个站点创建触发器同步表
- 方法关键字Deprecated,ExternalProcName,Final,ForceGenerate
猜你喜欢
Reflection and illegalaccessexception exception during application
腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发
理解 YOLOV1 第二篇 预测阶段 非极大值抑制(NMS)
Microservice architecture debate between radical technologists vs Project conservatives
Hudi vs Delta vs Iceberg
[play with Linux] [docker] MySQL installation and configuration
Druid database connection pool details
深度剖析原理,看完这一篇就够了
LeetCode_双指针_中等_61. 旋转链表
Standardized QCI characteristics
随机推荐
Hudi vs Delta vs Iceberg
DaGAN论文解读
【基础架构】Flink/Flink-CDC的部署和配置(MySQL / ES)
Social recruitment interview experience, 2022 latest Android high-frequency selected interview questions sharing
信息系统项目管理师---第八章 项目质量管理
Appx代码签名指南
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
Learning and Exploration - Seamless rotation map
DOM operation
面试突击63:MySQL 中如何去重?
AddressSanitizer 技术初体验
广州首个数据安全峰会将在白云区开幕
Dom 操作
Tensorflow2.0 self defined training method to solve function coefficients
【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
AsyncHandler
Recursive implementation of department tree
LeetCode_ Gray code_ Medium_ 89. Gray code
Cesium 两点之间的直线距离
Systematic and detailed explanation of redis operation hash type data (with source code analysis and test results)