|
10 | 10 |
|
11 | 11 | package org.glassfish.jaxb.runtime.v2.runtime.unmarshaller; |
12 | 12 |
|
| 13 | +import org.glassfish.jaxb.runtime.v2.runtime.output.Pcdata; |
13 | 14 | import org.glassfish.jaxb.runtime.v2.util.FatalAdapter; |
14 | 15 | import org.glassfish.jaxb.core.v2.runtime.unmarshaller.LocatorEx; |
15 | 16 | import org.xml.sax.SAXException; |
@@ -99,8 +100,25 @@ public void text( CharSequence pcdata ) throws SAXException { |
99 | 100 | if(buf.length<len) { |
100 | 101 | buf = new char[len]; |
101 | 102 | } |
102 | | - for( int i=0;i<len; i++ ) |
103 | | - buf[i] = pcdata.charAt(i); // isn't this kinda slow? |
| 103 | + |
| 104 | + // performance optimization: use getChars()/writeTo() if possible |
| 105 | + // note: this will become just pcdata.getChars() once minimum supported JDK version reaches 25 (cf. JDK-8343110) |
| 106 | + if (pcdata instanceof String) { |
| 107 | + ((String) pcdata).getChars(0, len, buf, 0); |
| 108 | + } |
| 109 | + else if (pcdata instanceof StringBuilder) { |
| 110 | + ((StringBuilder) pcdata).getChars(0, len, buf, 0); |
| 111 | + } |
| 112 | + else if (pcdata instanceof StringBuffer) { |
| 113 | + ((StringBuffer) pcdata).getChars(0, len, buf, 0); |
| 114 | + } |
| 115 | + else if (pcdata instanceof Pcdata) { |
| 116 | + ((Pcdata) pcdata).writeTo(buf, 0); |
| 117 | + } |
| 118 | + else { |
| 119 | + for (int i = 0; i < len; i++) |
| 120 | + buf[i] = pcdata.charAt(i); // this is slow due to O(n) range checks |
| 121 | + } |
104 | 122 |
|
105 | 123 | validator.characters(buf,0,len); |
106 | 124 | if(predictor.expectText()) |
|
0 commit comments