java.util.Properties: reproducible write
Rob Spoor
openjdk at icemanx.nl
Tue Nov 26 19:22:34 UTC 2024
The comment provided in `store(Writer, String)` is added in addition to
the date comment:
if (comments != null) {
writeComments(bw, comments);
}
writeDateComment(bw);
The system property can be used to replace the date with a constant, but
a) it's not possible to omit it because an empty property value still
leads to the date being written, b) it's a global setting, not something
to apply to a single Properties instance, and c) you need to set it when
starting the JVM because its value is cached (tested in jshell).
Perhaps adding an enum Properties.StoreProperties with single constant
OMIT_DATE can be used:
// overload, not replacement; similar for OutputStream
public void store(Writer writer, String comments,
StoreProperties... properties)
This can then be called as props.store(writer, null,
Properties.StoreProperties.OMIT_DATE).
Rob
On 26/11/2024 00:23, Roger Riggs wrote:
> Hi Rafael,
>
> You might have missed the update added by JDK-8231640 <https://
> bugs.openjdk.org/browse/JDK-8231640> Canonical property storage
> The comment string can be provided either by a separate method
> `store(Writer, String)` or by a system property.
>
> Regards, Roger
>
> On 11/25/24 4:53 PM, Rafael Winterhalter wrote:
>> Hello,
>>
>> I find the java.util.Properties class to be quite convenient when
>> writing minor collections of key-values. One unfortunate implication
>> of the store method is that it always contains a comment line with the
>> current date. The comment is never deserialized when loading the file,
>> but the file hash is of course always changed, even if the content is
>> identical at a later time. This can have unfortunate implications when
>> the file hash is relevant to some form of cashing or validation
>> mechanism.
>>
>> Would it be a good idea to add an overload that allows disabling the
>> date comment? This would only require minimal code changes and the
>> storage format would become reproducible. The key-values are already
>> sorted by their key. The date can be fixed by a system property, but
>> this is not always possible to define if the writer is not in control
>> of the command line. Right now I have to reimplement the
>> Properties::store method only to exclude this line.
>>
>> Thanks for your consideration and opinion,
>> Rafael
>
More information about the core-libs-dev
mailing list