Skip to content

Commit 82e18be

Browse files
authored
Fixed % in path bug. (#33)
1 parent a7f231b commit 82e18be

3 files changed

Lines changed: 82 additions & 11 deletions

File tree

NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
- 0.9.56
22
- Fixed deployment to tagtraum.
33
- Fixed PCM endianness bug.
4+
- Fixed % in path bug.
45

56

67
- 0.9.55

ffsampledsp-complete/src/test/java/com/tagtraum/ffsampledsp/TestFFAudioFileReader.java

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,84 @@ public void testBogusFile() throws IOException {
517517

518518
@Test
519519
public void testFileWithPunctuationToURL() throws MalformedURLException {
520+
// What matters is that all punctuation survives the fileToURL → urlToString round-trip
521+
// so FFmpeg receives the correct literal path. The intermediate URL encoding may vary.
520522
Assume.assumeTrue(File.separator.equals("/"));
521523
final File file = new File("/someDir/;:&=+@[]?/name.txt");
522524
final URL url = FFAudioFileReader.fileToURL(file);
523-
assertEquals("file:/someDir/;:&=+@[]?/name.txt", url.toString());
525+
final String s = FFAudioFileReader.urlToString(url);
526+
assertTrue(
527+
"Punctuation must survive round-trip: " + s,
528+
s.contains("/someDir/")
529+
&& s.contains(";")
530+
&& s.contains("&")
531+
&& s.contains("+")
532+
&& s.contains("[")
533+
&& s.contains("]")
534+
&& s.endsWith("name.txt"));
535+
}
536+
537+
@Test
538+
public void testFileWithPercentSignToURL() throws MalformedURLException {
539+
// A literal % in a file path must survive the fileToURL → urlToString round-trip intact.
540+
Assume.assumeTrue(File.separator.equals("/"));
541+
final File file = new File("/someDir/50%off/name.ogg");
542+
final URL url = FFAudioFileReader.fileToURL(file);
543+
final String s = FFAudioFileReader.urlToString(url);
544+
assertTrue("Round-tripped path must contain literal percent sign: " + s, s.contains("50%off"));
545+
}
546+
547+
@Test
548+
public void testGetAudioFileFormatFileWithPercentSign()
549+
throws IOException, UnsupportedAudioFileException {
550+
final String filename = "test.ogg";
551+
final File file = File.createTempFile("test50%off", filename);
552+
extractFile(filename, file);
553+
try {
554+
final AudioFileFormat fileFormat = new FFAudioFileReader().getAudioFileFormat(file);
555+
assertEquals("ogg", fileFormat.getType().getExtension());
556+
assertEquals(2, fileFormat.getFormat().getChannels());
557+
} finally {
558+
file.delete();
559+
}
560+
}
561+
562+
@Test
563+
public void testGetAudioFileFormatURLWithPercentSign()
564+
throws IOException, UnsupportedAudioFileException {
565+
// file.toURI().toURL() keeps % encoded as %25; urlToString() must decode it for FFmpeg.
566+
final String filename = "test.ogg";
567+
final File file = File.createTempFile("test50%off", filename);
568+
extractFile(filename, file);
569+
try {
570+
final AudioFileFormat fileFormat =
571+
new FFAudioFileReader().getAudioFileFormat(file.toURI().toURL());
572+
assertEquals("ogg", fileFormat.getType().getExtension());
573+
assertEquals(2, fileFormat.getFormat().getChannels());
574+
} finally {
575+
file.delete();
576+
}
577+
}
578+
579+
@Test
580+
public void testGetAudioInputStreamFileWithPercentSign()
581+
throws IOException, UnsupportedAudioFileException {
582+
final String filename = "test.ogg";
583+
final File file = File.createTempFile("test50%off", filename);
584+
extractFile(filename, file);
585+
try {
586+
final AudioInputStream stream = new FFAudioFileReader().getAudioInputStream(file);
587+
try {
588+
final byte[] buf = new byte[1024];
589+
assertTrue(
590+
"Expected to read audio bytes from file with percent sign in name",
591+
stream.read(buf) > 0);
592+
} finally {
593+
stream.close();
594+
}
595+
} finally {
596+
file.delete();
597+
}
524598
}
525599

526600
@Test

ffsampledsp-java/src/main/java/com/tagtraum/ffsampledsp/FFAudioFileReader.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,17 @@ public AudioFileFormat[] getAudioFileFormats(final File file)
140140
}
141141

142142
/**
143-
* Convert file to URL. Assumes that any punctuation in the filename must not be url encoded.
143+
* Convert file to URL. The returned URL keeps all path characters percent-encoded as produced by
144+
* {@link File#toURI()}, except that {@code +} is encoded as {@code %2B} so that {@link
145+
* #urlToString(URL)} (which calls {@link java.net.URLDecoder}) does not misinterpret it as a
146+
* space. All decoding for FFmpeg is done exclusively in {@code urlToString}.
144147
*
145148
* @param file file
146-
* @return correctly encoded URL
149+
* @return percent-encoded file URL suitable for passing to {@link #urlToString(URL)}
147150
* @throws MalformedURLException if the URL is malformed
148151
*/
149152
static URL fileToURL(final File file) throws MalformedURLException {
150-
try {
151-
String encoded = file.toURI().toString().replace("+", "%2B");
152-
return new URL(URLDecoder.decode(encoded, "UTF-8"));
153-
} catch (UnsupportedEncodingException e) {
154-
final MalformedURLException malformedURLException = new MalformedURLException();
155-
malformedURLException.initCause(e);
156-
throw malformedURLException;
157-
}
153+
return new URL(file.toURI().toString().replace("+", "%2B"));
158154
}
159155

160156
/**

0 commit comments

Comments
 (0)