Skip to content

Apache Lucene open-source search software

LUCENE-4199: fix more default charset uses

Robert Muird69c73c0277a
diff --git a/lucene/core/src/test/org/apache/lucene/document/TestBinaryDocument.java b/lucene/core/src/test/org/apache/lucene/document/TestBinaryDocument.java
index 45c72cf0617..67d1ad922e4 100644
--- a/lucene/core/src/test/org/apache/lucene/document/TestBinaryDocument.java
+++ b/lucene/core/src/test/org/apache/lucene/document/TestBinaryDocument.java
@@ -37,7 +37,7 @@ public class TestBinaryDocument extends LuceneTestCase {
{
FieldType ft = new FieldType();
ft.setStored(true);
- IndexableField binaryFldStored = new StoredField("binaryStored", binaryValStored.getBytes());
+ IndexableField binaryFldStored = new StoredField("binaryStored", binaryValStored.getBytes("UTF-8"));
IndexableField stringFldStored = new Field("stringStored", binaryValStored, ft);
Document doc = new Document();
@@ -62,7 +62,7 @@ public class TestBinaryDocument extends LuceneTestCase {
/** fetch the binary stored field and compare it's content with the original one */
BytesRef bytes = docFromReader.getBinaryValue("binaryStored");
assertNotNull(bytes);
- String binaryFldStoredTest = new String(bytes.bytes, bytes.offset, bytes.length);
+ String binaryFldStoredTest = new String(bytes.bytes, bytes.offset, bytes.length, "UTF-8");
assertTrue(binaryFldStoredTest.equals(binaryValStored));
/** fetch the string field and compare it's content with the original one */
@@ -75,7 +75,7 @@ public class TestBinaryDocument extends LuceneTestCase {
}
public void testCompressionTools() throws Exception {
- IndexableField binaryFldCompressed = new StoredField("binaryCompressed", CompressionTools.compress(binaryValCompressed.getBytes()));
+ IndexableField binaryFldCompressed = new StoredField("binaryCompressed", CompressionTools.compress(binaryValCompressed.getBytes("UTF-8")));
IndexableField stringFldCompressed = new StoredField("stringCompressed", CompressionTools.compressString(binaryValCompressed));
Document doc = new Document();
@@ -94,7 +94,7 @@ public class TestBinaryDocument extends LuceneTestCase {
assertTrue(docFromReader != null);
/** fetch the binary compressed field and compare it's content with the original one */
- String binaryFldCompressedTest = new String(CompressionTools.decompress(docFromReader.getBinaryValue("binaryCompressed")));
+ String binaryFldCompressedTest = new String(CompressionTools.decompress(docFromReader.getBinaryValue("binaryCompressed")), "UTF-8");
assertTrue(binaryFldCompressedTest.equals(binaryValCompressed));
assertTrue(CompressionTools.decompressString(docFromReader.getBinaryValue("stringCompressed")).equals(binaryValCompressed));
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDoc.java b/lucene/core/src/test/org/apache/lucene/index/TestDoc.java
index b345f82b452..0cf1ab57766 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestDoc.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestDoc.java
@@ -17,11 +17,14 @@ package org.apache.lucene.index;
* limitations under the License.
*/
import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.io.Writer;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
@@ -78,14 +81,14 @@ public class TestDoc extends LuceneTestCase {
}
private File createOutput(String name, String text) throws IOException {
- FileWriter fw = null;
+ Writer fw = null;
PrintWriter pw = null;
try {
File f = new File(workDir, name);
if (f.exists()) f.delete();
- fw = new FileWriter(f);
+ fw = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
pw = new PrintWriter(fw);
pw.println(text);
return f;
@@ -182,9 +185,11 @@ public class TestDoc extends LuceneTestCase {
{
File file = new File(workDir, fileName);
Document doc = new Document();
- doc.add(new TextField("contents", new FileReader(file), Field.Store.NO));
+ InputStreamReader is = new InputStreamReader(new FileInputStream(file), "UTF-8");
+ doc.add(new TextField("contents", is, Field.Store.NO));
writer.addDocument(doc);
writer.commit();
+ is.close();
return writer.newestSegment();
}
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/builders/TestNumericRangeFilterBuilder.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/builders/TestNumericRangeFilterBuilder.java
index 61656d21825..1b7b7aa427e 100644
--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/builders/TestNumericRangeFilterBuilder.java
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/builders/TestNumericRangeFilterBuilder.java
@@ -203,7 +203,7 @@ public class TestNumericRangeFilterBuilder extends LuceneTestCase {
private static Document getDocumentFromString(String str)
throws SAXException, IOException, ParserConfigurationException {
- InputStream is = new ByteArrayInputStream(str.getBytes());
+ InputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
diff --git a/lucene/suggest/src/java/org/apache/lucene/search/spell/PlainTextDictionary.java b/lucene/suggest/src/java/org/apache/lucene/search/spell/PlainTextDictionary.java
index d47ebc7779a..6d7a0c54577 100755
--- a/lucene/suggest/src/java/org/apache/lucene/search/spell/PlainTextDictionary.java
+++ b/lucene/suggest/src/java/org/apache/lucene/search/spell/PlainTextDictionary.java
@@ -38,12 +38,12 @@ public class PlainTextDictionary implements Dictionary {
private BufferedReader in;
- public PlainTextDictionary(File file) throws FileNotFoundException {
- in = new BufferedReader(new FileReader(file));
+ public PlainTextDictionary(File file) throws IOException {
+ in = new BufferedReader(IOUtils.getDecodingReader(file, IOUtils.CHARSET_UTF_8));
}
public PlainTextDictionary(InputStream dictFile) {
- in = new BufferedReader(new InputStreamReader(dictFile));
+ in = new BufferedReader(IOUtils.getDecodingReader(dictFile, IOUtils.CHARSET_UTF_8));
}
/**
diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/FileDictionary.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/FileDictionary.java
index 96b7f8794af..06a9d493ff3 100644
--- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/FileDictionary.java
+++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/FileDictionary.java
@@ -42,7 +42,7 @@ public class FileDictionary implements Dictionary {
private boolean done = false;
public FileDictionary(InputStream dictFile) {
- in = new BufferedReader(new InputStreamReader(dictFile));
+ in = new BufferedReader(IOUtils.getDecodingReader(dictFile, IOUtils.CHARSET_UTF_8));
}
/**