Refactor filename illegal char method

This commit is contained in:
SiboVG 2023-09-20 13:27:38 +02:00
parent 27b0498a3b
commit 19854154b4
2 changed files with 29 additions and 30 deletions

View File

@ -9,31 +9,29 @@ import java.io.OutputStream;
import java.nio.file.Paths;
public abstract class FileUtils {
private static final char[] ILLEGAL_CHARS = new char[] { '/', '\\', ':', '*', '?', '"', '<', '>', '|' };
public static void copy( InputStream is, OutputStream os ) throws IOException {
if ( ! (os instanceof BufferedOutputStream ) ) {
public static void copy(InputStream is, OutputStream os) throws IOException {
if (!(os instanceof BufferedOutputStream)) {
os = new BufferedOutputStream(os);
}
if ( ! (is instanceof BufferedInputStream ) ) {
if (!(is instanceof BufferedInputStream)) {
is = new BufferedInputStream(is);
}
byte[] buffer = new byte[1024];
int bytesRead = 0;
while( (bytesRead = is.read(buffer)) > 0 ) {
os.write(buffer,0,bytesRead);
while( (bytesRead = is.read(buffer)) > 0) {
os.write(buffer, 0, bytesRead);
}
os.flush();
}
public static byte[] readBytes( InputStream is ) throws IOException {
public static byte[] readBytes(InputStream is) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
copy( is, bos );
copy(is, bos);
return bos.toByteArray();
@ -61,4 +59,22 @@ public abstract class FileUtils {
return Paths.get(pathString).getFileName().toString();
}
/**
* Returns an illegal character if one is found in the filename, otherwise returns null.
* @param filename The filename to check
* @return The illegal character, or null if none is found
*/
public static Character getIllegalFilenameChar(String filename) {
if (filename == null || filename.isEmpty()) {
return null;
}
for (char c : ILLEGAL_CHARS) {
if (filename.indexOf(c) >= 0) {
return c;
}
}
return null;
}
}

View File

@ -2,6 +2,7 @@ package net.sf.openrocket.gui.widgets;
import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.startup.Application;
import net.sf.openrocket.util.FileUtils;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
@ -12,7 +13,6 @@ import java.util.regex.PatternSyntaxException;
public class SaveFileChooser extends JFileChooser {
private static final Translator trans = Application.getTranslator();
private static final char[] ILLEGAL_CHARS = new char[] { '/', '\\', ':', '*', '?', '"', '<', '>', '|' };
private File cwd = null;
private File currentFile = null;
@ -30,7 +30,7 @@ public class SaveFileChooser extends JFileChooser {
if (file.getParentFile() != getCurrentDirectory()) {
cwd = getCurrentDirectory();
fileName = getFilenameInput(currentFile, cwd);
if (getIllegalChar(fileName) != null) {
if (FileUtils.getIllegalFilenameChar(fileName) != null) {
return;
}
}
@ -42,7 +42,7 @@ public class SaveFileChooser extends JFileChooser {
@Override
public void approveSelection() {
Character c = getIllegalChar(fileName);
Character c = FileUtils.getIllegalFilenameChar(fileName);
if (c != null) {
// Illegal character found
JOptionPane.showMessageDialog(getParent(),
@ -57,23 +57,6 @@ public class SaveFileChooser extends JFileChooser {
}
}
/**
* Returns an illegal character if one is found in the filename, otherwise returns null.
* @param filename The filename to check
* @return The illegal character, or null if none is found
*/
private Character getIllegalChar(String filename) {
if (filename == null || filename.isEmpty()) {
return null;
}
for (char c : ILLEGAL_CHARS) {
if (filename.indexOf(c) >= 0) {
return c;
}
}
return null;
}
/**
* Returns the filename input by the user, or null if the filename is invalid.
* You can't simply use getSelectedFile().getName() because it won't work for malformed filenames.