DirectoryStreamFilters.newContentTypeFilter

Rémi Forax forax at univ-mlv.fr
Wed Oct 22 13:49:43 PDT 2008


Hi alan, hi all,
DirectoryStreamFilters.newContentTypeFilter signature can be changed
a little to use the method inference algorithm.

public static DirectoryStream.Filter<FileRef> 
newContentTypeFilter(String type)
can be written:
public static <T extends FileRef> DirectoryStream.Filter<T> 
newContentTypeFilter(String type)

In that case, the following code is legal:
DirectoryStream.Filter<FileRef> 
filter1=DirectoryStreamFilters.newContentTypeFilter("text/html");
DirectoryStream.Filter<Path> 
filter2=DirectoryStreamFilters.newContentTypeFilter("text/html");

Rémi
--------------------------------------------------------------------------------------------------------------------------------
public static DirectoryStream.Filter<T extends FileRef> 
newContentTypeFilter(String type) {
        final MimeType matchType = MimeType.parse(type);
        if (matchType.hasParameters())
            throw new IllegalArgumentException("Parameters not allowed");
        return new DirectoryStream.Filter<T>() {
            @Override
            public boolean accept(T entry) {
                String fileType;
                try {
                    fileType = Files.probeContentType(entry);
                } catch (IOException x) {
                    throw new IOError(x);
                }
                if (fileType != null) {
                    return matchType.match(fileType);
                }
                return false;
            }
        };
    }



More information about the nio-dev mailing list