Examples of DataUtilException


Examples of com.btaz.util.DataUtilException

     * @param dir directory
     * @param regex regular expression
     */
    public static void deleteFilesByRegex(File dir, String regex) {
        if(regex == null) {
            throw new DataUtilException("Filename regex can not be null");
        }
        FilenameFilter filter = new RegexFilenameFilter(regex);
        FileDeleter.deleteFiles(dir, filter);
    }
View Full Code Here

Examples of com.btaz.util.DataUtilException

     * @param filter filename filter, if null then delete all files
     */
    public static void deleteFiles(File dir, FilenameFilter filter) {
        // validations
        if(dir == null) {
            throw new DataUtilException("The delete directory parameter can not be a null value");
        } else if(!dir.exists() || !dir.isDirectory()) {
            throw new DataUtilException("The delete directory does not exist: " + dir.getAbsolutePath());
        }

        // delete files
        File [] files;
        if(filter == null) {
            files = dir.listFiles();
        } else {
            files = dir.listFiles(filter);
        }
        if (files != null) {
            for(File file : files) {
                if(file.isFile()) {
                    if(!file.delete()) {
                        throw new DataUtilException("Failed to delete file: " + file.getAbsolutePath());
                    }
                }
            }
        }
    }
View Full Code Here

Examples of com.btaz.util.DataUtilException

     * @return <code>List</code> of <code>FilePart</code> items
     */
    public static List<File> split(File splitDir, File inputFile, long maxBytes, boolean skipHeader) {
        // validations
        if(splitDir == null) {
            throw new DataUtilException("The split directory parameter can not be a null value");
        } else if(!splitDir.exists()) {
            throw new DataUtilException("The split directory is not a valid path");
        }
        if(inputFile == null) {
            throw new DataUtilException("The input file parameter can not be a null value");
        } else if(!inputFile.exists()) {
            throw new DataUtilException("The input file doesn't exist");
        }
        if(maxBytes < DEFAULT_MIN_BYTES) {
            throw new DataUtilException("Invalid max bytes specification: " + maxBytes + ", minimum is: "
                    + DEFAULT_MIN_BYTES);
        }

        ArrayList<File> splitFiles = new ArrayList<File>();

        // open file
        FileInputStream inputStream = null;
        LinkedList<String> rows = new LinkedList<String>();
        try {
            // create FilePart
            inputStream = new FileInputStream(inputFile);
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            long charsTotal = 0;
            String line;

            int maxChunk = 60;
            int rowNumber = 0;
            while((line=br.readLine()) != null) {
                rowNumber += 1;
                if(skipHeader && rowNumber == 1) {
                    continue;
                }
                if(storageCalculation(rows.size(), line.length(), charsTotal) > maxBytes) {
                    if(maxChunk-- <= 0) {
                        break;
                    }
                    // we have to stop or we may exceed max part size
                    File splitFile = writePartToFile(splitDir, rows);
                    splitFiles.add(splitFile);
                    rows.clear();
                    charsTotal = 0;
                }
                rows.add(line);
                charsTotal += line.length() + DataUtilDefaults.lineTerminator.length();
            }
            // no more data, finish up the final part
            if(charsTotal > 0) {
                File splitFile = writePartToFile(splitDir, rows);
                splitFiles.add(splitFile);
            }

            inputStream.close();
            inputStream = null;

        } catch (FileNotFoundException e) {
            throw new DataUtilException(e);
        } catch (UnsupportedEncodingException e) {
            throw new DataUtilException(e);
        } catch (IOException e) {
            throw new DataUtilException(e);
        } finally {
            if(inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
View Full Code Here

Examples of com.btaz.util.DataUtilException

            }
            writer.flush();
            writer.close();
            writer = null;
        } catch (UnsupportedEncodingException e) {
            throw new DataUtilException(e);
        } catch (FileNotFoundException e) {
            throw new DataUtilException(e);
        } catch (IOException e) {
            throw new DataUtilException(e);
        } finally {
            if(writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
View Full Code Here

Examples of com.btaz.util.DataUtilException

                           String pageDescription, int tableColumns, int maxRowsPerPage) {
        encoding = "UTF8";

        // validations
        if(outputDirectory == null) {
            throw new DataUtilException("The outputDirectory field can not be a null value");
        }
        if(!outputDirectory.exists()) {
            throw new DataUtilException("The output directory must exist: " + outputDirectory.getAbsolutePath());
        }
        if(filenamePrefix == null) {
            throw new DataUtilException("The filenamePrefix field can not be a null value");
        }
        if(filenamePrefix.length() < 1) {
            throw new DataUtilException("The filenamePrefix field must contain at least one character");
        }
        if(pageTitle == null) {
            pageTitle = "";
        }
        if(tableColumns < 1) {
            throw new DataUtilException("You must have at least one column. Invalid column value: " + tableColumns);
        }

        this.tableColumns = tableColumns;
        this.outputDirectory = outputDirectory;
        this.filenamePrefix = filenamePrefix;
View Full Code Here

Examples of com.btaz.util.DataUtilException

     * @param columns columns
     * @throws DataUtilException exception
     */
    public void write(String... columns) throws DataUtilException {
        if(columns.length != tableColumns) {
            throw new DataUtilException("Invalid column count. Expected " + tableColumns + " but found: "
                    + columns.length);
        }
        try {
            if(currentRow == 0) {
                // capture header row
                this.headerRowColumns = columns;
                writeTop();
                writeRow(headerRowColumns);
            } else if(maxRowsPerPage > 1 && currentRow % maxRowsPerPage == 0) {
                writeBottom(true);
                currentPageNumber = (currentRow/maxRowsPerPage) + 1;
                writeTop();
                writeRow(headerRowColumns);
                currentRow += 1;
                writeRow(columns);
            } else {
                writeRow(columns);
            }
            currentRow += 1;
        } catch (Exception e) {
            throw new DataUtilException(e);
        }
    }
View Full Code Here

Examples of com.btaz.util.DataUtilException

        try {
            if(writer != null) {
                writeBottom(false);
            }
        } catch (IOException e) {
            throw new DataUtilException("Failed to close the HTML table file", e);
        }
    }
View Full Code Here

Examples of com.btaz.util.DataUtilException

     */
    public static List<File> map(File workDir, List<File> inputFiles, Mapper mapper)
            throws DataUtilException {
        // validations
        if(workDir == null) {
            throw new DataUtilException("The workDir parameter can not be a null value");
        }
        if(inputFiles == null) {
            throw new DataUtilException("The inputFiles parameter can not be a null value");
        }
        if(mapper == null) {
            throw new DataUtilException("The mappable parameter can not be a null value");
        }

        // setup output collector
        OutputCollector collector = new OutputCollector(workDir, "map");

        // map all data
        try {
            for(File file : inputFiles) {
                FileInputStream inputStream;

                try {
                    inputStream = new FileInputStream(file);
                    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,
                            DataUtilDefaults.charSet));

                    // map data
                    String row;
                    while((row = br.readLine()) != null) {
                        mapper.map(row, collector);
                    }

                    inputStream.close();
                } catch (IOException e) {
                    throw new DataUtilException(e);
                } catch (MapReduceException e) {
                    throw new DataUtilException("Irrecoverable map operation", e);
                }
            }
        } finally {
            collector.close();
        }
View Full Code Here

Examples of com.btaz.util.DataUtilException

     */
    public static void reduce(File inputFile, File outputFile, Reducer reducer, KeyComparator comparator)
            throws DataUtilException {
        // validations
        if(inputFile == null) {
            throw new DataUtilException("The inputFile parameter can not be a null value");
        }
        if(outputFile == null) {
            throw new DataUtilException("The outputFile parameter can not be a null value");
        }
        if(reducer == null) {
            throw new DataUtilException("The reducable parameter can not be a null value");
        }
        if(comparator == null) {
            throw new DataUtilException("The comparator parameter can not be a null value");
        }

        // reduce all data
        FileInputStream inputStream;
        FileOutputCollector collector = new FileOutputCollector(outputFile);
        QueueReader queueReader;

        // Aggregate all items matching the comparator and call the Reducable callback
        try {
            inputStream = new FileInputStream(inputFile);
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,
                    DataUtilDefaults.charSet));
            queueReader = new QueueReader(br);

            // reduce data
            String prev = null;
            String curr;
            ArrayList<String> items = new ArrayList<String>();
            while(true) {
                curr = queueReader.readLine();
                if(curr == null) {
                    // no more data
                    reduceItems(collector, reducer, items);
                    break;
                } else if(prev == null) {
                    // first row in a new batch
                    items.add(curr);
                    prev = curr;
                } else if(comparator.compare(prev, curr) == 0) {
                    // same keys
                    items.add(curr);
                    prev = curr;
                } else {
                    // different keys
                    queueReader.push(curr);
                    reduceItems(collector, reducer, items);
                    items.clear();
                    prev = null;
                }
            }
            collector.close();
            inputStream.close();
        } catch (IOException e) {
            throw new DataUtilException(e);
        } catch (MapReduceException e) {
            throw new DataUtilException("Irrecoverable reduce operation", e);
        }
    }
