|
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 | + |
54 | 122 | /** |
55 | 123 | * EOF |
56 | 124 | * |
@@ -123,122 +191,6 @@ public abstract class BaseNCodec implements BinaryEncoder, BinaryDecoder { |
123 | 191 | */ |
124 | 192 | static final byte[] CHUNK_SEPARATOR = {'\r', '\n'}; |
125 | 193 |
|
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 | | - |
242 | 194 | /** |
243 | 195 | * Create a positive capacity at least as large the minimum required capacity. |
244 | 196 | * If the minimum capacity is negative then this throws an OutOfMemoryError as no array |
@@ -311,6 +263,54 @@ private static byte[] resizeBuffer(final Context context, final int minCapacity) |
311 | 263 | return b; |
312 | 264 | } |
313 | 265 |
|
| 266 | + /** |
| 267 | + * @deprecated Use {@link #pad}. Will be removed in 2.0. |
| 268 | + */ |
| 269 | + @Deprecated |
| 270 | + protected final byte PAD = PAD_DEFAULT; // instance variable just in case it needs to vary later |
| 271 | + |
| 272 | + /** Pad byte. Instance variable just in case it needs to vary later. */ |
| 273 | + protected final byte pad; |
| 274 | + |
| 275 | + /** Number of bytes in each full block of unencoded data, e.g. 4 for Base64 and 5 for Base32 */ |
| 276 | + private final int unencodedBlockSize; |
| 277 | + |
| 278 | + /** Number of bytes in each full block of encoded data, e.g. 3 for Base64 and 8 for Base32 */ |
| 279 | + private final int encodedBlockSize; |
| 280 | + |
| 281 | + /** |
| 282 | + * Chunksize for encoding. Not used when decoding. |
| 283 | + * A value of zero or less implies no chunking of the encoded data. |
| 284 | + * Rounded down to the nearest multiple of encodedBlockSize. |
| 285 | + */ |
| 286 | + protected final int lineLength; |
| 287 | + |
| 288 | + /** |
| 289 | + * Size of chunk separator. Not used unless {@link #lineLength} > 0. |
| 290 | + */ |
| 291 | + private final int chunkSeparatorLength; |
| 292 | + |
| 293 | + /** |
| 294 | + * Defines the decoding behavior when the input bytes contain leftover trailing bits that |
| 295 | + * cannot be created by a valid encoding. These can be bits that are unused from the final |
| 296 | + * character or entire characters. The default mode is lenient decoding. Set this to |
| 297 | + * {@code true} to enable strict decoding. |
| 298 | + * <ul> |
| 299 | + * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. |
| 300 | + * The remainder are discarded. |
| 301 | + * <li>Strict: The decoding will raise an {@link IllegalArgumentException} if trailing bits |
| 302 | + * are not part of a valid encoding. Any unused bits from the final character must |
| 303 | + * be zero. Impossible counts of entire final characters are not allowed. |
| 304 | + * </ul> |
| 305 | + * <p> |
| 306 | + * When strict decoding is enabled it is expected that the decoded bytes will be re-encoded |
| 307 | + * to a byte array that matches the original, i.e. no changes occur on the final |
| 308 | + * character. This requires that the input bytes use the same padding and alphabet |
| 309 | + * as the encoder. |
| 310 | + * </p> |
| 311 | + */ |
| 312 | + private final CodecPolicy decodingPolicy; |
| 313 | + |
314 | 314 | /** |
315 | 315 | * Note {@code lineLength} is rounded down to the nearest multiple of the encoded block size. |
316 | 316 | * If {@code chunkSeparatorLength} is zero, then chunking is disabled. |
|
0 commit comments