From 4fae01236a88883d6e014bdb488fd40ea0685589 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 8 Mar 2016 17:02:53 +0000 Subject: [PATCH] Use java.nio.charset.StandardCharsets.* instead of Charset.forName --- source/net/filebot/CacheManager.java | 5 ++-- source/net/filebot/MetaAttributeView.java | 3 ++- source/net/filebot/cli/CmdlineOperations.java | 3 ++- .../net/filebot/cli/ScriptShellMethods.java | 6 ++--- source/net/filebot/mac/xattr/XAttrUtil.java | 7 ++--- source/net/filebot/torrent/BDecoder.java | 17 +++--------- source/net/filebot/torrent/Torrent.java | 26 ++----------------- .../subtitle/SubtitleDownloadComponent.java | 3 ++- .../ui/subtitle/SubtitleFileChooser.java | 14 ++-------- .../ui/transfer/TextFileTransferable.java | 14 +++------- source/net/filebot/util/DownloadTask.java | 4 +-- source/net/filebot/util/FileUtilities.java | 4 +-- source/net/filebot/web/WebRequest.java | 5 ++-- .../util/ByteBufferOutputStreamTest.java | 10 +++---- 14 files changed, 36 insertions(+), 85 deletions(-) diff --git a/source/net/filebot/CacheManager.java b/source/net/filebot/CacheManager.java index 57b01038..d440190e 100644 --- a/source/net/filebot/CacheManager.java +++ b/source/net/filebot/CacheManager.java @@ -1,5 +1,6 @@ package net.filebot; +import static java.nio.charset.StandardCharsets.*; import static net.filebot.Logging.*; import static net.filebot.Settings.*; import static net.filebot.util.FileUtilities.*; @@ -8,7 +9,6 @@ import java.io.File; import java.io.IOException; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; -import java.nio.charset.Charset; import java.nio.file.StandardOpenOption; import java.util.Scanner; import java.util.logging.Level; @@ -107,11 +107,10 @@ public class CacheManager { clearDiskStore(cache); } - // TODO: use UTF8 if (isNewCache) { // set new cache revision channel.position(0); - channel.write(Charset.forName("UTF-8").encode(String.valueOf(applicationRevision))); + channel.write(UTF_8.encode(String.valueOf(applicationRevision))); channel.truncate(channel.position()); } diff --git a/source/net/filebot/MetaAttributeView.java b/source/net/filebot/MetaAttributeView.java index 60d9d58d..ab5f0ab4 100644 --- a/source/net/filebot/MetaAttributeView.java +++ b/source/net/filebot/MetaAttributeView.java @@ -1,5 +1,6 @@ package net.filebot; +import static java.nio.charset.StandardCharsets.*; import static java.nio.file.Files.*; import java.io.File; @@ -46,7 +47,7 @@ public class MetaAttributeView extends AbstractMap { } } else { // UserDefinedFileAttributeView - this.encoding = Charset.forName("UTF-8"); + this.encoding = UTF_8; } } diff --git a/source/net/filebot/cli/CmdlineOperations.java b/source/net/filebot/cli/CmdlineOperations.java index ab273b4e..c831ff5e 100644 --- a/source/net/filebot/cli/CmdlineOperations.java +++ b/source/net/filebot/cli/CmdlineOperations.java @@ -1,5 +1,6 @@ package net.filebot.cli; +import static java.nio.charset.StandardCharsets.*; import static java.util.Arrays.*; import static java.util.Collections.*; import static net.filebot.Logging.*; @@ -689,7 +690,7 @@ public class CmdlineOperations implements CmdlineInterface { final SubtitleNaming naming = getSubtitleNaming(format); // when rewriting subtitles to target format an encoding must be defined, default to UTF-8 - final Charset outputEncoding = (csn != null) ? Charset.forName(csn) : (output != null) ? Charset.forName("UTF-8") : null; + final Charset outputEncoding = csn != null ? Charset.forName(csn) : output != null ? UTF_8 : null; final SubtitleFormat outputFormat = (output != null) ? getSubtitleFormatByName(output) : null; // ignore anything that is not a video diff --git a/source/net/filebot/cli/ScriptShellMethods.java b/source/net/filebot/cli/ScriptShellMethods.java index 18d50152..6be6e9b9 100644 --- a/source/net/filebot/cli/ScriptShellMethods.java +++ b/source/net/filebot/cli/ScriptShellMethods.java @@ -1,5 +1,6 @@ package net.filebot.cli; +import static java.nio.charset.StandardCharsets.*; import static java.util.Arrays.*; import static java.util.Collections.*; import static net.filebot.MediaTypes.*; @@ -9,7 +10,6 @@ import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.ByteBuffer; -import java.nio.charset.Charset; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; @@ -272,7 +272,7 @@ public class ScriptShellMethods { } public static String getText(ByteBuffer self) { - return Charset.forName("UTF-8").decode(self.duplicate()).toString(); + return UTF_8.decode(self.duplicate()).toString(); } public static ByteBuffer get(URL self) throws IOException { @@ -318,7 +318,7 @@ public class ScriptShellMethods { } public static File saveAs(String self, File file) throws IOException { - return saveAs(Charset.forName("UTF-8").encode(self), file); + return saveAs(UTF_8.encode(self), file); } public static File saveAs(URL self, File file) throws IOException { diff --git a/source/net/filebot/mac/xattr/XAttrUtil.java b/source/net/filebot/mac/xattr/XAttrUtil.java index 8278a10f..58378930 100644 --- a/source/net/filebot/mac/xattr/XAttrUtil.java +++ b/source/net/filebot/mac/xattr/XAttrUtil.java @@ -12,8 +12,9 @@ */ package net.filebot.mac.xattr; +import static java.nio.charset.StandardCharsets.*; + import java.nio.ByteBuffer; -import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; @@ -67,7 +68,7 @@ public class XAttrUtil { protected static Memory encodeString(String s) { // create NULL-terminated UTF-8 String - byte[] bb = s.getBytes(Charset.forName("UTF-8")); + byte[] bb = s.getBytes(UTF_8); Memory valueBuffer = new Memory(bb.length + 1); valueBuffer.write(0, bb, 0, bb.length); valueBuffer.setByte(valueBuffer.size() - 1, (byte) 0); @@ -75,7 +76,7 @@ public class XAttrUtil { } protected static String decodeString(ByteBuffer bb) { - return Charset.forName("UTF-8").decode(bb).toString(); + return UTF_8.decode(bb).toString(); } protected static List decodeStringSequence(ByteBuffer bb) { diff --git a/source/net/filebot/torrent/BDecoder.java b/source/net/filebot/torrent/BDecoder.java index f29952d0..72fd187c 100644 --- a/source/net/filebot/torrent/BDecoder.java +++ b/source/net/filebot/torrent/BDecoder.java @@ -22,34 +22,28 @@ package net.filebot.torrent; +import static java.nio.charset.StandardCharsets.*; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.CharBuffer; -import java.nio.charset.Charset; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; - /** - * A set of utility methods to decode a bencoded array of byte into a Map. integer are - * represented as Long, String as byte[], dictionnaries as Map, and list as List. + * A set of utility methods to decode a bencoded array of byte into a Map. integer are represented as Long, String as byte[], dictionnaries as Map, and list as List. * * @author TdC_VgA */ class BDecoder { - private static final Charset BINARY_CHARSET = Charset.forName("ISO-8859-1"); - - public static Map decode(InputStream is) throws IOException { return (new BDecoder().decodeStream(is)); } - public Map decodeStream(InputStream data) throws IOException { Object res = decodeInputStream(data, 0); @@ -61,7 +55,6 @@ class BDecoder { return ((Map) res); } - private Object decodeInputStream(InputStream bais, int nesting) throws IOException { if (!bais.markSupported()) throw new IOException("InputStream must support the mark() method"); @@ -89,7 +82,7 @@ class BDecoder { // add the value to the map - CharBuffer cb = BINARY_CHARSET.decode(ByteBuffer.wrap(tempByteArray)); + CharBuffer cb = ISO_8859_1.decode(ByteBuffer.wrap(tempByteArray)); String key = new String(cb.array(), 0, cb.limit()); @@ -156,7 +149,6 @@ class BDecoder { } } - private long getNumberFromStream(InputStream bais, char parseChar) throws IOException { int length = 0; @@ -189,14 +181,13 @@ class BDecoder { bais.skip(1); // return the value - CharBuffer cb = BINARY_CHARSET.decode(ByteBuffer.wrap(tempArray)); + CharBuffer cb = ISO_8859_1.decode(ByteBuffer.wrap(tempArray)); String str_value = new String(cb.array(), 0, cb.limit()); return Long.parseLong(str_value); } - private byte[] getByteArrayFromStream(InputStream bais) throws IOException { int length = (int) getNumberFromStream(bais, ':'); diff --git a/source/net/filebot/torrent/Torrent.java b/source/net/filebot/torrent/Torrent.java index 9a2dae6d..5d970e6c 100644 --- a/source/net/filebot/torrent/Torrent.java +++ b/source/net/filebot/torrent/Torrent.java @@ -1,6 +1,6 @@ - package net.filebot.torrent; +import static java.nio.charset.StandardCharsets.*; import java.io.BufferedInputStream; import java.io.File; @@ -14,7 +14,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; - public class Torrent { private String name; @@ -28,19 +27,16 @@ public class Torrent { private List files; private boolean singleFileTorrent; - protected Torrent() { // used by serializer } - public Torrent(File torrent) throws IOException { this(decodeTorrent(torrent)); } - public Torrent(Map torrentMap) { - Charset charset = Charset.forName("UTF-8"); + Charset charset = UTF_8; encoding = decodeString(torrentMap.get("encoding"), charset); try { @@ -99,7 +95,6 @@ public class Torrent { } } - private static Map decodeTorrent(File torrent) throws IOException { InputStream in = new BufferedInputStream(new FileInputStream(torrent)); @@ -110,7 +105,6 @@ public class Torrent { } } - private String decodeString(Object byteArray, Charset charset) { if (byteArray == null) return null; @@ -118,7 +112,6 @@ public class Torrent { return new String((byte[]) byteArray, charset); } - private Long decodeLong(Object number) { if (number == null) return null; @@ -126,82 +119,67 @@ public class Torrent { return (Long) number; } - public String getAnnounce() { return announce; } - public String getComment() { return comment; } - public String getCreatedBy() { return createdBy; } - public Long getCreationDate() { return creationDate; } - public String getEncoding() { return encoding; } - public List getFiles() { return files; } - public String getName() { return name; } - public Long getPieceLength() { return pieceLength; } - public boolean isSingleFileTorrent() { return singleFileTorrent; } - public static class Entry { private final String path; private final long length; - public Entry(String path, long length) { this.path = path; this.length = length; } - public String getPath() { return path; } - public String getName() { // the last element in the path is the filename // torrents don't contain directory entries, so there is always a non-empty name return path.substring(path.lastIndexOf("/") + 1); } - public long getLength() { return length; } - @Override public String toString() { return getPath(); diff --git a/source/net/filebot/ui/subtitle/SubtitleDownloadComponent.java b/source/net/filebot/ui/subtitle/SubtitleDownloadComponent.java index d21a8956..217882b2 100644 --- a/source/net/filebot/ui/subtitle/SubtitleDownloadComponent.java +++ b/source/net/filebot/ui/subtitle/SubtitleDownloadComponent.java @@ -1,5 +1,6 @@ package net.filebot.ui.subtitle; +import static java.nio.charset.StandardCharsets.*; import static net.filebot.MediaTypes.*; import static net.filebot.UserFiles.*; import static net.filebot.subtitle.SubtitleUtilities.*; @@ -294,7 +295,7 @@ class SubtitleDownloadComponent extends JComponent { // default values SubtitleFormat selectedFormat = SubtitleFormat.SubRip; long selectedTimingOffset = 0; - Charset selectedEncoding = Charset.forName("UTF-8"); + Charset selectedEncoding = UTF_8; // just use default values when we can't use a JFC with accessory component (also Swing OSX LaF doesn't seem to support JFileChooser::setAccessory) if (Settings.isMacApp()) { diff --git a/source/net/filebot/ui/subtitle/SubtitleFileChooser.java b/source/net/filebot/ui/subtitle/SubtitleFileChooser.java index 6ff50122..7b60728e 100644 --- a/source/net/filebot/ui/subtitle/SubtitleFileChooser.java +++ b/source/net/filebot/ui/subtitle/SubtitleFileChooser.java @@ -1,7 +1,6 @@ - package net.filebot.ui.subtitle; - +import static java.nio.charset.StandardCharsets.*; import static java.util.Collections.*; import java.nio.charset.Charset; @@ -20,30 +19,26 @@ import javax.swing.SpinnerNumberModel; import net.filebot.subtitle.SubtitleFormat; import net.miginfocom.swing.MigLayout; - public class SubtitleFileChooser extends JFileChooser { protected final JComboBox format = new JComboBox(); protected final JComboBox encoding = new JComboBox(); protected final JSpinner offset = new JSpinner(new SpinnerNumberModel(0, -14400000, 14400000, 100)); - public SubtitleFileChooser() { setAccessory(createAcessory()); setDefaultOptions(); } - protected void setDefaultOptions() { setFormatOptions(singleton(SubtitleFormat.SubRip)); Set encodings = new LinkedHashSet(2); - encodings.add(Charset.forName("UTF-8")); // UTF-8 as default charset + encodings.add(UTF_8); // UTF-8 as default charset encodings.add(Charset.defaultCharset()); // allow default system encoding to be used as well setEncodingOptions(encodings); } - protected JComponent createAcessory() { JPanel acessory = new JPanel(new MigLayout("nogrid")); @@ -58,27 +53,22 @@ public class SubtitleFileChooser extends JFileChooser { return acessory; } - public void setEncodingOptions(Set options) { encoding.setModel(new DefaultComboBoxModel(options.toArray())); } - public Charset getSelectedEncoding() { return (Charset) encoding.getSelectedItem(); } - public void setFormatOptions(Set options) { format.setModel(new DefaultComboBoxModel(options.toArray())); } - public SubtitleFormat getSelectedFormat() { return (SubtitleFormat) format.getSelectedItem(); } - public long getTimingOffset() { return (Integer) offset.getValue(); } diff --git a/source/net/filebot/ui/transfer/TextFileTransferable.java b/source/net/filebot/ui/transfer/TextFileTransferable.java index ec188cd9..ffc0a7cd 100644 --- a/source/net/filebot/ui/transfer/TextFileTransferable.java +++ b/source/net/filebot/ui/transfer/TextFileTransferable.java @@ -1,6 +1,6 @@ - package net.filebot.ui.transfer; +import static java.nio.charset.StandardCharsets.*; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.UnsupportedFlavorException; @@ -10,17 +10,14 @@ import java.util.AbstractMap; import java.util.Collections; import java.util.Set; - public class TextFileTransferable extends ByteBufferTransferable { private final String text; - public TextFileTransferable(String name, String text) { - this(name, text, Charset.forName("UTF-8")); + this(name, text, UTF_8); } - public TextFileTransferable(final String name, final String text, final Charset charset) { // lazy data map for file transfer super(new AbstractMap() { @@ -39,7 +36,6 @@ public class TextFileTransferable extends ByteBufferTransferable { this.text = text; } - @Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException { // check file flavor first, because text/uri-list is also text flavor @@ -55,15 +51,11 @@ public class TextFileTransferable extends ByteBufferTransferable { throw new UnsupportedFlavorException(flavor); } - @Override public DataFlavor[] getTransferDataFlavors() { - return new DataFlavor[] { - DataFlavor.javaFileListFlavor, FileTransferable.uriListFlavor, DataFlavor.stringFlavor - }; + return new DataFlavor[] { DataFlavor.javaFileListFlavor, FileTransferable.uriListFlavor, DataFlavor.stringFlavor }; } - @Override public boolean isDataFlavorSupported(DataFlavor flavor) { // file flavor or text flavor diff --git a/source/net/filebot/util/DownloadTask.java b/source/net/filebot/util/DownloadTask.java index 6c45981c..3180e0b7 100644 --- a/source/net/filebot/util/DownloadTask.java +++ b/source/net/filebot/util/DownloadTask.java @@ -1,5 +1,6 @@ package net.filebot.util; +import static java.nio.charset.StandardCharsets.*; import static net.filebot.util.FileUtilities.*; import static net.filebot.web.WebRequest.*; @@ -10,7 +11,6 @@ import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; -import java.nio.charset.Charset; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -59,7 +59,7 @@ public class DownloadTask extends SwingWorker { HttpURLConnection connection = createConnection(); if (postParameters != null) { - ByteBuffer postData = Charset.forName("UTF-8").encode(encodeParameters(postParameters, true)); + ByteBuffer postData = UTF_8.encode(encodeParameters(postParameters, true)); // add content type and content length headers connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded"); diff --git a/source/net/filebot/util/FileUtilities.java b/source/net/filebot/util/FileUtilities.java index a7b1c900..5ab9907a 100644 --- a/source/net/filebot/util/FileUtilities.java +++ b/source/net/filebot/util/FileUtilities.java @@ -1,5 +1,6 @@ package net.filebot.util; +import static java.nio.charset.StandardCharsets.*; import static java.util.Arrays.*; import static java.util.Collections.*; @@ -15,7 +16,6 @@ import java.io.Reader; import java.math.BigInteger; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.FileVisitOption; @@ -255,7 +255,7 @@ public final class FileUtilities { } // assume UTF-8 by default - return Charset.forName("UTF-8").decode(data).toString(); + return UTF_8.decode(data).toString(); } /** diff --git a/source/net/filebot/web/WebRequest.java b/source/net/filebot/web/WebRequest.java index 2bdbb54f..9fddd354 100644 --- a/source/net/filebot/web/WebRequest.java +++ b/source/net/filebot/web/WebRequest.java @@ -1,5 +1,6 @@ package net.filebot.web; +import static java.nio.charset.StandardCharsets.*; import static net.filebot.util.FileUtilities.*; import java.io.ByteArrayOutputStream; @@ -284,12 +285,12 @@ public final class WebRequest { // use http default encoding only for text/html if (contentType.equals("text/html")) { - return Charset.forName("ISO-8859-1"); + return ISO_8859_1; } } // use UTF-8 if we don't know any better - return Charset.forName("UTF-8"); + return UTF_8; } public static String getXmlString(Document dom, boolean indent) throws TransformerException { diff --git a/test/net/filebot/util/ByteBufferOutputStreamTest.java b/test/net/filebot/util/ByteBufferOutputStreamTest.java index f3d38158..c6602b55 100644 --- a/test/net/filebot/util/ByteBufferOutputStreamTest.java +++ b/test/net/filebot/util/ByteBufferOutputStreamTest.java @@ -1,17 +1,14 @@ - package net.filebot.util; - +import static java.nio.charset.StandardCharsets.*; import static org.junit.Assert.*; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.channels.Channels; -import java.nio.charset.Charset; import org.junit.Test; - public class ByteBufferOutputStreamTest { @Test @@ -22,13 +19,12 @@ public class ByteBufferOutputStreamTest { buffer.write("asdf".getBytes("utf-8")); // check content - assertEquals("asdf", Charset.forName("utf-8").decode(buffer.getByteBuffer()).toString()); + assertEquals("asdf", UTF_8.decode(buffer.getByteBuffer()).toString()); // check capacity assertEquals(4, buffer.capacity()); } - @Test public void transferFrom() throws Exception { InputStream in = new ByteArrayInputStream("asdf".getBytes("utf-8")); @@ -41,7 +37,7 @@ public class ByteBufferOutputStreamTest { assertEquals(4, n); // check content - assertEquals("asdf", Charset.forName("utf-8").decode(buffer.getByteBuffer()).toString()); + assertEquals("asdf", UTF_8.decode(buffer.getByteBuffer()).toString()); } }