private static byte[] textFromProperties(Map<String, ?> props) { byte[] text = null; if (props != null) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(256); for (String key : props.keySet()) { Object val = props.get(key); ByteArrayOutputStream out2 = new ByteArrayOutputStream(100); writeUTF(out2, key); if (val == null) { // Skip } else if (val instanceof String) { out2.write('='); writeUTF(out2, (String) val); } else if (val instanceof byte[]) { byte[] bval = (byte[]) val; if (bval.length > 0) { out2.write('='); out2.write(bval, 0, bval.length); } else { val = null; } } else { throw new IllegalArgumentException("invalid property value: " + val); } byte data[] = out2.toByteArray(); if (data.length > 255) { throw new IOException( "Cannot have individual values larger that 255 chars. Offending value: " + key + (val != null ? "" : "=" + val)); } out.write((byte) data.length); out.write(data, 0, data.length); } text = out.toByteArray(); } catch (IOException e) { throw new RuntimeException("unexpected exception: " + e); } } return (text != null && text.length > 0 ? text : DNSRecord.EMPTY_TXT); }
ServiceInfoImpl( Map<Fields, String> qualifiedNameMap, int port, int weight, int priority, boolean persistent, String text) { this(qualifiedNameMap, port, weight, priority, persistent, (byte[]) null); _server = text; try { ByteArrayOutputStream out = new ByteArrayOutputStream(text.length()); writeUTF(out, text); this._text = out.toByteArray(); } catch (IOException e) { throw new RuntimeException("unexpected exception: " + e); } }
/** * @param type * @param name * @param subtype * @param port * @param weight * @param priority * @param persistent * @param text * @see javax.jmdns.ServiceInfo#create(String, String, int, int, int, String) */ public ServiceInfoImpl( String type, String name, String subtype, int port, int weight, int priority, boolean persistent, String text) { this( ServiceInfoImpl.decodeQualifiedNameMap(type, name, subtype), port, weight, priority, persistent, (byte[]) null); _server = text; byte[] encodedText = null; try { ByteArrayOutputStream out = new ByteArrayOutputStream(256); ByteArrayOutputStream out2 = new ByteArrayOutputStream(100); writeUTF(out2, text); byte data[] = out2.toByteArray(); if (data.length > 255) { throw new IOException( "Cannot have individual values larger that 255 chars. Offending value: " + text); } out.write((byte) data.length); out.write(data, 0, data.length); encodedText = out.toByteArray(); } catch (IOException e) { throw new RuntimeException("unexpected exception: " + e); } this._text = (encodedText != null && encodedText.length > 0 ? encodedText : DNSRecord.EMPTY_TXT); }