Skip to content

Commit

Permalink
Example file type detector
Browse files Browse the repository at this point in the history
  • Loading branch information
fge committed Apr 18, 2015
1 parent 6526633 commit 9cb66ff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
@@ -0,0 +1,39 @@
package com.github.fge.filesystem.ftd;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.spi.FileTypeDetector;
import java.util.Arrays;

public final class PngFileTypeDetector
extends FileTypeDetector
{
private static final byte[] PNG_HEADER = {
(byte) 0x89,
(byte) 0x50, (byte) 0x4E, (byte) 0x47,
(byte) 0x0D, (byte) 0x0A,
(byte) 0x1A,
(byte) 0x0A
};

private static final int PNG_HEADER_SIZE = PNG_HEADER.length;

@Override
public String probeContentType(final Path path)
throws IOException
{
final byte[] buf = new byte[PNG_HEADER_SIZE];

try (
final InputStream in = Files.newInputStream(path);
) {
if (in.read(buf) != PNG_HEADER_SIZE)
return null;
}

return Arrays.equals(buf, PNG_HEADER) ? "image/png" : null;
}
}
@@ -0,0 +1 @@
com.github.fge.filesystem.ftd.PngFileTypeDetector

0 comments on commit 9cb66ff

Please sign in to comment.