View Full Code Here

Examples of com.btaz.util.DataUtilException

     * @return <code>List</code> of <code>FilePart</code> items
     */
    public static List<File> split(File splitDir, File inputFile, long maxBytes, boolean skipHeader) {
        // validations
        if(splitDir == null) {
            throw new DataUtilException("The split directory parameter can not be a null value");
        } else if(!splitDir.exists()) {
            throw new DataUtilException("The split directory is not a valid path");
        }
        if(inputFile == null) {
            throw new DataUtilException("The input file parameter can not be a null value");
        } else if(!inputFile.exists()) {
            throw new DataUtilException("The input file doesn't exist");
        }
        if(maxBytes < DEFAULT_MIN_BYTES) {
            throw new DataUtilException("Invalid max bytes specification: " + maxBytes + ", minimum is: "
                    + DEFAULT_MIN_BYTES);
        }

        ArrayList<File> splitFiles = new ArrayList<File>();

        // open file
        FileInputStream inputStream = null;
        LinkedList<String> rows = new LinkedList<String>();
        try {
            // create FilePart
            inputStream = new FileInputStream(inputFile);
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            long charsTotal = 0;
            String line;

            int rowNumber = 0;
            while((line=br.readLine()) != null) {
                rowNumber += 1;
                if(skipHeader && rowNumber == 1) {
                    continue;
                }
                if(storageCalculation(rows.size(), line.length(), charsTotal) > maxBytes) {
                    // we have to stop or we may exceed max part size
                    File splitFile = writePartToFile(splitDir, rows);
                    splitFiles.add(splitFile);
                    rows.clear();
                    charsTotal = 0;
                }
                rows.add(line);
                charsTotal += line.length() + DataUtilDefaults.lineTerminator.length();
            }
            // no more data, finish up the final part
            if(charsTotal > 0) {
                File splitFile = writePartToFile(splitDir, rows);
                splitFiles.add(splitFile);
            }

            inputStream.close();
            inputStream = null;

        } catch (FileNotFoundException e) {
            throw new DataUtilException(e);
        } catch (UnsupportedEncodingException e) {
            throw new DataUtilException(e);
        } catch (IOException e) {
            throw new DataUtilException(e);
        } finally {
            if(inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.