Connect minimal hosts to snapshots, target workers and shared Kit editing
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package de.neitzel.fxdesigner.protocol;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Metadata payload format 1. Properties escaping preserves arbitrary labels without class loading.
|
||||
*/
|
||||
public final class PreviewMessages {
|
||||
private PreviewMessages() {}
|
||||
|
||||
public static byte[] encode(Map<String, String> values) throws IOException {
|
||||
var p = new Properties();
|
||||
p.putAll(values);
|
||||
var w = new StringWriter();
|
||||
p.store(w, null);
|
||||
byte[] bytes = w.toString().getBytes(StandardCharsets.UTF_8);
|
||||
if (bytes.length > WireProtocol.MAX_METADATA) throw new IOException("Metadata too large");
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static Map<String, String> decode(byte[] bytes) throws IOException {
|
||||
if (bytes.length > WireProtocol.MAX_METADATA) throw new IOException("Metadata too large");
|
||||
var p =
|
||||
new Properties() {
|
||||
public synchronized Object put(Object k, Object v) {
|
||||
if (containsKey(k)) throw new IllegalArgumentException("Duplicate metadata key");
|
||||
return super.put(k, v);
|
||||
}
|
||||
};
|
||||
p.load(
|
||||
new StringReader(
|
||||
StandardCharsets.UTF_8
|
||||
.newDecoder()
|
||||
.decode(java.nio.ByteBuffer.wrap(bytes))
|
||||
.toString()));
|
||||
var result = new TreeMap<String, String>();
|
||||
for (String k : p.stringPropertyNames()) result.put(k, p.getProperty(k));
|
||||
return Map.copyOf(result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user