@@ -136,11 +136,13 @@ public static interface IXHRReceiving {
136136 }
137137
138138 public static String DEFAULT_USER_AGENT = "Java2Script/2.0.2" ;
139-
139+
140140 protected int status ;
141141 protected String statusText ;
142142 protected int readyState ;
143143
144+ protected String responseType ;
145+ protected ByteArrayOutputStream responseBAOS ;
144146 protected String responseText ;
145147 protected byte [] responseBytes ;
146148 protected Document responseXML ;
@@ -178,6 +180,59 @@ public int getReadyState() {
178180 * or an error happens.
179181 */
180182 public String getResponseText () {
183+ if (responseText != null ) {
184+ return responseText ;
185+ }
186+ ByteArrayOutputStream baos = responseBAOS ;
187+ String type = responseType ;
188+ if (type != null ) {
189+ String charset = null ;
190+ String lowerType = type .toLowerCase ();
191+ int idx = lowerType .indexOf ("charset=" );
192+ if (idx != -1 ) {
193+ charset = type .substring (idx + 8 );
194+ } else {
195+ idx = lowerType .indexOf ("/xml" ); // more xml Content-Type?
196+ if (idx != -1 ) {
197+ String tmp = baos .toString ();
198+ Matcher matcher = Pattern .compile (
199+ "<\\ ?.*encoding\\ s*=\\ s*[\' \" ]([^'\" ]*)[\' \" ].*\\ ?>" ,
200+ Pattern .MULTILINE ).matcher (tmp );
201+ if (matcher .find ()) {
202+ charset = matcher .group (1 );
203+ } else {
204+ // default charset of xml is UTF-8?
205+ responseText = tmp ;
206+ }
207+ } else {
208+ idx = lowerType .indexOf ("html" );
209+ if (idx != -1 ) {
210+ String tmp = baos .toString ();
211+ Matcher matcher = Pattern .compile (
212+ "<meta.*content\\ s*=\\ s*[\' \" ][^'\" ]*charset\\ s*=\\ s*([^'\" ]*)\\ s*[\' \" ].*>" ,
213+ Pattern .MULTILINE | Pattern .CASE_INSENSITIVE ).matcher (tmp );
214+ if (matcher .find ()) {
215+ charset = matcher .group (1 );
216+ } else {
217+ responseText = tmp ;
218+ }
219+ }
220+ }
221+ }
222+ if (charset != null ) {
223+ try {
224+ responseText = baos .toString (charset );
225+ } catch (UnsupportedEncodingException e ) {
226+ }
227+ }
228+ }
229+ if (responseText == null ) {
230+ try {
231+ responseText = baos .toString ("iso-8859-1" );
232+ } catch (UnsupportedEncodingException e ) {
233+ responseText = baos .toString ();
234+ }
235+ }
181236 return responseText ;
182237 }
183238 /**
@@ -186,6 +241,9 @@ public String getResponseText() {
186241 * or an error happens.
187242 */
188243 public byte [] getResponseBytes () {
244+ if (responseBytes == null && responseBAOS != null ) {
245+ responseBytes = responseBAOS .toByteArray ();
246+ }
189247 return responseBytes ;
190248 }
191249 /**
@@ -197,15 +255,16 @@ public Document getResponseXML() {
197255 if (responseXML != null ) {
198256 return responseXML ;
199257 }
200- String type = connection . getHeaderField ( "Content-Type" ) ;
258+ String type = responseType ;
201259 if (type != null && (type .indexOf ("/xml" ) != -1 || type .indexOf ("+xml" ) != -1 )) {
202- if (responseText != null && responseText .length () != 0 ) {
260+ String responseContent = getResponseText ();
261+ if (responseContent != null && responseContent .length () != 0 ) {
203262 DocumentBuilderFactory dbf = DocumentBuilderFactory .newInstance ();
204263 dbf .setNamespaceAware (true );
205264 dbf .setAttribute ("http://xml.org/sax/features/namespaces" , Boolean .TRUE );
206265 try {
207266 DocumentBuilder db = dbf .newDocumentBuilder ();
208- ByteArrayInputStream biStream = new ByteArrayInputStream (responseText .getBytes ("utf-8" ));
267+ ByteArrayInputStream biStream = new ByteArrayInputStream (responseContent .getBytes ("utf-8" ));
209268 responseXML = db .parse (biStream );
210269 } catch (Exception e ) {
211270 e .printStackTrace ();
@@ -361,11 +420,13 @@ public void open(String method, String url, boolean async, String user, String p
361420 this .url = url ;
362421 this .user = user ;
363422 this .password = password ;
423+ responseType = null ;
424+ responseBAOS = null ;
364425 responseText = null ;
365426 responseBytes = null ;
366427 responseXML = null ;
367428 readyState = 1 ;
368- status = 200 ; // default OK
429+ status = 0 ; // default OK
369430 statusText = null ;
370431 toAbort = false ;
371432 if (onreadystatechange != null ) {
@@ -504,8 +565,15 @@ private void request() {
504565
505566 receiving = initializeReceivingMonitor ();
506567
507- ByteArrayOutputStream baos = new ByteArrayOutputStream (10240 );
508- byte [] buffer = new byte [10240 ];
568+ int bufferSize = connection .getContentLength ();
569+ if (bufferSize <= 0 ) {
570+ bufferSize = 10240 ;
571+ } else if (bufferSize > 512 * 1024 ) {
572+ bufferSize = 512 * 1024 ; // buffer increases by 512k
573+ }
574+ ByteArrayOutputStream baos = new ByteArrayOutputStream (bufferSize );
575+ responseBAOS = baos ;
576+ byte [] buffer = new byte [Math .min (bufferSize , 10240 )];
509577 int read ;
510578 while (!toAbort && (read = is .read (buffer )) != -1 ) {
511579 if (checkAbort ()) return ; // stop receiving anything
@@ -527,56 +595,7 @@ private void request() {
527595 is .close ();
528596 activeIS = null ;
529597 responseText = null ;
530- responseBytes = baos .toByteArray ();
531- String type = connection .getHeaderField ("Content-Type" );
532- if (type != null ) {
533- String charset = null ;
534- String lowerType = type .toLowerCase ();
535- int idx = lowerType .indexOf ("charset=" );
536- if (idx != -1 ) {
537- charset = type .substring (idx + 8 );
538- } else {
539- idx = lowerType .indexOf ("/xml" ); // more xml Content-Type?
540- if (idx != -1 ) {
541- String tmp = baos .toString ();
542- Matcher matcher = Pattern .compile (
543- "<\\ ?.*encoding\\ s*=\\ s*[\' \" ]([^'\" ]*)[\' \" ].*\\ ?>" ,
544- Pattern .MULTILINE ).matcher (tmp );
545- if (matcher .find ()) {
546- charset = matcher .group (1 );
547- } else {
548- // default charset of xml is UTF-8?
549- responseText = tmp ;
550- }
551- } else {
552- idx = lowerType .indexOf ("html" );
553- if (idx != -1 ) {
554- String tmp = baos .toString ();
555- Matcher matcher = Pattern .compile (
556- "<meta.*content\\ s*=\\ s*[\' \" ][^'\" ]*charset\\ s*=\\ s*([^'\" ]*)\\ s*[\' \" ].*>" ,
557- Pattern .MULTILINE | Pattern .CASE_INSENSITIVE ).matcher (tmp );
558- if (matcher .find ()) {
559- charset = matcher .group (1 );
560- } else {
561- responseText = tmp ;
562- }
563- }
564- }
565- }
566- if (charset != null ) {
567- try {
568- responseText = baos .toString (charset );
569- } catch (UnsupportedEncodingException e ) {
570- }
571- }
572- }
573- if (responseText == null ) {
574- try {
575- responseText = baos .toString ("iso-8859-1" );
576- } catch (UnsupportedEncodingException e ) {
577- responseText = baos .toString ();
578- }
579- }
598+ responseType = connection .getHeaderField ("Content-Type" );
580599 readyState = 4 ;
581600 if (onreadystatechange != null ) {
582601 onreadystatechange .onLoaded ();
0 commit comments