Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properties.store() - suppress timestamp comment

Is it possible to force Properties not to add the date comment in front? I mean something like the first line here:

#Thu May 26 09:43:52 CEST 2011
main=pkg.ClientMain
args=myargs

I would like to get rid of it altogether. I need my config files to be diff-identical unless there is a meaningful change.

like image 594
Konrad Garus Avatar asked May 31 '11 07:05

Konrad Garus


3 Answers

Guess not. This timestamp is printed in private method on Properties and there is no property to control that behaviour.

Only idea that comes to my mind: subclass Properties, overwrite store and copy/paste the content of the store0 method so that the date comment will not be printed.

Or - provide a custom BufferedWriter that prints all but the first line (which will fail if you add real comments, because custom comments are printed before the timestamp...)

like image 122
Andreas Dolk Avatar answered Oct 19 '22 07:10

Andreas Dolk


Given the source code or Properties, no, it's not possible. BTW, since Properties is in fact a hash table and since its keys are thus not sorted, you can't rely on the properties to be always in the same order anyway.

I would use a custom algorithm to store the properties if I had this requirement. Use the source code of Properties as a starter.

like image 43
JB Nizet Avatar answered Oct 19 '22 09:10

JB Nizet


Based on https://stackoverflow.com/a/6184414/242042 here is the implementation I have written that strips out the first line and sorts the keys.

public class CleanProperties extends Properties {
    private static class StripFirstLineStream extends FilterOutputStream {

        private boolean firstlineseen = false;

        public StripFirstLineStream(final OutputStream out) {
            super(out);
        }

        @Override
        public void write(final int b) throws IOException {
            if (firstlineseen) {
                super.write(b);
            } else if (b == '\n') {
                firstlineseen = true;
            }
        }

    }

    private static final long serialVersionUID = 7567765340218227372L;

    @Override
    public synchronized Enumeration<Object> keys() {
        return Collections.enumeration(new TreeSet<>(super.keySet()));
    }

    @Override
    public void store(final OutputStream out, final String comments) throws IOException {
        super.store(new StripFirstLineStream(out), null);
    }
}

Cleaning looks like this

    final Properties props = new CleanProperties();
    try (final Reader inStream = Files.newBufferedReader(file, Charset.forName("ISO-8859-1"))) {
        props.load(inStream);
    } catch (final MalformedInputException mie) {
        throw new IOException("Malformed on " + file, mie);
    }
    if (props.isEmpty()) {
        Files.delete(file);
        return;
    }

    try (final OutputStream os = Files.newOutputStream(file)) {
        props.store(os, "");
    }
like image 5
Archimedes Trajano Avatar answered Oct 19 '22 09:10

Archimedes Trajano