Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

timob-9205: use CharsetDecoder to decode data. #2259

Merged
merged 4 commits into from about 2 years ago

3 participants

hieupham007 vishalduggal pingwang2011
...rc/java/ti/modules/titanium/network/TiHTTPClient.java
((11 lines not shown))
559 565 try {
560   - responseText = new String(responseData.getBytes(), charset);
561   - } catch (UnsupportedEncodingException e) {
562   - Log.e(LCAT, "Unable to convert to String using charset: " + charset);
563   - } catch (OutOfMemoryError e) {
564   - Log.e(LCAT, "Unable to get response text: out of memory");
  566 + ByteBuffer data = ByteBuffer.wrap(responseData.getBytes());
  567 + CharBuffer b = decoder.decode(data);
  568 + responseText = b.toString();
  569 + } catch (CharacterCodingException e) {
1
pingwang2011 Collaborator
pingwang2011 added a note

Maybe we should catch a general exception instead of CharacterCodingException.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...rc/java/ti/modules/titanium/network/TiHTTPClient.java
((5 lines not shown))
554 559 if (responseData != null && responseText == null) {
555 560 if (charset == null) {
556 561 charset = HTTP.UTF_8;
557 562 }
558 563
  564 + CharsetDecoder decoder = Charset.forName(charset).newDecoder();
1
vishalduggal Collaborator
vishalduggal added a note

This statement can throw
IllegalCharsetNameException - If the given charset name is illegal
UnsupportedCharsetException - If no support for the named charset is available in this instance of the Java virtual machine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
vishalduggal
Collaborator

Code reviewed. REJECTED

vishalduggal vishalduggal commented on the diff
...rc/java/ti/modules/titanium/network/TiHTTPClient.java
((5 lines not shown))
554 559 if (responseData != null && responseText == null) {
555 560 if (charset == null) {
556 561 charset = HTTP.UTF_8;
557 562 }
558 563
559 564 try {
560   - responseText = new String(responseData.getBytes(), charset);
561   - } catch (UnsupportedEncodingException e) {
562   - Log.e(LCAT, "Unable to convert to String using charset: " + charset);
563   - } catch (OutOfMemoryError e) {
564   - Log.e(LCAT, "Unable to get response text: out of memory");
  565 + CharsetDecoder decoder = Charset.forName(charset).newDecoder();
  566 + ByteBuffer data = ByteBuffer.wrap(responseData.getBytes());
  567 + CharBuffer b = decoder.decode(data);
  568 + responseText = b.toString();
  569 + } catch (Exception e) {
1
vishalduggal Collaborator
vishalduggal added a note

This statement will not catch outOfMemory errors. Either include an OOM catcher or catch Throwable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
vishalduggal
Collaborator

Code Reviewed. ACCEPTED

pingwang2011
Collaborator

Code reviewed and functionally tested. The test case in TIMOB-6922 and TIMOB-9205 passed. Accepted

vishalduggal vishalduggal merged commit d37d0b9 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
17 android/modules/network/src/java/ti/modules/titanium/network/TiHTTPClient.java
@@ -18,7 +18,11 @@
18 18 import java.net.MalformedURLException;
19 19 import java.net.URI;
20 20 import java.net.URL;
  21 +import java.nio.ByteBuffer;
  22 +import java.nio.CharBuffer;
  23 +import java.nio.charset.CharacterCodingException;
21 24 import java.nio.charset.Charset;
  25 +import java.nio.charset.CharsetDecoder;
22 26 import java.util.ArrayList;
23 27 import java.util.HashMap;
24 28 import java.util.List;
@@ -551,18 +555,23 @@ public void sendError(String error)
551 555
552 556 public String getResponseText()
553 557 {
  558 +
554 559 if (responseData != null && responseText == null) {
555 560 if (charset == null) {
556 561 charset = HTTP.UTF_8;
557 562 }
558 563
559 564 try {
560   - responseText = new String(responseData.getBytes(), charset);
561   - } catch (UnsupportedEncodingException e) {
562   - Log.e(LCAT, "Unable to convert to String using charset: " + charset);
  565 + CharsetDecoder decoder = Charset.forName(charset).newDecoder();
  566 + ByteBuffer data = ByteBuffer.wrap(responseData.getBytes());
  567 + CharBuffer b = decoder.decode(data);
  568 + responseText = b.toString();
  569 + } catch (Exception e) {
  570 + Log.e(LCAT, "Unable to decode using charset: " + charset);
563 571 } catch (OutOfMemoryError e) {
564   - Log.e(LCAT, "Unable to get response text: out of memory");
  572 + Log.e(LCAT, "Unable to get response text: out of memory");
565 573 }
  574 +
566 575 }
567 576
568 577 return responseText;

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.