Skip to content

Commit

Permalink
utf8properties: Fix store(File) which did not remove leading comment
Browse files Browse the repository at this point in the history
Also other fixes to clean up store implementation.

Signed-off-by: BJ Hargrave <bj@hargrave.dev>
  • Loading branch information
bjhargrave committed May 18, 2022
1 parent 1a3c34e commit b914286
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
40 changes: 17 additions & 23 deletions aQute.libg/src/aQute/lib/utf8properties/UTF8Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -144,45 +145,38 @@ private String decode(byte[] buffer) {
return new String(buffer); // default decoding
}

private static final Pattern LINE_SPLITTER = Pattern.compile("\n\r?");

@Override
public void store(OutputStream out, String msg) throws IOException {
public void store(Writer writer, String msg) throws IOException {
CharArrayWriter sw = new CharArrayWriter();
super.store(sw, null);

String[] lines = sw.toString()
.split("\n\r?");
String[] lines = LINE_SPLITTER.split(sw.toString());

byte[] newline = "\n".getBytes(UTF_8);
for (String line : lines) {
if (line.startsWith("#"))
continue;

out.write(line.getBytes(UTF_8));
out.write(newline);
writer.write(line);
writer.write('\n');
}
}

@Override
public void store(Writer out, String msg) throws IOException {
CharArrayWriter sw = new CharArrayWriter();
super.store(sw, null);

String[] lines = sw.toString()
.split("\n\r?");

for (String line : lines) {
if (line.startsWith("#"))
continue;

out.write(line);
out.write('\n');
public void store(OutputStream out, String msg) throws IOException {
Writer writer = new OutputStreamWriter(out, UTF_8);
try {
store(writer, msg);
} finally {
writer.flush();
}
}

public void store(File out) throws IOException {
CharArrayWriter sw = new CharArrayWriter();
super.store(sw, null);
IO.store(sw.toString(), out);
public void store(File file) throws IOException {
try (OutputStream out = IO.outputStream(file)) {
store(out, null);
}
}

public void store(OutputStream out) throws IOException {
Expand Down
20 changes: 19 additions & 1 deletion aQute.libg/test/aQute/lib/utf8properties/UTF8PropertiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
Expand All @@ -12,6 +13,7 @@

import org.junit.jupiter.api.Test;

import aQute.bnd.test.jupiter.InjectTemporaryDirectory;
import aQute.lib.io.IO;
import aQute.libg.reporter.ReporterAdapter;
import aQute.service.reporter.Report.Location;
Expand Down Expand Up @@ -381,12 +383,28 @@ public void testWrite() throws IOException {
StringWriter sw = new StringWriter();
p.store(sw, null);
String s = sw.toString();
assertThat(s).contains("#comment");
assertThat(s).doesNotStartWith("#")
.contains("#comment");
UTF8Properties p1 = new UTF8Properties();
p1.load(new StringReader(s));
assertThat(p1).containsExactlyInAnyOrderEntriesOf(p);
}

@Test
public void testWriteFile(@InjectTemporaryDirectory
File tmp) throws Exception {
UTF8Properties p = new UTF8Properties();
p.put("Foo", "Foo");
p.put("Bar", "Bar");
File props = new File(tmp, "props.properties");
p.store(props);
assertThat(props).isFile();
assertThat(IO.collect(props)).doesNotStartWith("#");
UTF8Properties p1 = new UTF8Properties();
p1.load(props, null);
assertThat(p1).containsExactlyInAnyOrderEntriesOf(p);
}

private void testProperty(String content, String key, String value) throws IOException {
testProperty(content, key, value, null);
}
Expand Down

0 comments on commit b914286

Please sign in to comment.