You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by el...@apache.org on 2026/05/22 23:14:50 UTC

(mina) branch 2.2.X updated: Added the fluent API for the CompressionFilter class to ease the creation of an instance, as suggested by Piotr

This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch 2.2.X
in repository https://gitbox.apache.org/repos/asf/mina.git


The following commit(s) were added to refs/heads/2.2.X by this push:
     new 94832ef54 Added the fluent API for the CompressionFilter class to ease the creation of an instance, as suggested by Piotr
94832ef54 is described below

commit 94832ef543ded66fa2d8b7e9e2ca1f827d2e663b
Author: Emmanuel Lécharny <em...@worteks.com>
AuthorDate: Sat May 23 01:13:42 2026 +0200

    Added the fluent API for the CompressionFilter class to ease the creation of an instance, as suggested by Piotr
---
 .../mina/filter/compression/CompressionFilter.java | 68 +++++++++++++++++++---
 1 file changed, 60 insertions(+), 8 deletions(-)

diff --git a/mina-filter-compression/src/main/java/org/apache/mina/filter/compression/CompressionFilter.java b/mina-filter-compression/src/main/java/org/apache/mina/filter/compression/CompressionFilter.java
index f6e174771..16b3845bc 100644
--- a/mina-filter-compression/src/main/java/org/apache/mina/filter/compression/CompressionFilter.java
+++ b/mina-filter-compression/src/main/java/org/apache/mina/filter/compression/CompressionFilter.java
@@ -109,17 +109,22 @@ public class CompressionFilter extends IoFilterAdapter {
     private int maxDecompressedSize;
 
     /** Maximum decompression ratio **/
-    private final long maxDecompressRatio;
+    private long maxDecompressRatio;
+
+    public void setMaxDecompressRatio(long maxDecompressRatio) {
+        this.maxDecompressRatio = maxDecompressRatio;
+    }
 
     /** Grace size before decompression ratio check is enforced **/
-    private final long decompressRatioMinSize;
+    private long decompressRatioMinSize;
 
     /**
      * Creates a new instance which compresses outboud data and decompresses
      * inbound data with default compression level.
      */
     public CompressionFilter() {
-        this(true, true, COMPRESSION_DEFAULT, Zlib.MAX_DECOMPRESSED_SIZE, Zlib.MAX_DECOMPRESS_RATIO, Zlib.DECOMPRESS_RATIO_MIN_SIZE);
+        this(true, true, COMPRESSION_DEFAULT, Zlib.MAX_DECOMPRESSED_SIZE, Zlib.MAX_DECOMPRESS_RATIO, 
+                Zlib.DECOMPRESS_RATIO_MIN_SIZE);
     }
 
     /**
@@ -133,7 +138,8 @@ public class CompressionFilter extends IoFilterAdapter {
      *                         {@link #COMPRESSION_NONE}.
      */
     public CompressionFilter(final int compressionLevel) {
-        this(true, true, compressionLevel, Zlib.MAX_DECOMPRESSED_SIZE, Zlib.MAX_DECOMPRESS_RATIO, Zlib.DECOMPRESS_RATIO_MIN_SIZE);
+        this(true, true, compressionLevel, Zlib.MAX_DECOMPRESSED_SIZE, Zlib.MAX_DECOMPRESS_RATIO, 
+                Zlib.DECOMPRESS_RATIO_MIN_SIZE);
     }
 
     /**
@@ -149,7 +155,8 @@ public class CompressionFilter extends IoFilterAdapter {
      */
     public CompressionFilter(final boolean compressInbound, final boolean compressOutbound, 
             final int compressionLevel) {
-        this(compressInbound, compressOutbound, compressionLevel, Zlib.MAX_DECOMPRESSED_SIZE, Zlib.MAX_DECOMPRESS_RATIO, Zlib.DECOMPRESS_RATIO_MIN_SIZE);
+        this(compressInbound, compressOutbound, compressionLevel, Zlib.MAX_DECOMPRESSED_SIZE, 
+                Zlib.MAX_DECOMPRESS_RATIO, Zlib.DECOMPRESS_RATIO_MIN_SIZE);
     }
 
     /**
@@ -169,7 +176,8 @@ public class CompressionFilter extends IoFilterAdapter {
      */
     public CompressionFilter(final boolean compressInbound, final boolean compressOutbound,
             final int compressionLevel, final int maxDecompressedSize) {
-        this(compressInbound, compressOutbound, compressionLevel, maxDecompressedSize, Zlib.MAX_DECOMPRESS_RATIO, Zlib.DECOMPRESS_RATIO_MIN_SIZE);
+        this(compressInbound, compressOutbound, compressionLevel, maxDecompressedSize, Zlib.MAX_DECOMPRESS_RATIO, 
+                Zlib.DECOMPRESS_RATIO_MIN_SIZE);
     }
 
     /**
@@ -273,8 +281,10 @@ public class CompressionFilter extends IoFilterAdapter {
             throw new IllegalStateException("Only one " + CompressionFilter.class + " is permitted.");
         }
 
-        Zlib deflater = new Zlib(compressionLevel, Zlib.MODE_INFLATER, maxDecompressedSize, maxDecompressRatio, decompressRatioMinSize);
-        Zlib inflater = new Zlib(compressionLevel, Zlib.MODE_INFLATER, maxDecompressedSize, maxDecompressRatio, decompressRatioMinSize);
+        Zlib deflater = new Zlib(compressionLevel, Zlib.MODE_INFLATER, maxDecompressedSize, 
+                maxDecompressRatio, decompressRatioMinSize);
+        Zlib inflater = new Zlib(compressionLevel, Zlib.MODE_INFLATER, maxDecompressedSize, 
+                maxDecompressRatio, decompressRatioMinSize);
 
         IoSession session = parent.getSession();
 
@@ -282,6 +292,48 @@ public class CompressionFilter extends IoFilterAdapter {
         session.setAttribute(INFLATER, inflater);
     }
 
+    /**
+     * Set the compression level. On of:
+     * <ul>
+     *   <li>Zlib.COMPRESSION_DEFAULT (-1)</li>
+     *   <li>Zlib.COMPRESSION_NONE (0)</li>
+     *   <li>Zlib.COMPRESSION_MIN (1)</li>
+     *   <li>Zlib.COMPRESSION_MAX (9)</li>
+     * </ul>
+     * 
+     * @param compressionLevel The compression level to set
+     * @ The CompressionFilter instance
+     */
+    public CompressionFilter setCompressionLevel(int compressionLevel) {
+        this.compressionLevel = compressionLevel;
+        
+        return this;
+    }
+
+    /**
+     * Set The maximum decompressed size, to avoid an OOM. Default to 1Mb
+     *
+     * @param maxDecompressedSize The maximum decompressed size
+     * @return The CompressionFilter instance
+     */
+    public CompressionFilter setMaxDecompressedSize(int maxDecompressedSize) {
+        this.maxDecompressedSize = maxDecompressedSize;
+        
+        return this;
+    }
+
+    /**
+     * Grace size before decompression ratio check is enforced. Default to 1Mb?
+     *
+     * @param decompressRatioMinSize The maximum decompressed size before the ratio is checked
+     * @return The CompressionFilter instance
+     */
+    public CompressionFilter setDecompressRatioMinSize(long decompressRatioMinSize) {
+        this.decompressRatioMinSize = decompressRatioMinSize;
+        
+        return this;
+    }
+
     /**
      * @return <code>true</code> if incoming data is being compressed.
      */