Skip to content

Commit

Permalink
Refactoring case-insensitive file extension checking
Browse files Browse the repository at this point in the history
  • Loading branch information
woesss committed Sep 3, 2024
1 parent 86d3435 commit 724b90a
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 28 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/siemens/mp/lcdui/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.microedition.io.Connector;

import ru.playsoftware.j2meloader.util.PNGUtils;
import ru.woesss.util.TextUtils;

public class Image extends com.siemens.mp.ui.Image {

Expand Down Expand Up @@ -89,7 +90,7 @@ public static void writeImageToFile(javax.microedition.lcdui.Image img, String f
file = "a:" + file;
}
try (OutputStream stream = Connector.openOutputStream("file:///" + file)) {
if (file.regionMatches(true, file.length() - 4, ".jpg", 0, 4)) {
if (TextUtils.endsWithIgnoreCase(file, ".jpg")) {
img.getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, stream);
} else {
img.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@
import javax.microedition.shell.MicroActivity;
import javax.microedition.util.ContextHolder;

import kotlin.io.FilesKt;
import ru.playsoftware.j2meloader.R;
import ru.playsoftware.j2meloader.config.model.Size;
import ru.playsoftware.j2meloader.databinding.ActivityConfigBinding;
import ru.playsoftware.j2meloader.settings.KeyMapperActivity;
import ru.playsoftware.j2meloader.util.FileUtils;
import ru.playsoftware.j2meloader.util.ViewUtils;
import ru.woesss.util.TextUtils;
import yuku.ambilwarna.AmbilWarnaDialog;

public class ConfigActivity extends AppCompatActivity implements View.OnClickListener, ShaderTuneAlert.Callback {
Expand Down Expand Up @@ -409,10 +411,22 @@ private void initShaderSpinner() {
ArrayAdapter<ShaderInfo> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, shaders);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
binding.spShader.setAdapter(adapter);
File[] files = dir.listFiles((f) -> f.isFile() && f.getName().toLowerCase().endsWith(".ini"));
String[] files = dir.list();
if (files != null) {
for (File file : files) {
String text = FileUtils.getText(file.getAbsolutePath());
for (String fileName : files) {
if (!TextUtils.endsWithIgnoreCase(fileName, ".ini")) {
continue;
}
File file = new File(dir, fileName);
String text;
try {
//noinspection CharsetObjectCanBeUsed
text = FilesKt.readText(file, Charset.forName("UTF-8"));
} catch (Exception e) {
Log.e(TAG, "getText: " + file, e);
continue;
}

String[] split = text.split("[\\n\\r]+");
ShaderInfo info = null;
for (String line : split) {
Expand Down
13 changes: 7 additions & 6 deletions app/src/main/java/ru/woesss/j2me/installer/AppInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import ru.playsoftware.j2meloader.util.IOUtils;
import ru.playsoftware.j2meloader.util.ZipUtils;
import ru.woesss.j2me.jar.Descriptor;
import ru.woesss.util.TextUtils;
import ru.woesss.util.zip.ZipFile;

public class AppInstaller {
Expand Down Expand Up @@ -121,7 +122,7 @@ void loadInfo(SingleEmitter<Integer> emitter) throws IOException, ConverterExcep

String name = srcFile.getName();

if (name.toLowerCase().endsWith(".jad")) {
if (TextUtils.endsWithIgnoreCase(name, ".jad")) {
newDesc = new Descriptor(srcFile, true);
String url = newDesc.getJarUrl();
if (url == null) {
Expand All @@ -136,7 +137,7 @@ void loadInfo(SingleEmitter<Integer> emitter) throws IOException, ConverterExcep
return;
}
}
} else if (name.toLowerCase().endsWith(".kjx")) {
} else if (TextUtils.endsWithIgnoreCase(name, ".kjx")) {
// Load kjx file
parseKjx();
newDesc = new Descriptor(srcFile, true);
Expand Down Expand Up @@ -213,8 +214,8 @@ private void downloadJad() throws ConverterException {
connection.setReadTimeout(3 * 60 * 1000);
connection.setConnectTimeout(15000);
int code = connection.getResponseCode();
if (code == HttpURLConnection.HTTP_MOVED_PERM
|| code == HttpURLConnection.HTTP_MOVED_TEMP) {
if (code == HttpURLConnection.HTTP_MOVED_PERM ||
code == HttpURLConnection.HTTP_MOVED_TEMP) {
String urlStr = connection.getHeaderField("Location");
connection.disconnect();
connection = (HttpURLConnection) new URL(urlStr).openConnection();
Expand Down Expand Up @@ -432,8 +433,8 @@ private void downloadJar() throws ConverterException {
connection.setReadTimeout(3 * 60 * 1000);
connection.setConnectTimeout(15000);
int code = connection.getResponseCode();
if (code == HttpURLConnection.HTTP_MOVED_PERM
|| code == HttpURLConnection.HTTP_MOVED_TEMP) {
if (code == HttpURLConnection.HTTP_MOVED_PERM ||
code == HttpURLConnection.HTTP_MOVED_TEMP) {
String urlStr = connection.getHeaderField("Location");
connection.disconnect();
connection = (HttpURLConnection) new URL(urlStr).openConnection();
Expand Down
4 changes: 3 additions & 1 deletion dexlib/src/main/java/com/android/dex/Dex.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import ru.woesss.util.TextUtils;

/**
* The bytes of a dex file in memory for reading and writing. All int offsets
* are unsigned.
Expand Down Expand Up @@ -88,7 +90,7 @@ public Dex(File file) throws IOException {
} else {
throw new DexException("Expected " + DexFormat.DEX_IN_JAR_NAME + " in " + file);
}
} else if (file.getName().endsWith(".dex")) {
} else if (TextUtils.endsWithIgnoreCase(file.getName(), ".dex")) {
try (InputStream inputStream = new FileInputStream(file)) {
loadFrom(inputStream);
}
Expand Down
8 changes: 5 additions & 3 deletions dexlib/src/main/java/com/android/dex/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.io.FileInputStream;
import java.io.IOException;

import ru.woesss.util.TextUtils;

/**
* File I/O utilities.
*/
Expand Down Expand Up @@ -78,8 +80,8 @@ public static byte[] readFile(File file) {
* Returns true if {@code fileName} names a .zip, .jar, or .apk.
*/
public static boolean hasArchiveSuffix(String fileName) {
return fileName.endsWith(".zip")
|| fileName.endsWith(".jar")
|| fileName.endsWith(".apk");
return TextUtils.endsWithIgnoreCase(fileName, ".zip")
|| TextUtils.endsWithIgnoreCase(fileName, ".jar")
|| TextUtils.endsWithIgnoreCase(fileName, ".apk");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import ru.woesss.util.TextUtils;
import ru.woesss.util.zip.ZipFile;

/**
Expand Down Expand Up @@ -149,11 +149,11 @@ private boolean processOne(File file, boolean topLevel) {
}

String path = file.getPath();
String lowerName = file.getName().toLowerCase(Locale.US);
String name = file.getName();

if (lowerName.endsWith(".zip") ||
lowerName.endsWith(".jar") ||
lowerName.endsWith(".apk")) {
if (TextUtils.endsWithIgnoreCase(name, ".zip") ||
TextUtils.endsWithIgnoreCase(name, ".jar") ||
TextUtils.endsWithIgnoreCase(name, ".apk")) {
return processArchive(file);
}
if (filter.accept(path)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import com.android.dx.util.ByteArray;
import com.android.dx.util.Hex;

import java.util.Locale;
import ru.woesss.util.TextUtils;

/**
* Class file with info taken from a {@code byte[]} or slice thereof.
Expand Down Expand Up @@ -514,7 +514,7 @@ private void parse0() {
* package/class name.
*/
String thisClassName = thisClass.getClassType().getClassName();
if (!(filePath.toLowerCase(Locale.US).endsWith(".class") &&
if (!(TextUtils.endsWithIgnoreCase(filePath, ".class") &&
filePath.startsWith(thisClassName) &&
(filePath.length() == (thisClassName.length() + 6)))) {
throw new ParseException("class name (" + thisClassName +
Expand Down
13 changes: 6 additions & 7 deletions dexlib/src/main/java/com/android/dx/command/dexer/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ArrayBlockingQueue;
Expand All @@ -71,6 +70,8 @@
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;

import ru.woesss.util.TextUtils;

/**
* Main class for the class file translator.
*/
Expand Down Expand Up @@ -308,7 +309,7 @@ private boolean processAllFiles() {

try {
for (int i = 0; i < fileNames.length; i++) {
processOne(fileNames[i], path -> path.toLowerCase(Locale.US).endsWith(".class"));
processOne(fileNames[i], path -> TextUtils.endsWithIgnoreCase(path, ".class"));
}
} catch (StopProcessing ex) {
/*
Expand Down Expand Up @@ -413,7 +414,7 @@ private void updateStatus(boolean res) {
*/
private boolean processFileBytes(String name, long crc, byte[] bytes) {

boolean isClass = name != null && name.toLowerCase(Locale.US).endsWith(".class");
boolean isClass = name != null && TextUtils.endsWithIgnoreCase(name, ".class");
boolean keepResources = (outputResources != null);

if (!isClass && !keepResources) {
Expand Down Expand Up @@ -1124,12 +1125,10 @@ private void parseFlags(ArgumentsParser parser) {
outputIsDirectory = true;
} else if (FileUtils.hasArchiveSuffix(outName)) {
jarOutput = true;
} else if (outName.endsWith(".dex") ||
outName.equals("-")) {
} else if (TextUtils.endsWithIgnoreCase(outName, ".dex") || outName.equals("-")) {
jarOutput = false;
} else {
context.err.println("unknown output extension: " +
outName);
context.err.println("unknown output extension: " + outName);
throw new UsageException();
}
} else if (parser.isArg("--dump-to=")) {
Expand Down
28 changes: 28 additions & 0 deletions dexlib/src/main/java/ru/woesss/util/TextUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 Yury Kharchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ru.woesss.util;

public class TextUtils {

public static boolean endsWithIgnoreCase(String string, String suffix) {
int offset = string.length() - suffix.length();
if (offset < 0) {
return false;
}
return string.regionMatches(true, offset, suffix, 0, suffix.length());
}
}

0 comments on commit 724b90a

Please sign in to comment.