From aed9248b3544dda6e67eedead993fcad3a6448a8 Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Fri, 6 Jan 2012 16:06:12 -0800 Subject: [PATCH] Fix comments for af72371db217 --- .../android/apps/mytracks/util/FileUtils.java | 15 +++++++------- .../apps/mytracks/util/FileUtilsTest.java | 20 +++++++------------ 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/FileUtils.java b/MyTracks/src/com/google/android/apps/mytracks/util/FileUtils.java index 3e203ffea..652e31a35 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/FileUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/FileUtils.java @@ -32,7 +32,8 @@ import java.util.TimeZone; 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 static final int MAX_FAT32_PATH_LENGTH = 260; @@ -143,12 +144,11 @@ public class FileUtils { */ @VisibleForTesting String sanitizeFileName(String name) { - StringBuffer buffer = new StringBuffer(); + StringBuffer buffer = new StringBuffer(name.length()); for (int i = 0; i < name.length(); i++) { int codePoint = name.codePointAt(i); - if (codePoint > 127) { - buffer.appendCodePoint(codePoint); - } else if (Character.isLetterOrDigit(name.charAt(i))) { + char character = name.charAt(i); + if (Character.isLetterOrDigit(character) || codePoint > 127 || character == '-') { buffer.appendCodePoint(codePoint); } else { 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. * * @param directory directory @@ -168,8 +168,7 @@ public class FileUtils { */ @VisibleForTesting String truncateFileName(File directory, String name, String suffix) { - - // 1 at the end accounts for the trailing NUL character + // 1 at the end accounts for the FAT32 filename trailing NUL character int requiredLength = directory.getPath().length() + suffix.length() + 1; if (name.length() + requiredLength > MAX_FAT32_PATH_LENGTH) { int limit = MAX_FAT32_PATH_LENGTH - requiredLength; diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/util/FileUtilsTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/util/FileUtilsTest.java index a738a2861..aa1765f3d 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/util/FileUtilsTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/util/FileUtilsTest.java @@ -31,9 +31,6 @@ import junit.framework.TestCase; * @author Rodrigo Damazio */ 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 Set existingFiles; @@ -66,7 +63,10 @@ public class FileUtilsTest extends TestCase { } 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() { @@ -78,16 +78,10 @@ public class FileUtilsTest extends TestCase { } 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); - assertEquals(expectedString, truncated); + for (int i = 0; i < truncated.length(); i++) { + assertEquals('a', truncated.charAt(i)); + } assertEquals(FileUtils.MAX_FAT32_PATH_LENGTH, new File(directory, truncated + suffix).getPath().length()); }