1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.compression;
17
18 import io.netty.util.internal.PlatformDependent;
19 import io.netty.util.internal.SystemPropertyUtil;
20 import io.netty.util.internal.logging.InternalLogger;
21 import io.netty.util.internal.logging.InternalLoggerFactory;
22
23
24
25
26 public final class ZlibCodecFactory {
27 private static final InternalLogger logger = InternalLoggerFactory.getInstance(ZlibCodecFactory.class);
28
29 private static final int DEFAULT_JDK_WINDOW_SIZE = 15;
30 private static final int DEFAULT_JDK_MEM_LEVEL = 8;
31
32 private static final boolean noJdkZlibDecoder;
33 private static final boolean noJdkZlibEncoder;
34 private static final boolean supportsWindowSizeAndMemLevel;
35
36 static {
37 noJdkZlibDecoder = SystemPropertyUtil.getBoolean("io.netty.noJdkZlibDecoder",
38 PlatformDependent.javaVersion() < 7);
39 logger.debug("-Dio.netty.noJdkZlibDecoder: {}", noJdkZlibDecoder);
40
41 noJdkZlibEncoder = SystemPropertyUtil.getBoolean("io.netty.noJdkZlibEncoder", false);
42 logger.debug("-Dio.netty.noJdkZlibEncoder: {}", noJdkZlibEncoder);
43
44 supportsWindowSizeAndMemLevel = noJdkZlibDecoder || PlatformDependent.javaVersion() >= 7;
45 }
46
47
48
49
50 public static boolean isSupportingWindowSizeAndMemLevel() {
51 return supportsWindowSizeAndMemLevel;
52 }
53
54 public static ZlibEncoder newZlibEncoder(int compressionLevel) {
55 if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder) {
56 return new JZlibEncoder(compressionLevel);
57 } else {
58 return new JdkZlibEncoder(compressionLevel);
59 }
60 }
61
62 public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper) {
63 if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder) {
64 return new JZlibEncoder(wrapper);
65 } else {
66 return new JdkZlibEncoder(wrapper);
67 }
68 }
69
70 public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper, int compressionLevel) {
71 if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder) {
72 return new JZlibEncoder(wrapper, compressionLevel);
73 } else {
74 return new JdkZlibEncoder(wrapper, compressionLevel);
75 }
76 }
77
78 public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper, int compressionLevel, int windowBits, int memLevel) {
79 if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder ||
80 windowBits != DEFAULT_JDK_WINDOW_SIZE || memLevel != DEFAULT_JDK_MEM_LEVEL) {
81 return new JZlibEncoder(wrapper, compressionLevel, windowBits, memLevel);
82 } else {
83 return new JdkZlibEncoder(wrapper, compressionLevel);
84 }
85 }
86
87 public static ZlibEncoder newZlibEncoder(byte[] dictionary) {
88 if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder) {
89 return new JZlibEncoder(dictionary);
90 } else {
91 return new JdkZlibEncoder(dictionary);
92 }
93 }
94
95 public static ZlibEncoder newZlibEncoder(int compressionLevel, byte[] dictionary) {
96 if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder) {
97 return new JZlibEncoder(compressionLevel, dictionary);
98 } else {
99 return new JdkZlibEncoder(compressionLevel, dictionary);
100 }
101 }
102
103 public static ZlibEncoder newZlibEncoder(int compressionLevel, int windowBits, int memLevel, byte[] dictionary) {
104 if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder ||
105 windowBits != DEFAULT_JDK_WINDOW_SIZE || memLevel != DEFAULT_JDK_MEM_LEVEL) {
106 return new JZlibEncoder(compressionLevel, windowBits, memLevel, dictionary);
107 } else {
108 return new JdkZlibEncoder(compressionLevel, dictionary);
109 }
110 }
111
112 public static ZlibDecoder newZlibDecoder() {
113 if (PlatformDependent.javaVersion() < 7 || noJdkZlibDecoder) {
114 return new JZlibDecoder();
115 } else {
116 return new JdkZlibDecoder(true);
117 }
118 }
119
120 public static ZlibDecoder newZlibDecoder(ZlibWrapper wrapper) {
121 if (PlatformDependent.javaVersion() < 7 || noJdkZlibDecoder) {
122 return new JZlibDecoder(wrapper);
123 } else {
124 return new JdkZlibDecoder(wrapper, true);
125 }
126 }
127
128 public static ZlibDecoder newZlibDecoder(byte[] dictionary) {
129 if (PlatformDependent.javaVersion() < 7 || noJdkZlibDecoder) {
130 return new JZlibDecoder(dictionary);
131 } else {
132 return new JdkZlibDecoder(dictionary);
133 }
134 }
135
136 private ZlibCodecFactory() {
137
138 }
139 }