Java Path File Write nio compareTable(final String table, final List d1, final List d2, final BufferedWriter errors)

Here you can find the source of compareTable(final String table, final List d1, final List d2, final BufferedWriter errors)

Description

compare Table

License

Open Source License

Declaration

private static boolean compareTable(final String table,
            final List<Path> d1, final List<Path> d2,
            final BufferedWriter errors) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;

import java.util.List;
import java.util.Map;

import java.util.zip.GZIPInputStream;

public class Main {
    private static boolean compareTable(final String table,
            final List<Path> d1, final List<Path> d2,
            final BufferedWriter errors) throws IOException {
        final Map<String, Integer> d1LineMap = new HashMap<String, Integer>();
        for (final Path dir : d1) {
            final Path tableDir = dir.resolve(table);
            if (!(Files.exists(tableDir) && Files.isDirectory(tableDir))) {
                errors.write("Dump directory " + tableDir
                        + " doesn't exist");
                return false;
            }/*from w ww .  j a  va2 s  .  c  om*/
            buildLineMap(d1LineMap, tableDir);
        }

        boolean passed = true;
        for (final Path dir : d2) {
            final Path tableDir = dir.resolve(table);
            if (!(Files.exists(tableDir) && Files.isDirectory(tableDir))) {
                errors.write("Dump directory " + tableDir
                        + " doesn't exist");
                return false;
            }
            passed &= removeMatchingLines(d1LineMap, tableDir, table,
                    errors);
        }

        for (final String line : d1LineMap.keySet()) {
            for (int i = 0; i < d1LineMap.get(line); i++) {
                passed = false;
                errors.write("< " + table + ":" + line);
            }
        }
        return passed;
    }

    private static void buildLineMap(final Map<String, Integer> d1LineMap,
            final Path tableDir) throws IOException {
        try (DirectoryStream<Path> listing = Files
                .newDirectoryStream(tableDir)) {
            for (final Path file : listing) {
                try (BufferedReader in = getReaderForFile(file)) {
                    String line = in.readLine();
                    while (line != null) {
                        if (d1LineMap.containsKey(line)) {
                            d1LineMap.put(line, d1LineMap.get(line) + 1);
                        } else {
                            d1LineMap.put(line, 1);
                        }
                        line = in.readLine();
                    }
                }
            }
        }
    }

    private static boolean removeMatchingLines(
            final Map<String, Integer> d1LineMap, final Path tableDir,
            final String table, final BufferedWriter errors)
            throws IOException {
        boolean passed = true;
        try (DirectoryStream<Path> listing = Files
                .newDirectoryStream(tableDir)) {
            for (final Path file : listing) {
                try (BufferedReader in = getReaderForFile(file)) {
                    String line = in.readLine();
                    while (line != null) {
                        if (!d1LineMap.containsKey(line)) {
                            passed = false;
                            errors.write("> " + table + ": " + line);
                        }
                        if (d1LineMap.get(line) == 1) {
                            d1LineMap.remove(line);
                        } else {
                            d1LineMap.put(line, d1LineMap.get(line) - 1);
                        }
                        line = in.readLine();
                    }
                }
            }
        }
        return passed;
    }

    private static BufferedReader getReaderForFile(final Path file)
            throws FileNotFoundException, IOException {
        final String filename = file.getFileName().toString().toLowerCase();
        if (filename.endsWith(".gz")) {
            return new BufferedReader(new InputStreamReader(
                    new GZIPInputStream(Files.newInputStream(file))));
        }
        return Files.newBufferedReader(file);
    }
}

Related

  1. canWrite(Path path)
  2. checkFilePermissions(final String filepath, final boolean canRead, final boolean canWrite)
  3. isWritePermission(Path p)
  4. newBufferedWriter(Path path)
  5. newBufferedWriter(Path path, OpenOption... options)
  6. newGzipBufferedWriter(Path path)