@@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal
2929LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3030OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3131SOFTWARE.
32- */
32+ */
3333
3434/**
3535 * A JSONTokener takes a source string and extracts characters and tokens from
@@ -39,15 +39,15 @@ of this software and associated documentation files (the "Software"), to deal
3939 * @version 2014-05-03
4040 */
4141public class JSONTokener {
42- /** current read character. */
42+ /** current read character position on the current line . */
4343 private long character ;
4444 /** flag to indicate if the end of the input has been found. */
4545 private boolean eof ;
4646 /** current read index of the input. */
4747 private long index ;
4848 /** current line of the input. */
4949 private long line ;
50- /** previous index of the input. */
50+ /** previous character read from the input. */
5151 private char previous ;
5252 /** Reader for the input. */
5353 private final Reader reader ;
@@ -62,8 +62,8 @@ public class JSONTokener {
6262 */
6363 public JSONTokener (Reader reader ) {
6464 this .reader = reader .markSupported ()
65- ? reader
66- : new BufferedReader (reader );
65+ ? reader
66+ : new BufferedReader (reader );
6767 this .eof = false ;
6868 this .usePrevious = false ;
6969 this .previous = 0 ;
@@ -103,8 +103,8 @@ public void back() throws JSONException {
103103 if (this .usePrevious || this .index <= 0 ) {
104104 throw new JSONException ("Stepping back two steps is not supported" );
105105 }
106- this .index -= 1 ;
107- this .character -= 1 ;
106+ this .index -- ;
107+ this .character -- ;
108108 this .usePrevious = true ;
109109 this .eof = false ;
110110 }
@@ -145,11 +145,23 @@ public boolean end() {
145145 * or backward while checking for more data.
146146 */
147147 public boolean more () throws JSONException {
148- this .next ();
149- if (this .end ()) {
150- return false ;
148+ if (this .usePrevious ) {
149+ return true ;
150+ }
151+ try {
152+ this .reader .mark (1 );
153+ } catch (IOException e ) {
154+ throw new JSONException ("Unable to preserve stream position" , e );
155+ }
156+ try {
157+ if (this .reader .read ()<0 ) {
158+ this .eof = true ;
159+ return false ;
160+ }
161+ this .reader .reset ();
162+ } catch (IOException e ) {
163+ throw new JSONException ("Unable to read the next character from the stream" , e );
151164 }
152- this .back ();
153165 return true ;
154166 }
155167
@@ -174,7 +186,7 @@ public char next() throws JSONException {
174186
175187 if (c <= 0 ) { // End of stream
176188 this .eof = true ;
177- c = 0 ;
189+ return 0 ;
178190 }
179191 }
180192 this .index += 1 ;
@@ -202,8 +214,11 @@ public char next() throws JSONException {
202214 public char next (char c ) throws JSONException {
203215 char n = this .next ();
204216 if (n != c ) {
205- throw this .syntaxError ("Expected '" + c + "' and instead saw '" +
206- n + "'" );
217+ if (n > 0 ) {
218+ throw this .syntaxError ("Expected '" + c + "' and instead saw '" +
219+ n + "'" );
220+ }
221+ throw this .syntaxError ("Expected '" + c + "' and instead saw ''" );
207222 }
208223 return n ;
209224 }
@@ -218,23 +233,23 @@ public char next(char c) throws JSONException {
218233 * Substring bounds error if there are not
219234 * n characters remaining in the source string.
220235 */
221- public String next (int n ) throws JSONException {
222- if (n == 0 ) {
223- return "" ;
224- }
236+ public String next (int n ) throws JSONException {
237+ if (n == 0 ) {
238+ return "" ;
239+ }
225240
226- char [] chars = new char [n ];
227- int pos = 0 ;
241+ char [] chars = new char [n ];
242+ int pos = 0 ;
228243
229- while (pos < n ) {
230- chars [pos ] = this .next ();
231- if (this .end ()) {
232- throw this .syntaxError ("Substring bounds error" );
233- }
234- pos += 1 ;
235- }
236- return new String (chars );
237- }
244+ while (pos < n ) {
245+ chars [pos ] = this .next ();
246+ if (this .end ()) {
247+ throw this .syntaxError ("Substring bounds error" );
248+ }
249+ pos += 1 ;
250+ }
251+ return new String (chars );
252+ }
238253
239254
240255 /**
@@ -378,15 +393,15 @@ public Object nextValue() throws JSONException {
378393 String string ;
379394
380395 switch (c ) {
381- case '"' :
382- case '\'' :
383- return this .nextString (c );
384- case '{' :
385- this .back ();
386- return new JSONObject (this );
387- case '[' :
388- this .back ();
389- return new JSONArray (this );
396+ case '"' :
397+ case '\'' :
398+ return this .nextString (c );
399+ case '{' :
400+ this .back ();
401+ return new JSONObject (this );
402+ case '[' :
403+ this .back ();
404+ return new JSONArray (this );
390405 }
391406
392407 /*
@@ -476,6 +491,6 @@ public JSONException syntaxError(String message, Throwable causedBy) {
476491 @ Override
477492 public String toString () {
478493 return " at " + this .index + " [character " + this .character + " line " +
479- this .line + "]" ;
494+ this .line + "]" ;
480495 }
481496}
0 commit comments