@Override public Map<String, String> parseBytes(byte[] buf) { if (buf == null) throw new NullPointerException("buf == null"); // programming error if (buf.length == 0) return Collections.emptyMap(); Buffer bytes = new Buffer(buf); byte version = bytes.readByte(); if (version != VERSION) { Platform.get().log("Invalid input: unsupported version {0}", version, null); return null; } Map<String, String> result = new LinkedHashMap<>(); while (bytes.remaining() > 3) { // tag for field ID and two lengths if (bytes.readByte() == TAG_FIELD_ID) { String key = bytes.readLengthPrefixed(); if (key == null) break; String val = bytes.readLengthPrefixed(); if (val == null) break; result.put(key, val); } else { Platform.get().log("Invalid input: expected TAG_FIELD_ID at offset {0}", bytes.pos, null); break; } } return result; }
@Override public byte[] toBytes(Map<String, String> tagContext) { if (tagContext == null) { throw new NullPointerException("tagContext == null"); // programming error } if (tagContext.isEmpty()) return EMPTY_BYTES; byte[] result = new byte[sizeInBytes(tagContext)]; Buffer bytes = new Buffer(result); bytes.writeByte(VERSION); for (Map.Entry<String, String> entry : tagContext.entrySet()) { bytes.writeByte(TAG_FIELD_ID); bytes.writeLengthPrefixed(entry.getKey()); bytes.writeLengthPrefixed(entry.getValue()); } return result; }
@Override public Map<String, String> parseBytes(byte[] buf) { if (buf == null) throw new NullPointerException("buf == null"); // programming error if (buf.length == 0) return Collections.emptyMap(); Buffer bytes = new Buffer(buf); byte version = bytes.readByte(); if (version != VERSION) { Platform.get().log("Invalid input: unsupported version {0}", version, null); return null; } Map<String, String> result = new LinkedHashMap<>(); while (bytes.remaining() > 3) { // tag for field ID and two lengths if (bytes.readByte() == TAG_FIELD_ID) { String key = bytes.readLengthPrefixed(); if (key == null) break; String val = bytes.readLengthPrefixed(); if (val == null) break; result.put(key, val); } else { Platform.get().log("Invalid input: expected TAG_FIELD_ID at offset {0}", bytes.pos, null); break; } } return result; }
@Override public byte[] toBytes(Map<String, String> tagContext) { if (tagContext == null) { throw new NullPointerException("tagContext == null"); // programming error } if (tagContext.isEmpty()) return EMPTY_BYTES; byte[] result = new byte[sizeInBytes(tagContext)]; Buffer bytes = new Buffer(result); bytes.writeByte(VERSION); for (Map.Entry<String, String> entry : tagContext.entrySet()) { bytes.writeByte(TAG_FIELD_ID); bytes.writeLengthPrefixed(entry.getKey()); bytes.writeLengthPrefixed(entry.getValue()); } return result; }
String readAsciiString(int length) { if (length == -1 || remaining() < length) return null; char[] string = new char[length]; for (int i = 0; i < length; i++) { string[i] = (char) buf[pos++]; } return new String(string); } }
String readLengthPrefixed() { byte b1 = buf[pos++]; if (b1 >= 0) { // negative means MSB set return readAsciiString(b1); } return readAsciiString(readVarint(b1)); }
String readAsciiString(int length) { if (length == -1 || remaining() < length) return null; char[] string = new char[length]; for (int i = 0; i < length; i++) { string[i] = (char) buf[pos++]; } return new String(string); } }
String readLengthPrefixed() { byte b1 = buf[pos++]; if (b1 >= 0) { // negative means MSB set return readAsciiString(b1); } return readAsciiString(readVarint(b1)); }