class CSVDetector implements Detector {

    @Override
    public Format detect(Metadata metadata, TikaInputStream inputStream) throws IOException {

        Format result = detectText(metadata, inputStream);

        if (result == null) {
            inputStream.mark(FormatUtils.META_TAG_BUFFER_SIZE);
            byte[] buffer = new byte[FormatUtils.META_TAG_BUFFER_SIZE];
            int n = 0;

            for (int m = inputStream.read(buffer); m != -1 && n < buffer.length; m =
                    inputStream.read(buffer, n, buffer.length - n)) {
                n += m;
            }

            inputStream.reset();
            String head = FormatUtils.readFromBuffer(buffer, 0, n);

            try (InputStream stream = TikaInputStream.get(IOUtils.toInputStream(head))) {
                result = detectText(new Metadata(), stream);
            }
        }
        return result;
    }

}