Skip to content

Commit

Permalink
Merge pull request #190 from bhamail/master
Browse files Browse the repository at this point in the history
Fix 'String index out of range: 5' error when video device file has no s...
  • Loading branch information
sarxos committed Mar 9, 2014
2 parents 061a27a + 2004517 commit 7743c7a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
7 changes: 7 additions & 0 deletions webcam-capture-drivers/driver-gstreamer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
</dependency>
-->
<!-- end uncomment -->

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class VideoDeviceFilenameFilter implements FilenameFilter {

@Override
public boolean accept(File dir, String name) {
return dir.getName().equals("dev") && name.startsWith("video") && Character.isDigit(name.charAt(5));
return dir.getName().equals("dev") && name.startsWith("video")
&& (name.length() > 5 && Character.isDigit(name.charAt(5)));
}

public File[] getVideoFiles() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.sarxos.webcam.ds.gstreamer.impl;

import org.junit.Test;

import java.io.File;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* @author Dan Rollo
* Date: 3/8/14
* Time: 10:44 PM
*/
public class VideoDeviceFilenameFilterTest {

/**
* Accept method was failing with exception: String index out of range: 5
* This occurs on opensuse 11 where video device files do not all have a suffix. The files are created like so:
* $ ls -l /dev/video*
* /dev/video -> video0
* /dev/video0
*
* In this case, the link name 'video' is less that 6 characters long, so the filter statement:
* Character.isDigit(name.charAt(5))
* causes the exception.
*
* Fix is to also check for length before checking for isDigit().
*/
@Test
public void testAcceptHandlesShortVideoDeviceFilename() {
final VideoDeviceFilenameFilter videoDeviceFilenameFilter = new VideoDeviceFilenameFilter();
assertFalse(videoDeviceFilenameFilter.accept(new File("/dev"), "video"));
assertTrue(videoDeviceFilenameFilter.accept(new File("/dev"), "video0"));
}
}

0 comments on commit 7743c7a

Please sign in to comment.