Skip to content

Commit

Permalink
Bug 582462 Character encodings for output and input files
Browse files Browse the repository at this point in the history
Tidy up some code and add missing ResultRenderer.

Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=582260
Change-Id: I1567a0b295068b08f87c204197a8a58e01ec6230
  • Loading branch information
ajohnson1 committed Sep 28, 2023
1 parent 5699fe4 commit 4fe0ab9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2021 SAP AG and IBM Corporation.
* Copyright (c) 2008, 2023 SAP AG and IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -14,19 +14,22 @@
package org.eclipse.mat.report.internal;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -36,6 +39,7 @@
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
Expand Down Expand Up @@ -72,8 +76,21 @@ private HtmlArtefact(AbstractPart part, File directory, String relativeURL, Stri
{
this.file = new File(directory, relativeURL.replace('/', File.separatorChar));
// Should the encoding be hard-coded to UTF-8?
String encoding = System.getProperty("file.encoding"); //$NON-NLS-1$
this.writer = new PrintWriter(file, encoding);
Charset cs;
try
{
String encoding = ResourcesPlugin.getEncoding();
if (encoding != null)
cs = Charset.forName(encoding);
else
cs = StandardCharsets.UTF_8;
}
catch (UnsupportedCharsetException e)
{
cs = StandardCharsets.UTF_8;
}
String encoding = cs.name();
this.writer = new PrintWriter(file, cs.name());

this.pathToRoot = ""; //$NON-NLS-1$
for (int ii = 0; ii < relativeURL.length(); ii++)
Expand Down Expand Up @@ -278,14 +295,24 @@ private void doProcessAlien(String format, IOutputter outputter, QueryPart test,

PageSnippets.linkedHeading(artefact, test, 5, filename);

Writer writer = new FileWriter(new File(this.directory, filename));
Charset cs;
try
{
outputter.process(info, result, writer);
String encoding = ResourcesPlugin.getEncoding();
if (encoding != null)
cs = Charset.forName(encoding);
else
cs = StandardCharsets.UTF_8;
}
finally
catch (UnsupportedCharsetException e)
{
writer.close();
cs = StandardCharsets.UTF_8;
}
try (FileOutputStream fos = new FileOutputStream(new File(this.directory, filename));
OutputStreamWriter osw = new OutputStreamWriter(fos, cs);
BufferedWriter writer = new BufferedWriter(osw))
{
outputter.process(info, result, writer);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
package org.eclipse.mat.ui.internal.viewer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -264,9 +261,7 @@ protected IStatus run(IProgressMonitor monitor)
{
cs = StandardCharsets.UTF_8;
}
try (FileOutputStream fos = new FileOutputStream(fileName);
Writer w = new OutputStreamWriter(fos, cs);
PrintWriter writer = new PrintWriter(w))
try (PrintWriter writer = new PrintWriter(fileName, cs.name()))
{
outputter.process(new ContextImpl(queryContext, //
new File(fileName).getParentFile()), result, writer);
Expand Down Expand Up @@ -339,9 +334,7 @@ protected IStatus run(IProgressMonitor monitor)
{
cs = StandardCharsets.UTF_8;
}
try (FileOutputStream fos = new FileOutputStream(fileName);
Writer w = new OutputStreamWriter(fos, cs);
PrintWriter writer = new PrintWriter(w))
try (PrintWriter writer = new PrintWriter(fileName, cs.name()))
{
outputter.process(new ContextImpl(queryContext, //
new File(fileName).getParentFile()), result, writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
package org.eclipse.mat.ui.snapshot.actions;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -122,11 +120,7 @@ private void writeStringData(IProgressListener listener) throws Exception
{
cs = StandardCharsets.UTF_8;
}
try (FileOutputStream out = new FileOutputStream(file);
OutputStreamWriter os = new OutputStreamWriter(out, cs);
BufferedWriter br = new BufferedWriter(os);
PrintWriter writer = new PrintWriter(br);
)
try (PrintWriter writer = new PrintWriter(file, cs.name()))
{

boolean isFirst = true;
Expand Down

0 comments on commit 4fe0ab9

Please sign in to comment.