From b29c53eb79bf838b93c8a1ebe4bc2045a36b9ba6 Mon Sep 17 00:00:00 2001 From: Daisuke Miyakawa Date: Fri, 21 May 2010 17:01:12 -0700 Subject: [PATCH] Small fix around intermideate charset (ISO-8859-1). After a few investigation, it is found that ISO-8859-1 is confirmed to be the best charset for that use. Modify comment so that we explicitly mention it. Change-Id: I16e487fab33964a1665a1dd6991f2e8598f8895e --- vcard/java/com/android/vcard/VCardConfig.java | 21 ++++++++++++------- .../android/vcard/VCardEntryConstructor.java | 4 ++-- .../android/vcard/VCardParserImpl_V21.java | 20 +++++++----------- .../android/vcard/VCardParserImpl_V30.java | 6 +----- .../com/android/vcard/VCardParser_V21.java | 4 ---- .../com/android/vcard/VCardParser_V30.java | 4 ---- .../test_utils/ContentValuesVerifier.java | 3 +-- .../test_utils/ContentValuesVerifierElem.java | 3 +-- .../vcard/tests/test_utils/VNodeBuilder.java | 16 ++------------ 9 files changed, 27 insertions(+), 54 deletions(-) diff --git a/vcard/java/com/android/vcard/VCardConfig.java b/vcard/java/com/android/vcard/VCardConfig.java index fc95922d9e257..a011d8e279318 100644 --- a/vcard/java/com/android/vcard/VCardConfig.java +++ b/vcard/java/com/android/vcard/VCardConfig.java @@ -43,15 +43,20 @@ public class VCardConfig { * The charset used during import. *

*

- * We cannot determine which charset should be used to interpret a given vCard file - * at first, while we have to decode sime encoded data (e.g. BASE64) to binary. - * In order to avoid "misinterpretation" of charset as much as possible, - * "ISO-8859-1" (a.k.a Latin-1) is first used for reading a stream. - * When charset is specified in a property (with "CHARSET=..." parameter), + * We cannot determine which charset should be used to interpret lines in vCard, + * while Java requires us to specify it when InputStream is used. + * We need to rely on the mechanism due to some performance reason. + *

+ *

+ * In order to avoid "misinterpretation" of charset and lose any data in vCard, + * "ISO-8859-1" is first used for reading the stream. + * When a charset is specified in a property (with "CHARSET=..." parameter), * the string is decoded to raw bytes and encoded into the specific charset, - * assuming "ISO-8859-1" is able to map "all" 8bit characters to some unicode, - * and it has 1 to 1 mapping in all 8bit characters. - * If the assumption is not correct, this setting will cause some bug. + *

+ *

+ * Unicode specification there's a one to one mapping between each byte in ISO-8859-1 + * and a codepoint, and Java specification requires runtime must have the charset. + * Thus, ISO-8859-1 is one effective mapping for intermediate mapping. *

*/ public static final String DEFAULT_INTERMEDIATE_CHARSET = "ISO-8859-1"; diff --git a/vcard/java/com/android/vcard/VCardEntryConstructor.java b/vcard/java/com/android/vcard/VCardEntryConstructor.java index 2679e238adcbe..6cee0704bb18b 100644 --- a/vcard/java/com/android/vcard/VCardEntryConstructor.java +++ b/vcard/java/com/android/vcard/VCardEntryConstructor.java @@ -68,7 +68,7 @@ public class VCardEntryConstructor implements VCardInterpreter { private final List mEntryHandlers = new ArrayList(); public VCardEntryConstructor() { - this(VCardConfig.VCARD_TYPE_V21_GENERIC, null, null, false); + this(VCardConfig.VCARD_TYPE_V21_GENERIC, null); } public VCardEntryConstructor(final int vcardType) { @@ -85,7 +85,7 @@ public class VCardEntryConstructor implements VCardInterpreter { } /** - * @hide + * @hide Just for testing. */ public VCardEntryConstructor(final int vcardType, final Account account, final String inputCharset, final boolean strictLineBreakParsing) { diff --git a/vcard/java/com/android/vcard/VCardParserImpl_V21.java b/vcard/java/com/android/vcard/VCardParserImpl_V21.java index 00ae6c91df2fd..b8343ae8ea5b9 100644 --- a/vcard/java/com/android/vcard/VCardParserImpl_V21.java +++ b/vcard/java/com/android/vcard/VCardParserImpl_V21.java @@ -15,7 +15,6 @@ */ package com.android.vcard; -import android.text.TextUtils; import android.util.Log; import com.android.vcard.exception.VCardAgentNotSupportedException; @@ -64,12 +63,12 @@ import java.util.Set; } } - private static final String sDefaultEncoding = "8BIT"; + private static final String DEFAULT_ENCODING = "8BIT"; protected boolean mCanceled; protected VCardInterpreter mInterpreter; - protected final String mImportCharset; + protected final String mIntermediateCharset; /** *

@@ -136,20 +135,15 @@ import java.util.Set; private long mTimeHandleBase64; public VCardParserImpl_V21() { - this(VCardConfig.VCARD_TYPE_DEFAULT, null); + this(VCardConfig.VCARD_TYPE_DEFAULT); } public VCardParserImpl_V21(int vcardType) { - this(vcardType, null); - } - - public VCardParserImpl_V21(int vcardType, String importCharset) { if ((vcardType & VCardConfig.FLAG_TORELATE_NEST) != 0) { mNestCount = 1; } - mImportCharset = (!TextUtils.isEmpty(importCharset) ? importCharset : - VCardConfig.DEFAULT_INTERMEDIATE_CHARSET); + mIntermediateCharset = VCardConfig.DEFAULT_INTERMEDIATE_CHARSET; } /** @@ -385,7 +379,7 @@ import java.util.Set; * "AGENT" [params] ":" vcard CRLF */ protected boolean parseItem() throws IOException, VCardException { - mCurrentEncoding = sDefaultEncoding; + mCurrentEncoding = DEFAULT_ENCODING; final String line = getNonEmptyLine(); long start = System.currentTimeMillis(); @@ -928,7 +922,7 @@ import java.util.Set; } protected String getDefaultEncoding() { - return sDefaultEncoding; + return DEFAULT_ENCODING; } @@ -938,7 +932,7 @@ import java.util.Set; throw new NullPointerException("InputStream must not be null."); } - final InputStreamReader tmpReader = new InputStreamReader(is, mImportCharset); + final InputStreamReader tmpReader = new InputStreamReader(is, mIntermediateCharset); if (VCardConfig.showPerformanceLog()) { mReader = new CustomBufferedReader(tmpReader); } else { diff --git a/vcard/java/com/android/vcard/VCardParserImpl_V30.java b/vcard/java/com/android/vcard/VCardParserImpl_V30.java index 61d0455983eb2..def149563e114 100644 --- a/vcard/java/com/android/vcard/VCardParserImpl_V30.java +++ b/vcard/java/com/android/vcard/VCardParserImpl_V30.java @@ -46,11 +46,7 @@ import java.util.Set; } public VCardParserImpl_V30(int vcardType) { - super(vcardType, null); - } - - public VCardParserImpl_V30(int vcardType, String importCharset) { - super(vcardType, importCharset); + super(vcardType); } @Override diff --git a/vcard/java/com/android/vcard/VCardParser_V21.java b/vcard/java/com/android/vcard/VCardParser_V21.java index 2a5e313266d25..7aa7a822e2a63 100644 --- a/vcard/java/com/android/vcard/VCardParser_V21.java +++ b/vcard/java/com/android/vcard/VCardParser_V21.java @@ -98,10 +98,6 @@ public final class VCardParser_V21 implements VCardParser { mVCardParserImpl = new VCardParserImpl_V21(vcardType); } - public VCardParser_V21(int parseType, String inputCharset) { - mVCardParserImpl = new VCardParserImpl_V21(parseType, null); - } - public void parse(InputStream is, VCardInterpreter interepreter) throws IOException, VCardException { mVCardParserImpl.parse(is, interepreter); diff --git a/vcard/java/com/android/vcard/VCardParser_V30.java b/vcard/java/com/android/vcard/VCardParser_V30.java index 179869b2163dd..475534c42589c 100644 --- a/vcard/java/com/android/vcard/VCardParser_V30.java +++ b/vcard/java/com/android/vcard/VCardParser_V30.java @@ -76,10 +76,6 @@ public class VCardParser_V30 implements VCardParser { mVCardParserImpl = new VCardParserImpl_V30(vcardType); } - public VCardParser_V30(int vcardType, String importCharset) { - mVCardParserImpl = new VCardParserImpl_V30(vcardType, importCharset); - } - public void parse(InputStream is, VCardInterpreter interepreter) throws IOException, VCardException { mVCardParserImpl.parse(is, interepreter); diff --git a/vcard/tests/src/com/android/vcard/tests/test_utils/ContentValuesVerifier.java b/vcard/tests/src/com/android/vcard/tests/test_utils/ContentValuesVerifier.java index 7d6db53f31e63..2b3e3ab4a330f 100644 --- a/vcard/tests/src/com/android/vcard/tests/test_utils/ContentValuesVerifier.java +++ b/vcard/tests/src/com/android/vcard/tests/test_utils/ContentValuesVerifier.java @@ -66,8 +66,7 @@ public class ContentValuesVerifier implements VCardEntryHandler { public void verify(InputStream is, int vCardType, final VCardParser vCardParser) throws IOException, VCardException { - VCardEntryConstructor builder = - new VCardEntryConstructor(vCardType, null, null, false); + VCardEntryConstructor builder = new VCardEntryConstructor(vCardType, null); builder.addEntryHandler(this); try { vCardParser.parse(is, builder); diff --git a/vcard/tests/src/com/android/vcard/tests/test_utils/ContentValuesVerifierElem.java b/vcard/tests/src/com/android/vcard/tests/test_utils/ContentValuesVerifierElem.java index ecf4a2b69432d..6c09693b1f047 100644 --- a/vcard/tests/src/com/android/vcard/tests/test_utils/ContentValuesVerifierElem.java +++ b/vcard/tests/src/com/android/vcard/tests/test_utils/ContentValuesVerifierElem.java @@ -62,8 +62,7 @@ public class ContentValuesVerifierElem { } else { vCardParser = new VCardParser_V21(); } - VCardEntryConstructor builder = - new VCardEntryConstructor(vCardType, null, null, false); + final VCardEntryConstructor builder = new VCardEntryConstructor(vCardType, null); builder.addEntryHandler(mHandler); try { vCardParser.parse(is, builder); diff --git a/vcard/tests/src/com/android/vcard/tests/test_utils/VNodeBuilder.java b/vcard/tests/src/com/android/vcard/tests/test_utils/VNodeBuilder.java index 9b4fe8358a0c0..77a28ad2acc27 100644 --- a/vcard/tests/src/com/android/vcard/tests/test_utils/VNodeBuilder.java +++ b/vcard/tests/src/com/android/vcard/tests/test_utils/VNodeBuilder.java @@ -62,23 +62,11 @@ import java.util.List; private boolean mStrictLineBreakParsing; public VNodeBuilder() { - this(VCardConfig.DEFAULT_INTERMEDIATE_CHARSET, VCardConfig.DEFAULT_IMPORT_CHARSET, false); + this(VCardConfig.DEFAULT_IMPORT_CHARSET, false); } public VNodeBuilder(String targetCharset, boolean strictLineBreakParsing) { - this(null, targetCharset, strictLineBreakParsing); - } - - /** - * @hide sourceCharset is temporal. - */ - public VNodeBuilder(String sourceCharset, String targetCharset, - boolean strictLineBreakParsing) { - if (sourceCharset != null) { - mSourceCharset = sourceCharset; - } else { - mSourceCharset = VCardConfig.DEFAULT_INTERMEDIATE_CHARSET; - } + mSourceCharset = VCardConfig.DEFAULT_INTERMEDIATE_CHARSET; if (targetCharset != null) { mTargetCharset = targetCharset; } else {