From 8ef765df064ee029b2659a60bd7e40cdd55167c6 Mon Sep 17 00:00:00 2001 From: Andrei Solntsev Date: Sun, 17 Sep 2023 08:54:20 +0300 Subject: [PATCH] fix IDEA warnings "Standard 'Charset' object can be used" --- .../test/ReferenceComparison.java | 2 +- .../java/org/xhtmlrenderer/test/Regress.java | 8 ++-- .../fop/nbsp/NonBreakPointsLoaderImpl.java | 7 +-- .../org/xhtmlrenderer/pdf/ITextRenderer.java | 45 ++++++------------ .../org/xhtmlrenderer/pdf/ITextRenderer.java | 46 ++++++------------- 5 files changed, 36 insertions(+), 72 deletions(-) diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/ReferenceComparison.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/ReferenceComparison.java index 1c1bf4df1..90814837c 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/ReferenceComparison.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/ReferenceComparison.java @@ -161,7 +161,7 @@ private void storeFailed(File failedDirectory, File refFile, String suffix, Stri OutputStreamWriter fw = null; try { - fw = new OutputStreamWriter(new FileOutputStream(new File(failedDirectory, refFile.getName() + ".err" + suffix)), "UTF-8"); + fw = new OutputStreamWriter(new FileOutputStream(new File(failedDirectory, refFile.getName() + ".err" + suffix)), UTF_8); BufferedWriter bw = new BufferedWriter(fw); try { bw.write(compareTo); diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/Regress.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/Regress.java index 70781eae1..636ad8131 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/Regress.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/Regress.java @@ -23,8 +23,6 @@ import org.xhtmlrenderer.swing.BoxRenderer; import org.xhtmlrenderer.swing.Java2DRenderer; import org.xhtmlrenderer.util.FSImageWriter; -import org.xhtmlrenderer.util.IOUtil; -import org.xhtmlrenderer.util.Zipper; import java.awt.image.BufferedImage; import java.io.*; @@ -32,6 +30,8 @@ import java.util.Iterator; import java.util.List; +import static java.nio.charset.StandardCharsets.UTF_8; + /** * Creates a directory containing output from the renderer as text, given a set of input files (XHTML, XML). @@ -189,7 +189,7 @@ private void writeToFile(File outputDir, String fileName, String output) throws } FileOutputStream fos = new FileOutputStream(outputFile); try { - OutputStreamWriter fw = new OutputStreamWriter(fos, "UTF-8"); + OutputStreamWriter fw = new OutputStreamWriter(fos, UTF_8); PrintWriter pw = new PrintWriter(new BufferedWriter(fw)); try { pw.print(output); @@ -202,8 +202,6 @@ private void writeToFile(File outputDir, String fileName, String output) throws // swallow } } - } catch (IOException e) { - e.printStackTrace(); } finally { try { fos.close(); diff --git a/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImpl.java b/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImpl.java index 6959d7929..b2ed4f6c8 100644 --- a/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImpl.java +++ b/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImpl.java @@ -22,10 +22,11 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; +import static java.nio.charset.StandardCharsets.UTF_8; + /** * @author Lukas Zaruba, lukas.zaruba@gmail.com */ @@ -50,8 +51,8 @@ private List loadForKey(String lang) { InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); if (is == null) return null; try { - BufferedReader r = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); - List result = new ArrayList(); + BufferedReader r = new BufferedReader(new InputStreamReader(is, UTF_8)); + List result = new ArrayList<>(); String line; while ((line = r.readLine()) != null) { if (line.isEmpty() || line.startsWith("#")) continue; diff --git a/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java b/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java index 0ebf4bb05..bb49a74f1 100644 --- a/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java +++ b/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java @@ -19,27 +19,8 @@ */ package org.xhtmlrenderer.pdf; -import java.awt.*; -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; -import java.io.StringReader; -import java.io.StringWriter; -import java.io.UnsupportedEncodingException; -import java.io.Writer; -import java.util.List; -import java.util.regex.Pattern; - -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerConfigurationException; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - +import com.itextpdf.text.DocumentException; +import com.itextpdf.text.pdf.PdfWriter; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -61,8 +42,15 @@ import org.xhtmlrenderer.util.Configuration; import org.xml.sax.InputSource; -import com.itextpdf.text.DocumentException; -import com.itextpdf.text.pdf.PdfWriter; +import javax.xml.transform.*; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import java.awt.*; +import java.io.*; +import java.util.List; +import java.util.regex.Pattern; + +import static java.nio.charset.StandardCharsets.UTF_8; public class ITextRenderer { // These two defaults combine to produce an effective resolution of 96 px to @@ -441,14 +429,9 @@ private void paintPage(RenderingContext c, PdfWriter writer, PageBox page) throw private void provideMetadataToPage(PdfWriter writer, PageBox page) throws IOException { byte[] metadata = null; if (page.getMetadata() != null) { - try { - String metadataBody = stringfyMetadata(page.getMetadata()); - if (metadataBody != null) { - metadata = createXPacket(stringfyMetadata(page.getMetadata())).getBytes("UTF-8"); - } - } catch (UnsupportedEncodingException e) { - // Can't happen - throw new RuntimeException(e); + String metadataBody = stringfyMetadata(page.getMetadata()); + if (metadataBody != null) { + metadata = createXPacket(stringfyMetadata(page.getMetadata())).getBytes(UTF_8); } } diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java index d8593c8ac..80630b7a6 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java @@ -19,28 +19,8 @@ */ package org.xhtmlrenderer.pdf; -import java.awt.Dimension; -import java.awt.Rectangle; -import java.awt.Shape; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; -import java.io.StringReader; -import java.io.StringWriter; -import java.io.UnsupportedEncodingException; -import java.io.Writer; -import java.util.List; -import java.util.regex.Pattern; - -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerConfigurationException; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - +import com.lowagie.text.DocumentException; +import com.lowagie.text.pdf.PdfWriter; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -61,8 +41,15 @@ import org.xhtmlrenderer.util.Configuration; import org.xml.sax.InputSource; -import com.lowagie.text.DocumentException; -import com.lowagie.text.pdf.PdfWriter; +import javax.xml.transform.*; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import java.awt.*; +import java.io.*; +import java.util.List; +import java.util.regex.Pattern; + +import static java.nio.charset.StandardCharsets.UTF_8; public class ITextRenderer { // These two defaults combine to produce an effective resolution of 96 px to @@ -438,14 +425,9 @@ private void paintPage(RenderingContext c, PdfWriter writer, PageBox page) { private void provideMetadataToPage(PdfWriter writer, PageBox page) { byte[] metadata = null; if (page.getMetadata() != null) { - try { - String metadataBody = stringfyMetadata(page.getMetadata()); - if (metadataBody != null) { - metadata = createXPacket(stringfyMetadata(page.getMetadata())).getBytes("UTF-8"); - } - } catch (UnsupportedEncodingException e) { - // Can't happen - throw new RuntimeException(e); + String metadataBody = stringfyMetadata(page.getMetadata()); + if (metadataBody != null) { + metadata = createXPacket(stringfyMetadata(page.getMetadata())).getBytes(UTF_8); } }