java.lang.Object
com.northconcepts.datapipeline.foundations.file.FileDetector

public class FileDetector extends Object
Detects the FileType of a file or stream from its name's extension or, failing that, by sampling its first bytes.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    Creates a detector that samples the stream's content, closing the stream once it is read.
    FileDetector(InputStream inputStream, String filename)
    Creates a detector that checks the file name's extension first and otherwise samples the stream, closing it once read.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    Returns the sampled bytes (up to 2048, see getBufferSize()), or null if the content has not been read.
    int
    Returns the number of valid bytes in getBuffer(): -1 for empty content and 0 before the content is read.
    int
    Returns the separator (comma, tab, pipe, semicolon or caret) that outnumbers all the others combined in CSV content, or -1 if the type is not CSV or none does.
    Returns the file being examined, or null when reading from a stream.
    Returns the detected type, from the name's extension if it matches one exactly, otherwise by sampling the content.
    int[]
    Returns the count of each byte value (256 entries) in the sample, or null if the content has not been read.
    boolean
    Indicates if the sample contains control characters (other than tab, CR and LF) or DEL; false until the content is read.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FileDetector

      public FileDetector(File file)
    • FileDetector

      public FileDetector(InputStream inputStream)
      Creates a detector that samples the stream's content, closing the stream once it is read.
    • FileDetector

      public FileDetector(InputStream inputStream, String filename)
      Creates a detector that checks the file name's extension first and otherwise samples the stream, closing it once read.
  • Method Details

    • getFile

      public File getFile()
      Returns the file being examined, or null when reading from a stream.
    • getBuffer

      public byte[] getBuffer()
      Returns the sampled bytes (up to 2048, see getBufferSize()), or null if the content has not been read.
    • getBufferSize

      public int getBufferSize()
      Returns the number of valid bytes in getBuffer(): -1 for empty content and 0 before the content is read.
    • getHistogram

      public int[] getHistogram()
      Returns the count of each byte value (256 entries) in the sample, or null if the content has not been read.
    • isBinary

      public boolean isBinary()
      Indicates if the sample contains control characters (other than tab, CR and LF) or DEL; false until the content is read.
    • getFileType

      public FileType getFileType()
      Returns the detected type, from the name's extension if it matches one exactly, otherwise by sampling the content. Sampled binary data is reported as XLSX, XLS or BINARY and text as XML, JSON, CSV or TEXT.
    • getCsvFieldSeparator

      public int getCsvFieldSeparator()
      Returns the separator (comma, tab, pipe, semicolon or caret) that outnumbers all the others combined in CSV content, or -1 if the type is not CSV or none does.