|
51 | 51 | */ |
52 | 52 | public abstract class BaseNCodec implements BinaryEncoder, BinaryDecoder { |
53 | 53 |
|
54 | | - /** |
55 | | - * Holds thread context so classes can be thread-safe. |
56 | | - * |
57 | | - * This class is not itself thread-safe; each thread must allocate its own copy. |
58 | | - * |
59 | | - * @since 1.7 |
60 | | - */ |
61 | | - static class Context { |
62 | | - |
63 | | - /** |
64 | | - * Placeholder for the bytes we're dealing with for our based logic. |
65 | | - * Bitwise operations store and extract the encoding or decoding from this variable. |
66 | | - */ |
67 | | - int ibitWorkArea; |
68 | | - |
69 | | - /** |
70 | | - * Placeholder for the bytes we're dealing with for our based logic. |
71 | | - * Bitwise operations store and extract the encoding or decoding from this variable. |
72 | | - */ |
73 | | - long lbitWorkArea; |
74 | | - |
75 | | - /** |
76 | | - * Buffer for streaming. |
77 | | - */ |
78 | | - byte[] buffer; |
79 | | - |
80 | | - /** |
81 | | - * Position where next character should be written in the buffer. |
82 | | - */ |
83 | | - int pos; |
84 | | - |
85 | | - /** |
86 | | - * Position where next character should be read from the buffer. |
87 | | - */ |
88 | | - int readPos; |
89 | | - |
90 | | - /** |
91 | | - * Boolean flag to indicate the EOF has been reached. Once EOF has been reached, this object becomes useless, |
92 | | - * and must be thrown away. |
93 | | - */ |
94 | | - boolean eof; |
95 | | - |
96 | | - /** |
97 | | - * Variable tracks how many characters have been written to the current line. Only used when encoding. We use |
98 | | - * it to make sure each encoded line never goes beyond lineLength (if lineLength > 0). |
99 | | - */ |
100 | | - int currentLinePos; |
101 | | - |
102 | | - /** |
103 | | - * Writes to the buffer only occur after every 3/5 reads when encoding, and every 4/8 reads when decoding. This |
104 | | - * variable helps track that. |
105 | | - */ |
106 | | - int modulus; |
107 | | - |
108 | | - /** |
109 | | - * Returns a String useful for debugging (especially within a debugger.) |
110 | | - * |
111 | | - * @return a String useful for debugging. |
112 | | - */ |
113 | | - @SuppressWarnings("boxing") // OK to ignore boxing here |
114 | | - @Override |
115 | | - public String toString() { |
116 | | - return String.format("%s[buffer=%s, currentLinePos=%s, eof=%s, ibitWorkArea=%s, lbitWorkArea=%s, " + |
117 | | - "modulus=%s, pos=%s, readPos=%s]", this.getClass().getSimpleName(), Arrays.toString(buffer), |
118 | | - currentLinePos, eof, ibitWorkArea, lbitWorkArea, modulus, pos, readPos); |
119 | | - } |
120 | | - } |
121 | | - |
122 | 54 | /** |
123 | 55 | * EOF |
124 | 56 | * |
@@ -191,6 +123,122 @@ public String toString() { |
191 | 123 | */ |
192 | 124 | static final byte[] CHUNK_SEPARATOR = {'\r', '\n'}; |
193 | 125 |
|
| 126 | + /** |
| 127 | + * @deprecated Use {@link #pad}. Will be removed in 2.0. |
| 128 | + */ |
| 129 | + @Deprecated |
| 130 | + protected final byte PAD = PAD_DEFAULT; // instance variable just in case it needs to vary later |
| 131 | + |
| 132 | + /** Pad byte. Instance variable just in case it needs to vary later. */ |
| 133 | + protected final byte pad; |
| 134 | + |
| 135 | + /** Number of bytes in each full block of unencoded data, e.g. 4 for Base64 and 5 for Base32 */ |
| 136 | + private final int unencodedBlockSize; |
| 137 | + |
| 138 | + /** Number of bytes in each full block of encoded data, e.g. 3 for Base64 and 8 for Base32 */ |
| 139 | + private final int encodedBlockSize; |
| 140 | + |
| 141 | + /** |
| 142 | + * Chunksize for encoding. Not used when decoding. |
| 143 | + * A value of zero or less implies no chunking of the encoded data. |
| 144 | + * Rounded down to the nearest multiple of encodedBlockSize. |
| 145 | + */ |
| 146 | + protected final int lineLength; |
| 147 | + |
| 148 | + /** |
| 149 | + * Size of chunk separator. Not used unless {@link #lineLength} > 0. |
| 150 | + */ |
| 151 | + private final int chunkSeparatorLength; |
| 152 | + |
| 153 | + /** |
| 154 | + * Defines the decoding behavior when the input bytes contain leftover trailing bits that |
| 155 | + * cannot be created by a valid encoding. These can be bits that are unused from the final |
| 156 | + * character or entire characters. The default mode is lenient decoding. Set this to |
| 157 | + * {@code true} to enable strict decoding. |
| 158 | + * <ul> |
| 159 | + * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. |
| 160 | + * The remainder are discarded. |
| 161 | + * <li>Strict: The decoding will raise an {@link IllegalArgumentException} if trailing bits |
| 162 | + * are not part of a valid encoding. Any unused bits from the final character must |
| 163 | + * be zero. Impossible counts of entire final characters are not allowed. |
| 164 | + * </ul> |
| 165 | + * <p> |
| 166 | + * When strict decoding is enabled it is expected that the decoded bytes will be re-encoded |
| 167 | + * to a byte array that matches the original, i.e. no changes occur on the final |
| 168 | + * character. This requires that the input bytes use the same padding and alphabet |
| 169 | + * as the encoder. |
| 170 | + * </p> |
| 171 | + */ |
| 172 | + private final CodecPolicy decodingPolicy; |
| 173 | + |
| 174 | + /** |
| 175 | + * Holds thread context so classes can be thread-safe. |
| 176 | + * |
| 177 | + * This class is not itself thread-safe; each thread must allocate its own copy. |
| 178 | + * |
| 179 | + * @since 1.7 |
| 180 | + */ |
| 181 | + static class Context { |
| 182 | + |
| 183 | + /** |
| 184 | + * Placeholder for the bytes we're dealing with for our based logic. |
| 185 | + * Bitwise operations store and extract the encoding or decoding from this variable. |
| 186 | + */ |
| 187 | + int ibitWorkArea; |
| 188 | + |
| 189 | + /** |
| 190 | + * Placeholder for the bytes we're dealing with for our based logic. |
| 191 | + * Bitwise operations store and extract the encoding or decoding from this variable. |
| 192 | + */ |
| 193 | + long lbitWorkArea; |
| 194 | + |
| 195 | + /** |
| 196 | + * Buffer for streaming. |
| 197 | + */ |
| 198 | + byte[] buffer; |
| 199 | + |
| 200 | + /** |
| 201 | + * Position where next character should be written in the buffer. |
| 202 | + */ |
| 203 | + int pos; |
| 204 | + |
| 205 | + /** |
| 206 | + * Position where next character should be read from the buffer. |
| 207 | + */ |
| 208 | + int readPos; |
| 209 | + |
| 210 | + /** |
| 211 | + * Boolean flag to indicate the EOF has been reached. Once EOF has been reached, this object becomes useless, |
| 212 | + * and must be thrown away. |
| 213 | + */ |
| 214 | + boolean eof; |
| 215 | + |
| 216 | + /** |
| 217 | + * Variable tracks how many characters have been written to the current line. Only used when encoding. We use |
| 218 | + * it to make sure each encoded line never goes beyond lineLength (if lineLength > 0). |
| 219 | + */ |
| 220 | + int currentLinePos; |
| 221 | + |
| 222 | + /** |
| 223 | + * Writes to the buffer only occur after every 3/5 reads when encoding, and every 4/8 reads when decoding. This |
| 224 | + * variable helps track that. |
| 225 | + */ |
| 226 | + int modulus; |
| 227 | + |
| 228 | + /** |
| 229 | + * Returns a String useful for debugging (especially within a debugger.) |
| 230 | + * |
| 231 | + * @return a String useful for debugging. |
| 232 | + */ |
| 233 | + @SuppressWarnings("boxing") // OK to ignore boxing here |
| 234 | + @Override |
| 235 | + public String toString() { |
| 236 | + return String.format("%s[buffer=%s, currentLinePos=%s, eof=%s, ibitWorkArea=%s, lbitWorkArea=%s, " + |
| 237 | + "modulus=%s, pos=%s, readPos=%s]", this.getClass().getSimpleName(), Arrays.toString(buffer), |
| 238 | + currentLinePos, eof, ibitWorkArea, lbitWorkArea, modulus, pos, readPos); |
| 239 | + } |
| 240 | + } |
| 241 | + |
194 | 242 | /** |
195 | 243 | * Create a positive capacity at least as large the minimum required capacity. |
196 | 244 | * If the minimum capacity is negative then this throws an OutOfMemoryError as no array |
@@ -269,54 +317,6 @@ private static byte[] resizeBuffer(final Context context, final int minCapacity) |
269 | 317 | return b; |
270 | 318 | } |
271 | 319 |
|
272 | | - /** |
273 | | - * @deprecated Use {@link #pad}. Will be removed in 2.0. |
274 | | - */ |
275 | | - @Deprecated |
276 | | - protected final byte PAD = PAD_DEFAULT; // instance variable just in case it needs to vary later |
277 | | - |
278 | | - /** Pad byte. Instance variable just in case it needs to vary later. */ |
279 | | - protected final byte pad; |
280 | | - |
281 | | - /** Number of bytes in each full block of unencoded data, e.g. 4 for Base64 and 5 for Base32 */ |
282 | | - private final int unencodedBlockSize; |
283 | | - |
284 | | - /** Number of bytes in each full block of encoded data, e.g. 3 for Base64 and 8 for Base32 */ |
285 | | - private final int encodedBlockSize; |
286 | | - |
287 | | - /** |
288 | | - * Chunksize for encoding. Not used when decoding. |
289 | | - * A value of zero or less implies no chunking of the encoded data. |
290 | | - * Rounded down to the nearest multiple of encodedBlockSize. |
291 | | - */ |
292 | | - protected final int lineLength; |
293 | | - |
294 | | - /** |
295 | | - * Size of chunk separator. Not used unless {@link #lineLength} > 0. |
296 | | - */ |
297 | | - private final int chunkSeparatorLength; |
298 | | - |
299 | | - /** |
300 | | - * Defines the decoding behavior when the input bytes contain leftover trailing bits that |
301 | | - * cannot be created by a valid encoding. These can be bits that are unused from the final |
302 | | - * character or entire characters. The default mode is lenient decoding. Set this to |
303 | | - * {@code true} to enable strict decoding. |
304 | | - * <ul> |
305 | | - * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. |
306 | | - * The remainder are discarded. |
307 | | - * <li>Strict: The decoding will raise an {@link IllegalArgumentException} if trailing bits |
308 | | - * are not part of a valid encoding. Any unused bits from the final character must |
309 | | - * be zero. Impossible counts of entire final characters are not allowed. |
310 | | - * </ul> |
311 | | - * <p> |
312 | | - * When strict decoding is enabled it is expected that the decoded bytes will be re-encoded |
313 | | - * to a byte array that matches the original, i.e. no changes occur on the final |
314 | | - * character. This requires that the input bytes use the same padding and alphabet |
315 | | - * as the encoder. |
316 | | - * </p> |
317 | | - */ |
318 | | - private final CodecPolicy decodingPolicy; |
319 | | - |
320 | 320 | /** |
321 | 321 | * Note {@code lineLength} is rounded down to the nearest multiple of the encoded block size. |
322 | 322 | * If {@code chunkSeparatorLength} is zero, then chunking is disabled. |
|
0 commit comments