0

am cloning xml file in my java code in a such way:

 public boolean isCrcCorrect(Path path) throws IOException, XPathExpressionException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    if (!fileData.currentFilePath.equals(path.toString())) {
        parseFile(path);
    }

    List<String> file_lines = Files.readAllLines(path);
    //BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(output));
    BufferedWriter bw = new BufferedWriter(new FileWriter(new File("f:\\Projects\\iqpdct\\iqpdct-domain\\src\\main\\java\\de\\iq2dev\\domain\\util\\debug.xml")));

    for (int i = 0; i < file_lines.size(); i++) {
        if (i != 0) {
            bw.write("\n");
        }

        if (file_lines.get(i).equals("  <Stamp crc=\"3916602279\"><Checker name=\"IODD-Checker V1.1.1\" version=\"V1.1.1.0\"/></Stamp>")) {
            bw.write("  <Stamp crc=\"\"><Checker name=\"IODD-Checker V1.1.1\" version=\"V1.1.1.0\"/></Stamp>");
        } else {
            bw.write(file_lines.get(i));
        }

        System.out.println(file_lines.get(i));
    }
    bw.flush();
    bw.close();

    crc.reset();
    crc.update(output.toByteArray());

    //debug
    System.out.println(crc.getValue());
    System.out.println(fileData.file_crc);
    //return fileData.file_crc == crc.getValue();
    return false;
}

I need to modiy attribute in xml (for debug I manualy made it in cycle). I required this clone for checksum calculating. Result file "debug.xml" is identical to source (InteliJ IDEA told so), but size is different: source: 41395, clone:40608 , so consequently I have incorrect checksum (CRC32 function)
What can causes this?

Constantine
  • 1,802
  • 3
  • 23
  • 37

2 Answers2

1

Just a guess, but you seem to hardcode new lines as \n. Would it be possible that your input file has Windows style line endings and you are modifying them with your code ?

Guillaume
  • 18,494
  • 8
  • 53
  • 74
  • Lets God blesses you, good man. It was a problem, line endings. I spent almost 3 hours for that. – Constantine Nov 01 '14 at 12:47
  • It all depends on what your intention is. I guess your goal is not just to write a program that parse a file and output this exact same file (if this is actually what you need, good news `cp` already does exactly that). – Guillaume Nov 01 '14 at 13:41
  • You might want to have a look at http://stackoverflow.com/questions/207947/java-how-do-i-get-a-platform-independent-new-line-character but the main question is "which line endings do you need, platform dependent or not ...". – Guillaume Nov 01 '14 at 13:43
0

Not sure here...but when you are replacing the line <Stamp crc=\"3916602279\"><Checker name=\"IODD-Checker V1.1.1\" version=\"V1.1.1.0\"/></Stamp> with <Stamp crc=\"\"><Checker name=\"IODD-Checker V1.1.1\" version=\"V1.1.1.0\"/></Stamp>, perhaps that is causing the difference?

Just try not doing that and see if it produces the same sized clone file. Try that for me please.

Moreover, as Guillaume said, does the difference in \n for windows and mac have different sizes?

In a completely different context, somebody correct me if I am wrong, but doesn't bw.close() automatically call bw.flush()? I thought it does.

Rakshith Ravi
  • 365
  • 5
  • 16