Class FileUtilities


  • public class FileUtilities
    extends java.lang.Object
    Download a zip archive of all output files within an AnalysisSubmission
    • Constructor Summary

      Constructors 
      Constructor Description
      FileUtilities()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void createAnalysisOutputFileZippedResponse​(javax.servlet.http.HttpServletResponse response, java.lang.String fileName, java.util.Set<AnalysisOutputFile> files)
      Utility method for download a zip file containing all output files from an analysis.
      static void createBatchAnalysisOutputFileZippedResponse​(javax.servlet.http.HttpServletResponse response, java.lang.String fileName, java.util.Map<ProjectSampleAnalysisOutputInfo,​AnalysisOutputFile> files)
      Utility method for download a zip file containing all output files from an analysis.
      static void createSingleFileResponse​(javax.servlet.http.HttpServletResponse response, AnalysisOutputFile file)
      Utility method for download single file from an analysis.
      static void createSingleFileResponse​(javax.servlet.http.HttpServletResponse response, AnalysisOutputFile file, java.lang.String fileName)
      Utility method for download single file from an analysis.
      static java.util.List<java.lang.String> getExcelSheetNames​(org.apache.poi.ss.usermodel.Workbook workbook)
      Extract the sheet names from the excel workbook
      static java.lang.String getFileExt​(java.nio.file.Path filepath)
      Get file extension from filepath.
      static java.lang.String getUniqueFilename​(java.nio.file.Path filePath, java.lang.String sampleName, java.lang.Long sampleId, java.lang.Long analysisSubmissionId)
      Get a unique filename for a filePath from an analysis output file.
      static java.util.List<ExcelHeader> getWorkbookHeaders​(org.apache.poi.ss.usermodel.Row row)
      Extract the headers from an excel file.
      static java.lang.String humanReadableByteCount​(long bytes, boolean si)
      From (http://stackoverflow.com/questions/3758606/how-to-convert-byte-size- into-human-readable-format-in-java)
      static boolean isZippedFile​(java.nio.file.Path path)
      Determine if a file is a zip file.
      static ExcelData parseExcelFile​(AnalysisOutputFile outputFile, int sheetIndex)
      Parse the data from an AnalysisOutputFile excel file.
      static java.lang.String readChunk​(java.io.RandomAccessFile raf, java.lang.Long seek, java.lang.Long chunk)
      Read bytes of length chunk of a file starting at byte seek.
      static java.util.List<java.lang.String> readLinesFromFilePointer​(java.io.RandomAccessFile randomAccessFile, java.lang.Long limit)
      Read lines from file using a RandomAccessFile.
      static java.util.List<java.lang.String> readLinesLimit​(java.io.BufferedReader reader, java.lang.Long limit, java.lang.Long start, java.lang.Long end)
      Read a specified number of lines from a file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • CONTENT_DISPOSITION

        public static final java.lang.String CONTENT_DISPOSITION
        See Also:
        Constant Field Values
      • ATTACHMENT_FILENAME

        public static final java.lang.String ATTACHMENT_FILENAME
        See Also:
        Constant Field Values
      • CONTENT_TYPE_APPLICATION_ZIP

        public static final java.lang.String CONTENT_TYPE_APPLICATION_ZIP
        See Also:
        Constant Field Values
      • CONTENT_TYPE_TEXT

        public static final java.lang.String CONTENT_TYPE_TEXT
        See Also:
        Constant Field Values
      • EXTENSION_HTML_ZIP

        public static final java.lang.String EXTENSION_HTML_ZIP
        See Also:
        Constant Field Values
    • Constructor Detail

      • FileUtilities

        public FileUtilities()
    • Method Detail

      • createAnalysisOutputFileZippedResponse

        public static void createAnalysisOutputFileZippedResponse​(javax.servlet.http.HttpServletResponse response,
                                                                  java.lang.String fileName,
                                                                  java.util.Set<AnalysisOutputFile> files)
        Utility method for download a zip file containing all output files from an analysis.
        Parameters:
        response - HttpServletResponse
        fileName - Name fo the file to create
        files - Set of AnalysisOutputFile
      • createBatchAnalysisOutputFileZippedResponse

        public static void createBatchAnalysisOutputFileZippedResponse​(javax.servlet.http.HttpServletResponse response,
                                                                       java.lang.String fileName,
                                                                       java.util.Map<ProjectSampleAnalysisOutputInfo,​AnalysisOutputFile> files)
        Utility method for download a zip file containing all output files from an analysis.
        Parameters:
        response - HttpServletResponse
        fileName - Name fo the file to create
        files - Set of AnalysisOutputFile
      • createSingleFileResponse

        public static void createSingleFileResponse​(javax.servlet.http.HttpServletResponse response,
                                                    AnalysisOutputFile file,
                                                    java.lang.String fileName)
        Utility method for download single file from an analysis.
        Parameters:
        response - HttpServletResponse
        file - Set of AnalysisOutputFile
        fileName - Filename
      • createSingleFileResponse

        public static void createSingleFileResponse​(javax.servlet.http.HttpServletResponse response,
                                                    AnalysisOutputFile file)
        Utility method for download single file from an analysis.
        Parameters:
        response - HttpServletResponse
        file - Set of AnalysisOutputFile
      • getFileExt

        public static java.lang.String getFileExt​(java.nio.file.Path filepath)
        Get file extension from filepath.

        Uses simple regex to parse file extension ^.*\.(\w+)$.

        Parameters:
        filepath - The Path of a file to retrieve ext.
        Returns:
        File extension if found; otherwise empty string
      • getUniqueFilename

        public static java.lang.String getUniqueFilename​(java.nio.file.Path filePath,
                                                         java.lang.String sampleName,
                                                         java.lang.Long sampleId,
                                                         java.lang.Long analysisSubmissionId)
        Get a unique filename for a filePath from an analysis output file.
        Parameters:
        filePath - The Path of the file to generate a unique filename for
        sampleName - The name of the Sample associated with the analysis
        sampleId - The id of the Sample associated with the analysis
        analysisSubmissionId - The id of the AnalysisSubmission that generated the file
        Returns:
        Unique filename of the format SAMPLENAME-sampleId-SAMPLEID-analysisSubmissionId-ANALYSISSUBMISSIONID-ORIGFILENAME
      • readChunk

        public static java.lang.String readChunk​(java.io.RandomAccessFile raf,
                                                 java.lang.Long seek,
                                                 java.lang.Long chunk)
                                          throws java.io.IOException
        Read bytes of length chunk of a file starting at byte seek.
        Parameters:
        raf - File reader
        seek - FilePointer position to start reading at
        chunk - Number of bytes to read from file
        Returns:
        Chunk of file as String
        Throws:
        java.io.IOException - if error enountered while reading file
      • readLinesLimit

        public static java.util.List<java.lang.String> readLinesLimit​(java.io.BufferedReader reader,
                                                                      java.lang.Long limit,
                                                                      java.lang.Long start,
                                                                      java.lang.Long end)
        Read a specified number of lines from a file.
        Parameters:
        reader - File reader
        limit - Limit to the number of lines to read
        start - Optional line number to start reading at
        end - Optional line number to read up to
        Returns:
        Lines read from file
      • readLinesFromFilePointer

        public static java.util.List<java.lang.String> readLinesFromFilePointer​(java.io.RandomAccessFile randomAccessFile,
                                                                                java.lang.Long limit)
                                                                         throws java.io.IOException
        Read lines from file using a RandomAccessFile.

        Use this method if preserving the RandomAccessFile.getFilePointer() for continuing reading is important. For most use cases, readLinesLimit(BufferedReader, Long, Long, Long) will perform better due to bufffered reading.

        Parameters:
        randomAccessFile - File reader
        limit - Limit to the number of lines to read
        Returns:
        Lines read from file
        Throws:
        java.io.IOException - if error enountered while reading file
      • getWorkbookHeaders

        public static java.util.List<ExcelHeader> getWorkbookHeaders​(org.apache.poi.ss.usermodel.Row row)
        Extract the headers from an excel file.
        Parameters:
        row - Row First row from the excel file.
        Returns:
        List of ExcelHeader header values.
      • getExcelSheetNames

        public static java.util.List<java.lang.String> getExcelSheetNames​(org.apache.poi.ss.usermodel.Workbook workbook)
        Extract the sheet names from the excel workbook
        Parameters:
        workbook - Workbook The excel workbook to get sheet names from
        Returns:
        List of String sheet names.
      • humanReadableByteCount

        public static java.lang.String humanReadableByteCount​(long bytes,
                                                              boolean si)
        From (http://stackoverflow.com/questions/3758606/how-to-convert-byte-size- into-human-readable-format-in-java)
        Parameters:
        bytes - The Long size of the file in bytes.
        si - Boolean true to use si units
        Returns:
        A human readable String representation of the file size.
      • isZippedFile

        public static boolean isZippedFile​(java.nio.file.Path path)
                                    throws java.io.IOException
        Determine if a file is a zip file.
        Parameters:
        path - The Path to the file to test.
        Returns:
        A boolean indicating whether the file is a zip file.
        Throws:
        java.io.IOException - If an invalid Path is passed