Bug 1316008: Use explicit charset encoding r=nechen,sebastian

MozReview-Commit-ID: 3y2CKQZrLtl

--HG--
extra : rebase_source : b75b5f59c7d9e6ef95a06dde982f8a70a352649b
This commit is contained in:
Adrian Zatreanu 2016-11-29 20:42:08 +02:00
Родитель 3539bc390b
Коммит c57580a398
19 изменённых файлов: 82 добавлений и 32 удалений

Просмотреть файл

@ -22,6 +22,7 @@ import java.util.regex.Pattern;
import org.json.JSONObject;
import org.mozilla.gecko.annotation.WrapForJNI;
import org.mozilla.gecko.AppConstants.Versions;
import org.mozilla.gecko.util.StringUtils;
import org.mozilla.gecko.util.ThreadUtils;
import android.content.BroadcastReceiver;
@ -155,7 +156,8 @@ public final class ANRReporter extends BroadcastReceiver
.start();
try {
BufferedReader buf = new BufferedReader(
new InputStreamReader(propProc.getInputStream()), TRACES_LINE_SIZE);
new InputStreamReader(
propProc.getInputStream(), StringUtils.UTF_8), TRACES_LINE_SIZE);
String propVal = buf.readLine();
if (DEBUG) {
Log.d(LOGTAG, "getprop returned " + String.valueOf(propVal));
@ -218,7 +220,8 @@ public final class ANRReporter extends BroadcastReceiver
Log.d(LOGTAG, "trying to match package: " + pkgName);
}
BufferedReader traces = new BufferedReader(
new FileReader(tracesFile), TRACES_BLOCK_SIZE);
new InputStreamReader(new FileInputStream(
tracesFile), StringUtils.UTF_8), TRACES_BLOCK_SIZE);
try {
for (int count = 0; count < LINES_TO_IDENTIFY_TRACES; count++) {
String line = traces.readLine();

Просмотреть файл

@ -2046,10 +2046,10 @@ public class BrowserApp extends GeckoApp
try {
out = new ByteArrayOutputStream();
out.write("data:image/png;base64,".getBytes());
out.write("data:image/png;base64,".getBytes(StringUtils.UTF_8));
b64 = new Base64OutputStream(out, Base64.NO_WRAP);
response.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, b64);
callback.sendSuccess(new String(out.toByteArray()));
callback.sendSuccess(new String(out.toByteArray(), StringUtils.UTF_8));
} catch (IOException e) {
Log.w(LOGTAG, "Failed to convert to base64 data URI");
callback.sendError("Failed to convert favicon to a base64 data URI");

Просмотреть файл

@ -7,6 +7,7 @@ package org.mozilla.gecko;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
@ -20,6 +21,7 @@ import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import org.mozilla.gecko.util.NetworkUtils;
import org.mozilla.gecko.util.StringUtils;
/**
* Use network-based search suggestions.
@ -134,7 +136,7 @@ public class SuggestClient {
private String convertStreamToString(java.io.InputStream is) {
try {
return new java.util.Scanner(is).useDelimiter("\\A").next();
return new java.util.Scanner(new InputStreamReader(is, StringUtils.UTF_8)).useDelimiter("\\A").next();
} catch (java.util.NoSuchElementException e) {
return "";
}

Просмотреть файл

@ -21,6 +21,7 @@ import org.mozilla.gecko.db.BrowserContract.DeletedLogins;
import org.mozilla.gecko.db.BrowserContract.Logins;
import org.mozilla.gecko.db.BrowserContract.LoginsDisabledHosts;
import org.mozilla.gecko.sync.Utils;
import org.mozilla.gecko.util.StringUtils;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
@ -507,7 +508,8 @@ public class LoginsProvider extends SharedBrowserDatabaseProvider {
private String decrypt(@NonNull String initialValue) {
try {
final Cipher cipher = getCipher(Cipher.DECRYPT_MODE);
return new String(cipher.doFinal(Base64.decode(initialValue.getBytes("UTF-8"), Base64.URL_SAFE)));
return new String(cipher.doFinal(Base64.decode(
initialValue.getBytes("UTF-8"), Base64.URL_SAFE)), StringUtils.UTF_8);
} catch (Exception e) {
debug("Decryption failed : " + e);
throw new IllegalStateException("Logins decryption failed", e);

Просмотреть файл

@ -550,7 +550,8 @@ public class BrowserSearch extends HomeFragment
domains = new LinkedHashSet<String>(500);
BufferedReader buf = null;
try {
buf = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.topdomains)));
buf = new BufferedReader(new InputStreamReader(
getResources().openRawResource(R.raw.topdomains), StringUtils.UTF_8));
String res = null;
do {

Просмотреть файл

@ -29,6 +29,8 @@ import android.media.MediaDrmException;
import android.media.NotProvisionedException;
import android.util.Log;
import org.mozilla.gecko.util.StringUtils;
public class GeckoMediaDrmBridgeV21 implements GeckoMediaDrm {
protected final String LOGTAG;
private static final String INVALID_SESSION_ID = "Invalid";
@ -183,7 +185,8 @@ public class GeckoMediaDrmBridgeV21 implements GeckoMediaDrm {
request.getData());
mSessionMIMETypes.put(sessionId, initDataType);
mSessionIds.add(sessionId);
if (DEBUG) Log.d(LOGTAG, " StringID : " + new String(sessionId.array()) + " is put into mSessionIds ");
if (DEBUG) Log.d(LOGTAG, " StringID : " + new String(
sessionId.array(), StringUtils.UTF_8) + " is put into mSessionIds ");
} catch (android.media.NotProvisionedException e) {
if (DEBUG) Log.d(LOGTAG, "Device not provisioned:" + e.getMessage());
if (sessionId != null) {
@ -207,7 +210,7 @@ public class GeckoMediaDrmBridgeV21 implements GeckoMediaDrm {
return;
}
ByteBuffer session = ByteBuffer.wrap(sessionId.getBytes());
ByteBuffer session = ByteBuffer.wrap(sessionId.getBytes(StringUtils.UTF_8));
if (!sessionExists(session)) {
onRejectPromise(promiseId, "Invalid session during updateSession.");
return;
@ -242,7 +245,7 @@ public class GeckoMediaDrmBridgeV21 implements GeckoMediaDrm {
return;
}
ByteBuffer session = ByteBuffer.wrap(sessionId.getBytes());
ByteBuffer session = ByteBuffer.wrap(sessionId.getBytes(StringUtils.UTF_8));
mSessionIds.remove(session);
mDrm.closeSession(session.array());
onSessionClosed(promiseId, session.array());
@ -388,10 +391,10 @@ public class GeckoMediaDrmBridgeV21 implements GeckoMediaDrm {
// No need to handle here if we're not in privacy mode.
break;
case MediaDrm.EVENT_KEY_EXPIRED:
if (DEBUG) Log.d(LOGTAG, "MediaDrm.EVENT_KEY_EXPIRED, sessionId=" + new String(session.array()));
if (DEBUG) Log.d(LOGTAG, "MediaDrm.EVENT_KEY_EXPIRED, sessionId=" + new String(session.array(), StringUtils.UTF_8));
break;
case MediaDrm.EVENT_VENDOR_DEFINED:
if (DEBUG) Log.d(LOGTAG, "MediaDrm.EVENT_VENDOR_DEFINED, sessionId=" + new String(session.array()));
if (DEBUG) Log.d(LOGTAG, "MediaDrm.EVENT_VENDOR_DEFINED, sessionId=" + new String(session.array(), StringUtils.UTF_8));
break;
default:
if (DEBUG) Log.d(LOGTAG, "Invalid DRM event " + event);
@ -466,7 +469,7 @@ public class GeckoMediaDrmBridgeV21 implements GeckoMediaDrm {
int responseCode = urlConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in =
new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), StringUtils.UTF_8));
String inputLine;
StringBuffer response = new StringBuffer();
@ -474,7 +477,7 @@ public class GeckoMediaDrmBridgeV21 implements GeckoMediaDrm {
response.append(inputLine);
}
in.close();
mResponseBody = String.valueOf(response).getBytes();
mResponseBody = String.valueOf(response).getBytes(StringUtils.UTF_8);
if (DEBUG) Log.d(LOGTAG, "Provisioning, response received.");
if (mResponseBody != null) Log.d(LOGTAG, "response length=" + mResponseBody.length);
} else {
@ -600,7 +603,8 @@ public class GeckoMediaDrmBridgeV21 implements GeckoMediaDrm {
final byte [] cryptoSessionId = mCryptoSessionId.array();
mCrypto = new MediaCrypto(mSchemeUUID, cryptoSessionId);
mSessionIds.add(mCryptoSessionId);
if (DEBUG) Log.d(LOGTAG, "MediaCrypto successfully created! - SId " + INVALID_SESSION_ID + ", " + new String(cryptoSessionId));
if (DEBUG) Log.d(LOGTAG, "MediaCrypto successfully created! - SId " + INVALID_SESSION_ID +
", " + new String(cryptoSessionId, StringUtils.UTF_8));
return true;
} else {
if (DEBUG) Log.d(LOGTAG, "Cannot create MediaCrypto for unsupported scheme.");

Просмотреть файл

@ -23,6 +23,7 @@ import org.mozilla.gecko.util.FileUtils;
import org.mozilla.gecko.util.GeckoJarReader;
import org.mozilla.gecko.util.HardwareUtils;
import org.mozilla.gecko.util.RawResource;
import org.mozilla.gecko.util.StringUtils;
import org.mozilla.gecko.util.ThreadUtils;
import org.xmlpull.v1.XmlPullParserException;
@ -393,7 +394,7 @@ public class SearchEngineManager implements SharedPreferences.OnSharedPreference
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("User-Agent", USER_AGENT);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setFixedLengthStreamingMode(message.getBytes().length);
urlConnection.setFixedLengthStreamingMode(message.getBytes(StringUtils.UTF_8).length);
final OutputStream out = urlConnection.getOutputStream();
out.write(message.getBytes());

Просмотреть файл

@ -9,6 +9,7 @@ import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
@ -20,6 +21,12 @@ public class StringUtils {
private static final String FILTER_URL_PREFIX = "filter://";
private static final String USER_ENTERED_URL_PREFIX = "user-entered:";
/**
* The UTF-8 charset.
*/
public static final Charset UTF_8 = Charset.forName("UTF-8");
/*
* This method tries to guess if the given string could be a search query or URL,
* and returns a previous result if there is ambiguity

Просмотреть файл

@ -4,6 +4,7 @@
package org.mozilla.gecko.background.common.log;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedHashSet;
@ -16,6 +17,7 @@ import org.mozilla.gecko.background.common.log.writers.LogWriter;
import org.mozilla.gecko.background.common.log.writers.PrintLogWriter;
import org.mozilla.gecko.background.common.log.writers.SimpleTagLogWriter;
import org.mozilla.gecko.background.common.log.writers.ThreadLocalTagLogWriter;
import org.mozilla.gecko.util.StringUtils;
import android.util.Log;
@ -126,7 +128,8 @@ public class Logger {
*/
public static synchronized void startLoggingToConsole() {
setThreadLogTag("Test");
startLoggingTo(new PrintLogWriter(new PrintWriter(System.out, true)));
startLoggingTo(new PrintLogWriter(new PrintWriter(
new OutputStreamWriter(System.out, StringUtils.UTF_8), true)));
}
// Synchronized version for other classes to use.

Просмотреть файл

@ -149,8 +149,10 @@ public class JSONWebTokenUtils {
if (parts.length != 3) {
return null;
}
String cHeader = new String(Base64.decodeBase64(parts[0]));
String cPayload = new String(Base64.decodeBase64(parts[1]));
String cHeader = new String(Base64.decodeBase64(parts[0]),
org.mozilla.gecko.util.StringUtils.UTF_8);
String cPayload = new String(Base64.decodeBase64(parts[1]),
org.mozilla.gecko.util.StringUtils.UTF_8);
String cSignature = Utils.byte2Hex(Base64.decodeBase64(parts[2]));
ExtendedJSONObject o = new ExtendedJSONObject();
o.put("header", new ExtendedJSONObject(cHeader));
@ -203,8 +205,10 @@ public class JSONWebTokenUtils {
if (parts.length != 3) {
return null;
}
String aHeader = new String(Base64.decodeBase64(parts[0]));
String aPayload = new String(Base64.decodeBase64(parts[1]));
String aHeader = new String(Base64.decodeBase64(parts[0]),
org.mozilla.gecko.util.StringUtils.UTF_8);
String aPayload = new String(Base64.decodeBase64(parts[1]),
org.mozilla.gecko.util.StringUtils.UTF_8);
String aSignature = Utils.byte2Hex(Base64.decodeBase64(parts[2]));
// We do all the assertion parsing *before* dumping the certificate in
// case there's a malformed assertion.

Просмотреть файл

@ -15,6 +15,7 @@ import org.json.simple.JSONArray;
import org.mozilla.apache.commons.codec.binary.Base64;
import org.mozilla.gecko.sync.crypto.CryptoException;
import org.mozilla.gecko.sync.crypto.KeyBundle;
import org.mozilla.gecko.util.StringUtils;
public class CollectionKeys {
private KeyBundle defaultKeyBundle = null;
@ -69,8 +70,8 @@ public class CollectionKeys {
private static JSONArray keyBundleToArray(KeyBundle bundle) {
// Generate JSON.
JSONArray keysArray = new JSONArray();
keysArray.add(new String(Base64.encodeBase64(bundle.getEncryptionKey())));
keysArray.add(new String(Base64.encodeBase64(bundle.getHMACKey())));
keysArray.add(new String(Base64.encodeBase64(bundle.getEncryptionKey()), StringUtils.UTF_8));
keysArray.add(new String(Base64.encodeBase64(bundle.getHMACKey()), StringUtils.UTF_8));
return keysArray;
}

Просмотреть файл

@ -16,6 +16,7 @@ import org.mozilla.gecko.sync.crypto.MissingCryptoInputException;
import org.mozilla.gecko.sync.crypto.NoKeyBundleException;
import org.mozilla.gecko.sync.repositories.domain.Record;
import org.mozilla.gecko.sync.repositories.domain.RecordParseException;
import org.mozilla.gecko.util.StringUtils;
/**
* A Sync crypto record has:
@ -204,7 +205,7 @@ public class CryptoRecord extends Record {
throw new NoKeyBundleException();
}
String cleartext = payload.toJSONString();
byte[] cleartextBytes = cleartext.getBytes("UTF-8");
byte[] cleartextBytes = cleartext.getBytes(StringUtils.UTF_8);
CryptoInfo info = CryptoInfo.encrypt(cleartextBytes, keyBundle);
String message = new String(Base64.encodeBase64(info.getMessage()));
String iv = new String(Base64.encodeBase64(info.getIV()));

Просмотреть файл

@ -31,6 +31,7 @@ import org.mozilla.apache.commons.codec.binary.Base64;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.background.nativecode.NativeCrypto;
import org.mozilla.gecko.sync.setup.Constants;
import org.mozilla.gecko.util.StringUtils;
import android.content.Context;
import android.content.SharedPreferences;
@ -47,7 +48,7 @@ public class Utils {
public static String generateGuid() {
byte[] encodedBytes = Base64.encodeBase64(generateRandomBytes(9), false);
return new String(encodedBytes).replace("+", "-").replace("/", "_");
return new String(encodedBytes, StringUtils.UTF_8).replace("+", "-").replace("/", "_");
}
/**
@ -493,7 +494,7 @@ public class Utils {
try {
fis = context.openFileInput(filename);
isr = new InputStreamReader(fis);
isr = new InputStreamReader(fis, StringUtils.UTF_8);
br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;

Просмотреть файл

@ -18,6 +18,7 @@ import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.NonArrayJSONException;
import org.mozilla.gecko.sync.NonObjectJSONException;
import org.mozilla.gecko.util.StringUtils;
import ch.boye.httpclientandroidlib.Header;
import ch.boye.httpclientandroidlib.HttpEntity;
@ -79,7 +80,7 @@ public class MozResponse {
return null;
}
InputStreamReader is = new InputStreamReader(entity.getContent());
InputStreamReader is = new InputStreamReader(entity.getContent(), StringUtils.UTF_8);
// Oh, Java, you are so evil.
body = new Scanner(is).useDelimiter("\\A").next();
return body;

Просмотреть файл

@ -11,6 +11,7 @@ import java.io.InputStreamReader;
import java.net.URI;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.util.StringUtils;
import ch.boye.httpclientandroidlib.Header;
import ch.boye.httpclientandroidlib.HttpEntity;
@ -103,7 +104,7 @@ public class SyncStorageCollectionRequest extends SyncStorageRequest {
BufferedReader br = null;
try {
content = entity.getContent();
br = new BufferedReader(new InputStreamReader(content), FETCH_BUFFER_SIZE);
br = new BufferedReader(new InputStreamReader(content, StringUtils.UTF_8), FETCH_BUFFER_SIZE);
String line;
// This relies on connection timeouts at the HTTP layer.

Просмотреть файл

@ -6,7 +6,9 @@ package org.mozilla.mozstumbler.service.stumblerthread.datahandling;
import android.content.Context;
import android.util.Log;
import org.mozilla.mozstumbler.service.AppGlobals;
import org.mozilla.mozstumbler.service.utils.StringUtils;
import org.mozilla.mozstumbler.service.utils.Zipper;
import java.io.File;
import java.io.FileInputStream;
@ -101,7 +103,7 @@ public class DataStorageManager {
if (mCurrentReports.reports.size() > 0) {
try {
bytes = Zipper.zipData(finalizeReports(mCurrentReports.reports).getBytes()).length;
bytes = Zipper.zipData(finalizeReports(mCurrentReports.reports).getBytes(StringUtils.UTF_8)).length;
} catch (IOException ex) {
Log.e(LOG_TAG, "Zip error in getQueuedCounts()", ex);
}
@ -301,7 +303,7 @@ public class DataStorageManager {
if (currentReportsCount > 0) {
final String filename = MEMORY_BUFFER_NAME;
final byte[] data = Zipper.zipData(finalizeReports(mCurrentReports.reports).getBytes());
final byte[] data = Zipper.zipData(finalizeReports(mCurrentReports.reports).getBytes(StringUtils.UTF_8));
final int wifiCount = mCurrentReports.wifiCount;
final int cellCount = mCurrentReports.cellCount;
clearCurrentReports();
@ -412,7 +414,7 @@ public class DataStorageManager {
if (mCurrentReports.reports.size() < 1) {
return;
}
final byte[] bytes = Zipper.zipData(finalizeReports(mCurrentReports.reports).getBytes());
final byte[] bytes = Zipper.zipData(finalizeReports(mCurrentReports.reports).getBytes(StringUtils.UTF_8));
saveToDisk(bytes, mCurrentReports.reports.size(), mCurrentReports.wifiCount, mCurrentReports.cellCount);
clearCurrentReports();
}

Просмотреть файл

@ -0,0 +1,15 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.mozstumbler.service.utils;
import java.nio.charset.Charset;
public class StringUtils {
/**
* The UTF-8 charset.
*/
public static final Charset UTF_8 = Charset.forName("UTF-8");
}

Просмотреть файл

@ -33,7 +33,7 @@ public class Zipper {
final ByteArrayInputStream bs = new ByteArrayInputStream(data);
GZIPInputStream gstream = new GZIPInputStream(bs);
try {
InputStreamReader reader = new InputStreamReader(gstream);
InputStreamReader reader = new InputStreamReader(gstream, StringUtils.UTF_8);
BufferedReader in = new BufferedReader(reader);
String read;
while ((read = in.readLine()) != null) {

Просмотреть файл

@ -31,6 +31,7 @@ stumbler_sources = [
'java/org/mozilla/mozstumbler/service/utils/AbstractCommunicator.java',
'java/org/mozilla/mozstumbler/service/utils/NetworkUtils.java',
'java/org/mozilla/mozstumbler/service/utils/PersistentIntentService.java',
'java/org/mozilla/mozstumbler/service/utils/StringUtils.java',
'java/org/mozilla/mozstumbler/service/utils/TelemetryWrapper.java',
'java/org/mozilla/mozstumbler/service/utils/Zipper.java',
]