Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
8272249: Better properties of loaded Properties
Browse files Browse the repository at this point in the history
Reviewed-by: bae
Backport-of: f9f7e5eaf51cd4793805e50f8ba3549689d939e9
  • Loading branch information
Yuri Nesterenko committed Jul 19, 2022
1 parent 0370988 commit fdb2030
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions src/java.base/share/classes/jdk/internal/util/xml/impl/Parser.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -30,6 +30,7 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import jdk.internal.org.xml.sax.InputSource;
Expand All @@ -42,7 +43,11 @@ public abstract class Parser {

public static final String FAULT = "";
protected static final int BUFFSIZE_READER = 512;
// Initial buffer (mBuff) size
protected static final int BUFFSIZE_PARSER = 128;
// Max buffer size
private static final int MAX_ARRAY_SIZE = 1024 << 16;

/**
* The end of stream character.
*/
Expand Down Expand Up @@ -525,6 +530,10 @@ private void dtd() throws Exception {
mPh = PH_DTD; // DTD
for (short st = 0; st >= 0;) {
ch = getch();
// report error if EOS is reached while parsing the DTD
if (ch == EOS) {
panic(FAULT);
}
switch (st) {
case 0: // read the document type name
if (chtyp(ch) != ' ') {
Expand Down Expand Up @@ -1664,6 +1673,10 @@ private void cdat()
mBuffIdx = -1;
for (short st = 0; st >= 0;) {
ch = getch();
// report error if EOS is reached while parsing the DTD
if (ch == EOS) {
panic(FAULT);
}
switch (st) {
case 0: // the first '[' of the CDATA open
if (ch == '[') {
Expand Down Expand Up @@ -1871,7 +1884,7 @@ protected String eqstr(char flag) throws Exception {
}

/**
* Resoves an entity.
* Resolves an entity.
*
* This method resolves built-in and character entity references. It is also
* reports external entities to the application.
Expand Down Expand Up @@ -2529,7 +2542,7 @@ private char bkeyword()
}

/**
* Reads a single or double quotted string in to the buffer.
* Reads a single or double quoted string into the buffer.
*
* This method resolves entities inside a string unless the parser parses
* DTD.
Expand Down Expand Up @@ -2664,7 +2677,7 @@ protected abstract void bflash_ws()
* @param ch The character to append to the buffer.
* @param mode The normalization mode.
*/
private void bappend(char ch, char mode) {
private void bappend(char ch, char mode) throws Exception {
// This implements attribute value normalization as
// described in the XML specification [#3.3.3].
switch (mode) {
Expand Down Expand Up @@ -2714,16 +2727,9 @@ private void bappend(char ch, char mode) {
*
* @param ch The character to append to the buffer.
*/
private void bappend(char ch) {
try {
mBuff[++mBuffIdx] = ch;
} catch (Exception exp) {
// Double the buffer size
char buff[] = new char[mBuff.length << 1];
System.arraycopy(mBuff, 0, buff, 0, mBuff.length);
mBuff = buff;
mBuff[mBuffIdx] = ch;
}
private void bappend(char ch) throws Exception {
ensureCapacity(++mBuffIdx);
mBuff[mBuffIdx] = ch;
}

/**
Expand All @@ -2733,14 +2739,9 @@ private void bappend(char ch) {
* @param cidx The character buffer (mChars) start index.
* @param bidx The parser buffer (mBuff) start index.
*/
private void bcopy(int cidx, int bidx) {
private void bcopy(int cidx, int bidx) throws Exception {
int length = mChIdx - cidx;
if ((bidx + length + 1) >= mBuff.length) {
// Expand the buffer
char buff[] = new char[mBuff.length + length];
System.arraycopy(mBuff, 0, buff, 0, mBuff.length);
mBuff = buff;
}
ensureCapacity(bidx + length + 1);
System.arraycopy(mChars, cidx, mBuff, bidx, length);
mBuffIdx += length;
}
Expand Down Expand Up @@ -3327,7 +3328,7 @@ protected char chtyp(char ch) {
}

/**
* Retrives the next character in the document.
* Retrieves the next character in the document.
*
* @return The next character in the document.
*/
Expand Down Expand Up @@ -3433,4 +3434,23 @@ protected Pair del(Pair pair) {

return next;
}

private void ensureCapacity(int minCapacity) throws Exception {
if (mBuff == null) {
int newCapacity = minCapacity > BUFFSIZE_PARSER ?
minCapacity + BUFFSIZE_PARSER : BUFFSIZE_PARSER;
mBuff = new char[newCapacity];
return;
}

if (mBuff.length <= minCapacity) {
int size = mBuff.length << 1;
int newCapacity = size > minCapacity ? size : minCapacity + BUFFSIZE_PARSER;
if (newCapacity < 0 || newCapacity > MAX_ARRAY_SIZE) {
panic(FAULT);
}

mBuff = Arrays.copyOf(mBuff, newCapacity);
}
}
}

0 comments on commit fdb2030

Please sign in to comment.