forked from upstream-mirrors/OpenTracks
Fix comments for af72371db217
This commit is contained in:
@@ -32,7 +32,8 @@ import java.util.TimeZone;
|
|||||||
public class FileUtils {
|
public class FileUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The maximum Fat32 path length.
|
* The maximum Fat32 path length. See
|
||||||
|
* http://www.scribd.com/doc/2187273/FAT32-File-System-Specification
|
||||||
*/
|
*/
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final int MAX_FAT32_PATH_LENGTH = 260;
|
static final int MAX_FAT32_PATH_LENGTH = 260;
|
||||||
@@ -143,12 +144,11 @@ public class FileUtils {
|
|||||||
*/
|
*/
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
String sanitizeFileName(String name) {
|
String sanitizeFileName(String name) {
|
||||||
StringBuffer buffer = new StringBuffer();
|
StringBuffer buffer = new StringBuffer(name.length());
|
||||||
for (int i = 0; i < name.length(); i++) {
|
for (int i = 0; i < name.length(); i++) {
|
||||||
int codePoint = name.codePointAt(i);
|
int codePoint = name.codePointAt(i);
|
||||||
if (codePoint > 127) {
|
char character = name.charAt(i);
|
||||||
buffer.appendCodePoint(codePoint);
|
if (Character.isLetterOrDigit(character) || codePoint > 127 || character == '-') {
|
||||||
} else if (Character.isLetterOrDigit(name.charAt(i))) {
|
|
||||||
buffer.appendCodePoint(codePoint);
|
buffer.appendCodePoint(codePoint);
|
||||||
} else {
|
} else {
|
||||||
buffer.append("_");
|
buffer.append("_");
|
||||||
@@ -159,7 +159,7 @@ public class FileUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Truncates the name if necessary thus the filename path length (directory +
|
* Truncates the name if necessary so the filename path length (directory +
|
||||||
* name + suffix) meets the Fat32 path limit.
|
* name + suffix) meets the Fat32 path limit.
|
||||||
*
|
*
|
||||||
* @param directory directory
|
* @param directory directory
|
||||||
@@ -168,8 +168,7 @@ public class FileUtils {
|
|||||||
*/
|
*/
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
String truncateFileName(File directory, String name, String suffix) {
|
String truncateFileName(File directory, String name, String suffix) {
|
||||||
|
// 1 at the end accounts for the FAT32 filename trailing NUL character
|
||||||
// 1 at the end accounts for the trailing NUL character
|
|
||||||
int requiredLength = directory.getPath().length() + suffix.length() + 1;
|
int requiredLength = directory.getPath().length() + suffix.length() + 1;
|
||||||
if (name.length() + requiredLength > MAX_FAT32_PATH_LENGTH) {
|
if (name.length() + requiredLength > MAX_FAT32_PATH_LENGTH) {
|
||||||
int limit = MAX_FAT32_PATH_LENGTH - requiredLength;
|
int limit = MAX_FAT32_PATH_LENGTH - requiredLength;
|
||||||
|
|||||||
@@ -31,9 +31,6 @@ import junit.framework.TestCase;
|
|||||||
* @author Rodrigo Damazio
|
* @author Rodrigo Damazio
|
||||||
*/
|
*/
|
||||||
public class FileUtilsTest extends TestCase {
|
public class FileUtilsTest extends TestCase {
|
||||||
private static final String ORIGINAL_NAME = "Swim\10ming-^across: the/ pacific (ocean).";
|
|
||||||
private static final String SANITIZED_NAME = "Swim_ming_across_the_pacific_ocean_";
|
|
||||||
|
|
||||||
private FileUtils fileUtils;
|
private FileUtils fileUtils;
|
||||||
private Set<String> existingFiles;
|
private Set<String> existingFiles;
|
||||||
|
|
||||||
@@ -66,7 +63,10 @@ public class FileUtilsTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testSanitizeFileName() {
|
public void testSanitizeFileName() {
|
||||||
assertEquals(SANITIZED_NAME, fileUtils.sanitizeFileName(ORIGINAL_NAME));
|
String name = "Swim\10ming-^across: the/ pacific (ocean).您好-привет_";
|
||||||
|
String expected = "Swim_ming-_across_the_pacific_ocean_您好-привет_";
|
||||||
|
|
||||||
|
assertEquals(expected, fileUtils.sanitizeFileName(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testTruncateFileName() {
|
public void testTruncateFileName() {
|
||||||
@@ -78,16 +78,10 @@ public class FileUtilsTest extends TestCase {
|
|||||||
}
|
}
|
||||||
String nameString = new String(name);
|
String nameString = new String(name);
|
||||||
|
|
||||||
int expectedLength = FileUtils.MAX_FAT32_PATH_LENGTH - directory.getPath().length()
|
|
||||||
- suffix.length() - 1;
|
|
||||||
char[] expected = new char[expectedLength];
|
|
||||||
for (int i = 0; i < expected.length; i++) {
|
|
||||||
expected[i] = 'a';
|
|
||||||
}
|
|
||||||
String expectedString = new String(expected);
|
|
||||||
|
|
||||||
String truncated = fileUtils.truncateFileName(directory, nameString, suffix);
|
String truncated = fileUtils.truncateFileName(directory, nameString, suffix);
|
||||||
assertEquals(expectedString, truncated);
|
for (int i = 0; i < truncated.length(); i++) {
|
||||||
|
assertEquals('a', truncated.charAt(i));
|
||||||
|
}
|
||||||
assertEquals(FileUtils.MAX_FAT32_PATH_LENGTH,
|
assertEquals(FileUtils.MAX_FAT32_PATH_LENGTH,
|
||||||
new File(directory, truncated + suffix).getPath().length());
|
new File(directory, truncated + suffix).getPath().length());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user