/** * Write a mapping to the stream, as defined by the I2P data structure spec, * and store it into a Properties object. See readProperties for the format. * Output is sorted by property name. * Property keys and values must not contain '=' or ';', this is not checked and they are not escaped * Keys and values must be 255 bytes or less, * Formatted length must not exceed 65535 bytes * * Properties from the defaults table of props (if any) are not written out by this method. * * @param rawStream stream to write to * @param props properties to write out, may be null * @throws DataFormatException if there is not enough valid data to write out, * or a length limit is exceeded * @throws IOException if there is an IO error writing out the data */ public static void writeProperties(OutputStream rawStream, Properties props) throws DataFormatException, IOException { writeProperties(rawStream, props, false); }
/** * Writes the props to the stream, sorted by property name. * See readProperties() for the format. * Property keys and values must not contain '=' or ';', this is not checked and they are not escaped * Keys and values must be 255 bytes or less, * Formatted length must not exceed 65535 bytes * * Properties from the defaults table of props (if any) are not written out by this method. * * jrandom disabled UTF-8 in mid-2004, for performance reasons, * i.e. slow foo.getBytes("UTF-8") * Re-enable it so we can pass UTF-8 tunnel names through the I2CP SessionConfig. * * Use utf8 = false for RouterAddress (fast, non UTF-8) * Use utf8 = true for SessionConfig (slow, UTF-8) * @param props source may be null * @throws DataFormatException if a length limit is exceeded */ public static void writeProperties(OutputStream rawStream, Properties props, boolean utf8) throws DataFormatException, IOException { writeProperties(rawStream, props, utf8, props != null && props.size() > 1 && !(props instanceof OrderedProperties)); }
/** * Writes the props to returned byte array, not sorted * (unless the opts param is an OrderedProperties) * Strings will be UTF-8 encoded in the byte array. * See readProperties() for the format. * Property keys and values must not contain '=' or ';', this is not checked and they are not escaped * Keys and values must be 255 bytes or less, * Formatted length must not exceed 65535 bytes * Warning - confusing method name, Properties is the source. * * Properties from the defaults table of props (if any) are not written out by this method. * * @throws DataFormatException if key, value, or total is too long */ public static byte[] toProperties(Properties opts) throws DataFormatException { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(2 + (32 * opts.size())); writeProperties(baos, opts, true, false); return baos.toByteArray(); } catch (IOException ioe) { throw new RuntimeException("IO error writing to memory?! " + ioe.getMessage()); } }
@Override protected byte[] doWriteMessage() throws I2CPMessageException, IOException { if (_version == null) return new byte[0]; ByteArrayOutputStream os = new ByteArrayOutputStream(_options != null ? 128 : 16); try { DataHelper.writeString(os, _version); if (_options != null && !_options.isEmpty()) DataHelper.writeProperties(os, _options, true); // UTF-8 } catch (DataFormatException dfe) { throw new I2CPMessageException("Error writing out the message data", dfe); } return os.toByteArray(); }
/** * A format error on the properties is non-fatal (only the properties are lost) * A format error on the destination is fatal */ public byte[] getBytes(DestEntry de) { ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); try { try { DataHelper.writeProperties(baos, de.props, true, false); // UTF-8, unsorted } catch (DataFormatException dfe) { logError("DB Write Fail - properties too big?", dfe); // null properties is a two-byte length of 0. baos.write(new byte[2]); } de.dest.writeBytes(baos); } catch (IOException ioe) { logError("DB Write Fail", ioe); } catch (DataFormatException dfe) { logError("DB Write Fail", dfe); } return baos.toByteArray(); }
/** * As of 0.9.3, expiration MUST be all zeros as it is ignored on * readin and the signature will fail. */ public void writeBytes(OutputStream out) throws DataFormatException, IOException { if (_transportStyle == null) throw new DataFormatException("uninitialized"); out.write((byte) _cost); DataHelper.writeLong(out, 8, _expiration); DataHelper.writeString(out, _transportStyle); DataHelper.writeProperties(out, _options); }
/** * Without sig. This does NOT validate the signature */ @Override protected void writeBytesWithoutSig(OutputStream out) throws DataFormatException, IOException { if (_destination == null) throw new DataFormatException("Not enough data to write out a LeaseSet"); // LS2 header writeHeader(out); // Meta LS2 part if (_options != null && !_options.isEmpty()) { DataHelper.writeProperties(out, _options); } else { DataHelper.writeLong(out, 2, 0); } out.write(_leases.size()); for (Lease lease : _leases) { lease.writeBytes(out); } // revocations out.write(0); }
public void writeBytes(OutputStream out) throws DataFormatException, IOException { if ((_destination == null) || (_options == null) || (_signature == null) || (_creationDate == null)) throw new DataFormatException("Not enough data to create the session config"); _destination.writeBytes(out); DataHelper.writeProperties(out, _options, true); // UTF-8 DataHelper.writeDate(out, _creationDate); _signature.writeBytes(out); }
@Test public void testProperties() throws Exception{ byte[] temp = null; Properties orig = new Properties(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); orig.setProperty("key1", "val1"); orig.setProperty("key2", "val2"); orig.setProperty("key3", "val3"); DataHelper.writeProperties(baos, orig); temp = baos.toByteArray(); Properties p = null; ByteArrayInputStream bais = new ByteArrayInputStream(temp); p = DataHelper.readProperties(bais); assertEquals(orig, p); } }
DataHelper.writeProperties(out, _options);
/** * Without sig. This does NOT validate the signature */ protected void writeBody(OutputStream out) throws DataFormatException, IOException { if (_options != null && !_options.isEmpty()) { DataHelper.writeProperties(out, _options); } else { DataHelper.writeLong(out, 2, 0); } List<PublicKey> keys = getEncryptionKeys(); out.write((byte) keys.size()); for (PublicKey key : keys) { EncType type = key.getType(); if (type != null) { DataHelper.writeLong(out, 2, type.getCode()); } else { DataHelper.writeLong(out, 2, key.getUnknownTypeCode()); } DataHelper.writeLong(out, 2, key.length()); key.writeBytes(out); } out.write((byte) _leases.size()); for (Lease lease : _leases) { lease.writeBytes(out); } }
private byte[] getBytes() { if (_destination == null) return null; if (_options == null) return null; if (_creationDate == null) return null; ByteArrayOutputStream out = new ByteArrayOutputStream(); try { //_log.debug("PubKey size for destination: " + _destination.getPublicKey().getData().length); //_log.debug("SigningKey size for destination: " + _destination.getSigningPublicKey().getData().length); _destination.writeBytes(out); DataHelper.writeProperties(out, _options, true); // UTF-8 DataHelper.writeDate(out, _creationDate); } catch (IOException ioe) { Log log = I2PAppContext.getGlobalContext().logManager().getLog(SessionConfig.class); log.error("IOError signing", ioe); return null; } catch (DataFormatException dfe) { Log log = I2PAppContext.getGlobalContext().logManager().getLog(SessionConfig.class); log.error("Error writing out the bytes for signing/verification", dfe); return null; } return out.toByteArray(); }