30
30
import java .io .InputStreamReader ;
31
31
import java .io .Reader ;
32
32
import java .io .UnsupportedEncodingException ;
33
+ import java .util .Arrays ;
33
34
import java .util .HashMap ;
34
35
import java .util .Map ;
35
36
import jdk .internal .org .xml .sax .InputSource ;
@@ -42,7 +43,11 @@ public abstract class Parser {
42
43
43
44
public static final String FAULT = "" ;
44
45
protected static final int BUFFSIZE_READER = 512 ;
46
+ // Initial buffer (mBuff) size
45
47
protected static final int BUFFSIZE_PARSER = 128 ;
48
+ // Max buffer size
49
+ private static final int MAX_ARRAY_SIZE = 1024 << 16 ;
50
+
46
51
/**
47
52
* The end of stream character.
48
53
*/
@@ -525,6 +530,10 @@ private void dtd() throws Exception {
525
530
mPh = PH_DTD ; // DTD
526
531
for (short st = 0 ; st >= 0 ;) {
527
532
ch = getch ();
533
+ // report error if EOS is reached while parsing the DTD
534
+ if (ch == EOS ) {
535
+ panic (FAULT );
536
+ }
528
537
switch (st ) {
529
538
case 0 : // read the document type name
530
539
if (chtyp (ch ) != ' ' ) {
@@ -1664,6 +1673,10 @@ private void cdat()
1664
1673
mBuffIdx = -1 ;
1665
1674
for (short st = 0 ; st >= 0 ;) {
1666
1675
ch = getch ();
1676
+ // report error if EOS is reached while parsing the DTD
1677
+ if (ch == EOS ) {
1678
+ panic (FAULT );
1679
+ }
1667
1680
switch (st ) {
1668
1681
case 0 : // the first '[' of the CDATA open
1669
1682
if (ch == '[' ) {
@@ -2525,7 +2538,7 @@ private char bkeyword()
2525
2538
}
2526
2539
2527
2540
/**
2528
- * Reads a single or double quotted string in to the buffer.
2541
+ * Reads a single or double quoted string into the buffer.
2529
2542
*
2530
2543
* This method resolves entities inside a string unless the parser parses
2531
2544
* DTD.
@@ -2660,7 +2673,7 @@ protected abstract void bflash_ws()
2660
2673
* @param ch The character to append to the buffer.
2661
2674
* @param mode The normalization mode.
2662
2675
*/
2663
- private void bappend (char ch , char mode ) {
2676
+ private void bappend (char ch , char mode ) throws Exception {
2664
2677
// This implements attribute value normalization as
2665
2678
// described in the XML specification [#3.3.3].
2666
2679
switch (mode ) {
@@ -2710,16 +2723,9 @@ private void bappend(char ch, char mode) {
2710
2723
*
2711
2724
* @param ch The character to append to the buffer.
2712
2725
*/
2713
- private void bappend (char ch ) {
2714
- try {
2715
- mBuff [++mBuffIdx ] = ch ;
2716
- } catch (Exception exp ) {
2717
- // Double the buffer size
2718
- char buff [] = new char [mBuff .length << 1 ];
2719
- System .arraycopy (mBuff , 0 , buff , 0 , mBuff .length );
2720
- mBuff = buff ;
2721
- mBuff [mBuffIdx ] = ch ;
2722
- }
2726
+ private void bappend (char ch ) throws Exception {
2727
+ ensureCapacity (++mBuffIdx );
2728
+ mBuff [mBuffIdx ] = ch ;
2723
2729
}
2724
2730
2725
2731
/**
@@ -2729,14 +2735,9 @@ private void bappend(char ch) {
2729
2735
* @param cidx The character buffer (mChars) start index.
2730
2736
* @param bidx The parser buffer (mBuff) start index.
2731
2737
*/
2732
- private void bcopy (int cidx , int bidx ) {
2738
+ private void bcopy (int cidx , int bidx ) throws Exception {
2733
2739
int length = mChIdx - cidx ;
2734
- if ((bidx + length + 1 ) >= mBuff .length ) {
2735
- // Expand the buffer
2736
- char buff [] = new char [mBuff .length + length ];
2737
- System .arraycopy (mBuff , 0 , buff , 0 , mBuff .length );
2738
- mBuff = buff ;
2739
- }
2740
+ ensureCapacity (bidx + length + 1 );
2740
2741
System .arraycopy (mChars , cidx , mBuff , bidx , length );
2741
2742
mBuffIdx += length ;
2742
2743
}
@@ -3429,4 +3430,23 @@ protected Pair del(Pair pair) {
3429
3430
3430
3431
return next ;
3431
3432
}
3433
+
3434
+ private void ensureCapacity (int minCapacity ) throws Exception {
3435
+ if (mBuff == null ) {
3436
+ int newCapacity = minCapacity > BUFFSIZE_PARSER ?
3437
+ minCapacity + BUFFSIZE_PARSER : BUFFSIZE_PARSER ;
3438
+ mBuff = new char [newCapacity ];
3439
+ return ;
3440
+ }
3441
+
3442
+ if (mBuff .length <= minCapacity ) {
3443
+ int size = mBuff .length << 1 ;
3444
+ int newCapacity = size > minCapacity ? size : minCapacity + BUFFSIZE_PARSER ;
3445
+ if (newCapacity < 0 || newCapacity > MAX_ARRAY_SIZE ) {
3446
+ panic (FAULT );
3447
+ }
3448
+
3449
+ mBuff = Arrays .copyOf (mBuff , newCapacity );
3450
+ }
3451
+ }
3432
3452
}
0 commit comments