4242 */
4343final class ChunkedInputStream extends InputStream {
4444
45+ /**
46+ * Empty value for checking the result.
47+ */
48+ private static final int EMPTY_VALUE = -1 ;
49+
50+ /**
51+ * Default radix value.
52+ */
53+ private static final int DEFAULT_RADIX = 16 ;
54+
55+ /**
56+ * Double slash value.
57+ */
58+ private static final int DOUBLE_SLASH = '\\' ;
59+
60+ /**
61+ * Quoted string value.
62+ */
63+ private static final int QUOTED_VALUE = '\"' ;
64+
65+ /**
66+ * Next line value.
67+ */
68+ private static final int NEXT_LINE = '\n' ;
69+
70+ /**
71+ * R value.
72+ */
73+ private static final int R_VALUE = '\r' ;
74+
75+ /**
76+ * Semicolon value.
77+ */
78+ private static final int SEMICOLON = ';' ;
79+
80+ /**
81+ * Exception for bad state.
82+ */
83+ private static final String BAD_STATE = "Bad state" ;
84+
85+ /**
86+ * Exception for bad chunk.
87+ */
88+ private static final String BAD_CHUNK_SIZE = "Bad chunk size: %s" ;
89+
90+ /**
91+ * Exception for chunk stream end.
92+ */
93+ private static final String END_OF_STREAM = "chunked stream ended unexpectedly" ;
94+
95+ /**
96+ * Exception for crlf expectation state.
97+ */
98+ private static final String CRLF_EXPECTED = "CRLF expected at end of chunk: " ;
99+
100+ /**
101+ * Exception for protocol violation.
102+ */
103+ private static final String BAD_PROTOCOL = String .format (
104+ "%s%s" ,
105+ "Protocol violation: Unexpected" ,
106+ " single newline character in chunk size"
107+ );
108+
45109 /**
46110 * The inputstream that we're wrapping.
47111 */
@@ -85,7 +149,7 @@ public int read() throws IOException {
85149 }
86150 final int result ;
87151 if (this .eof ) {
88- result = - 1 ;
152+ result = ChunkedInputStream . EMPTY_VALUE ;
89153 } else {
90154 ++this .pos ;
91155 result = this .origin .read ();
@@ -101,7 +165,7 @@ public int read(final byte[] buf, final int off, final int len)
101165 }
102166 final int result ;
103167 if (this .eof ) {
104- result = - 1 ;
168+ result = ChunkedInputStream . EMPTY_VALUE ;
105169 } else {
106170 final int shift = Math .min (len , this .size - this .pos );
107171 final int count = this .origin .read (buf , off , shift );
@@ -137,7 +201,7 @@ private void readCrlf() throws IOException {
137201 throw new IOException (
138202 String .format (
139203 "%s %d%s%d" ,
140- "CRLF expected at end of chunk: " ,
204+ ChunkedInputStream . CRLF_EXPECTED ,
141205 crsymbol ,
142206 "/" ,
143207 lfsymbol
@@ -174,7 +238,7 @@ private static int chunkSize(final InputStream stream)
174238 throws IOException {
175239 final ByteArrayOutputStream baos = ChunkedInputStream .sizeLine (stream );
176240 final String data = baos .toString (Charset .defaultCharset ().name ());
177- final int separator = data .indexOf (';' );
241+ final int separator = data .indexOf (ChunkedInputStream . SEMICOLON );
178242 final Text number = new Trimmed (
179243 new Unchecked <>(
180244 new Ternary <>(
@@ -186,44 +250,20 @@ private static int chunkSize(final InputStream stream)
186250 );
187251 try {
188252 return Integer .parseInt (
189- new UncheckedText (
190- number
191- ).asString (),
192- 16
253+ new UncheckedText (number ).asString (),
254+ ChunkedInputStream .DEFAULT_RADIX
193255 );
194256 } catch (final NumberFormatException ex ) {
195257 throw new IOException (
196258 String .format (
197- "Bad chunk size: %s" ,
259+ ChunkedInputStream . BAD_CHUNK_SIZE ,
198260 baos .toString (Charset .defaultCharset ().name ())
199261 ),
200262 ex
201263 );
202264 }
203265 }
204266
205- /**
206- * Possible states of FSM that used to find chunk size.
207- */
208- private enum State {
209- /**
210- * Normal.
211- */
212- NORMAL ,
213- /**
214- * If \r was scanned.
215- */
216- R ,
217- /**
218- * Inside quoted string.
219- */
220- QUOTED_STRING ,
221- /**
222- * End.
223- */
224- END ;
225- }
226-
227267 /**
228268 * Extract line with chunk size from stream.
229269 * @param stream Input stream.
@@ -251,32 +291,26 @@ private static ByteArrayOutputStream sizeLine(final InputStream stream)
251291 private static State next (final InputStream stream , final State state ,
252292 final ByteArrayOutputStream line ) throws IOException {
253293 final int next = stream .read ();
254- if (next == - 1 ) {
255- throw new IOException ("chunked stream ended unexpectedly" );
294+ if (next == ChunkedInputStream . EMPTY_VALUE ) {
295+ throw new IOException (ChunkedInputStream . END_OF_STREAM );
256296 }
257297 final State result ;
258298 switch (state ) {
259299 case NORMAL :
260300 result = nextNormal (state , line , next );
261301 break ;
262302 case R :
263- if (next == '\n' ) {
303+ if (next == ChunkedInputStream . NEXT_LINE ) {
264304 result = State .END ;
265305 } else {
266- throw new IOException (
267- String .format (
268- "%s%s" ,
269- "Protocol violation: Unexpected" ,
270- " single newline character in chunk size"
271- )
272- );
306+ throw new IOException (ChunkedInputStream .BAD_PROTOCOL );
273307 }
274308 break ;
275309 case QUOTED_STRING :
276310 result = nextQuoted (stream , state , line , next );
277311 break ;
278312 default :
279- throw new IllegalStateException ("Bad state" );
313+ throw new IllegalStateException (ChunkedInputStream . BAD_STATE );
280314 }
281315 return result ;
282316 }
@@ -292,10 +326,10 @@ private static State nextNormal(final State state,
292326 final ByteArrayOutputStream line , final int next ) {
293327 final State result ;
294328 switch (next ) {
295- case '\r' :
329+ case ChunkedInputStream . R_VALUE :
296330 result = State .R ;
297331 break ;
298- case '\"' :
332+ case ChunkedInputStream . QUOTED_VALUE :
299333 result = State .QUOTED_STRING ;
300334 break ;
301335 default :
@@ -321,11 +355,11 @@ private static State nextQuoted(final InputStream stream, final State state,
321355 throws IOException {
322356 final State result ;
323357 switch (next ) {
324- case '\\' :
358+ case ChunkedInputStream . DOUBLE_SLASH :
325359 result = state ;
326360 line .write (stream .read ());
327361 break ;
328- case '\"' :
362+ case ChunkedInputStream . QUOTED_VALUE :
329363 result = State .NORMAL ;
330364 break ;
331365 default :
@@ -335,4 +369,26 @@ private static State nextQuoted(final InputStream stream, final State state,
335369 }
336370 return result ;
337371 }
372+
373+ /**
374+ * Possible states of FSM that used to find chunk size.
375+ */
376+ private enum State {
377+ /**
378+ * Normal.
379+ */
380+ NORMAL ,
381+ /**
382+ * If \r was scanned.
383+ */
384+ R ,
385+ /**
386+ * Inside quoted string.
387+ */
388+ QUOTED_STRING ,
389+ /**
390+ * End.
391+ */
392+ END ;
393+ }
338394}
0 commit comments