@@ -136,11 +136,13 @@ public static interface IXHRReceiving {
136
136
}
137
137
138
138
public static String DEFAULT_USER_AGENT = "Java2Script/2.0.2" ;
139
-
139
+
140
140
protected int status ;
141
141
protected String statusText ;
142
142
protected int readyState ;
143
143
144
+ protected String responseType ;
145
+ protected ByteArrayOutputStream responseBAOS ;
144
146
protected String responseText ;
145
147
protected byte [] responseBytes ;
146
148
protected Document responseXML ;
@@ -178,6 +180,59 @@ public int getReadyState() {
178
180
* or an error happens.
179
181
*/
180
182
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
+ }
181
236
return responseText ;
182
237
}
183
238
/**
@@ -186,6 +241,9 @@ public String getResponseText() {
186
241
* or an error happens.
187
242
*/
188
243
public byte [] getResponseBytes () {
244
+ if (responseBytes == null && responseBAOS != null ) {
245
+ responseBytes = responseBAOS .toByteArray ();
246
+ }
189
247
return responseBytes ;
190
248
}
191
249
/**
@@ -197,15 +255,16 @@ public Document getResponseXML() {
197
255
if (responseXML != null ) {
198
256
return responseXML ;
199
257
}
200
- String type = connection . getHeaderField ( "Content-Type" ) ;
258
+ String type = responseType ;
201
259
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 ) {
203
262
DocumentBuilderFactory dbf = DocumentBuilderFactory .newInstance ();
204
263
dbf .setNamespaceAware (true );
205
264
dbf .setAttribute ("http://xml.org/sax/features/namespaces" , Boolean .TRUE );
206
265
try {
207
266
DocumentBuilder db = dbf .newDocumentBuilder ();
208
- ByteArrayInputStream biStream = new ByteArrayInputStream (responseText .getBytes ("utf-8" ));
267
+ ByteArrayInputStream biStream = new ByteArrayInputStream (responseContent .getBytes ("utf-8" ));
209
268
responseXML = db .parse (biStream );
210
269
} catch (Exception e ) {
211
270
e .printStackTrace ();
@@ -361,11 +420,13 @@ public void open(String method, String url, boolean async, String user, String p
361
420
this .url = url ;
362
421
this .user = user ;
363
422
this .password = password ;
423
+ responseType = null ;
424
+ responseBAOS = null ;
364
425
responseText = null ;
365
426
responseBytes = null ;
366
427
responseXML = null ;
367
428
readyState = 1 ;
368
- status = 200 ; // default OK
429
+ status = 0 ; // default OK
369
430
statusText = null ;
370
431
toAbort = false ;
371
432
if (onreadystatechange != null ) {
@@ -504,8 +565,15 @@ private void request() {
504
565
505
566
receiving = initializeReceivingMonitor ();
506
567
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 )];
509
577
int read ;
510
578
while (!toAbort && (read = is .read (buffer )) != -1 ) {
511
579
if (checkAbort ()) return ; // stop receiving anything
@@ -527,56 +595,7 @@ private void request() {
527
595
is .close ();
528
596
activeIS = null ;
529
597
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" );
580
599
readyState = 4 ;
581
600
if (onreadystatechange != null ) {
582
601
onreadystatechange .onLoaded ();
0 commit comments