/** Write out a date to the stream as specified by the I2P data structure spec. * @param out stream to write to * @param date date to write (can be null) * @throws DataFormatException if the date is not valid * @throws IOException if there is an IO error writing the date */ public static void writeDate(OutputStream out, Date date) throws DataFormatException, IOException { if (date == null) writeLong(out, DATE_LENGTH, 0L); else writeLong(out, DATE_LENGTH, date.getTime()); }
public void writeBytes(OutputStream out) throws DataFormatException, IOException { if (_type < 0) throw new DataFormatException("Invalid certificate type: " + _type); //if ((_type != 0) && (_payload == null)) throw new DataFormatException("Payload is required for non null type"); out.write((byte) _type); if (_payload != null) { DataHelper.writeLong(out, 2, _payload.length); out.write(_payload); } else { DataHelper.writeLong(out, 2, 0L); } }
public void writeBytes(OutputStream out) throws DataFormatException, IOException { if (_encryptedData == null) throw new DataFormatException("Not yet encrypted. Please set the encrypted data"); DataHelper.writeLong(out, 4, _encryptedData.length); out.write(_encryptedData); //if (_log.shouldLog(Log.DEBUG)) // _log.debug("wrote payload: " + _encryptedData.length); }
@Override public void writeBytes(OutputStream out) throws DataFormatException, IOException { if (_gateway == null) throw new DataFormatException("Not enough data to write out a Lease"); _gateway.writeBytes(out); // flags DataHelper.writeLong(out, 2, 0); out.write(_type); out.write(_cost); DataHelper.writeLong(out, 4, _end.getTime() / 1000); }
@Override protected byte[] doWriteMessage() throws I2CPMessageException, IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(64); try { for (int i = 0; i < LIMITS; i++) { DataHelper.writeLong(os, 4, data[i]); } } catch (DataFormatException dfe) { throw new I2CPMessageException("Error writing out the message data", dfe); } return os.toByteArray(); }
public void writeBytes(OutputStream out) throws DataFormatException, IOException { if (_sessionId < 0) throw new DataFormatException("Invalid session ID: " + _sessionId); DataHelper.writeLong(out, 2, _sessionId); }
public void writeBytes(OutputStream out) throws DataFormatException, IOException { if (_tunnelId < 0) throw new DataFormatException("Invalid tunnel ID: " + _tunnelId); DataHelper.writeLong(out, 4, _tunnelId); }
public void writeBytes(OutputStream out) throws DataFormatException, IOException { if (_messageId < 0) throw new DataFormatException("Invalid message ID: " + _messageId); DataHelper.writeLong(out, 4, _messageId); }
protected void writeHeader(OutputStream out) throws DataFormatException, IOException { _destination.writeBytes(out); if (_published <= 0) _published = Clock.getInstance().now(); long pub1k = _published / 1000; DataHelper.writeLong(out, 4, pub1k); // Divide separately to prevent rounding errors DataHelper.writeLong(out, 2, ((_expires / 1000) - pub1k)); DataHelper.writeLong(out, 2, _flags); if (isOffline()) writeOfflineBytes(out); }
/** * Overridden because we have a blinded key, not a dest */ @Override protected void writeHeader(OutputStream out) throws DataFormatException, IOException { DataHelper.writeLong(out, 2, _signingKey.getType().getCode()); _signingKey.writeBytes(out); if (_published <= 0) _published = Clock.getInstance().now(); DataHelper.writeLong(out, 4, _published / 1000); DataHelper.writeLong(out, 2, (_expires - _published) / 1000); DataHelper.writeLong(out, 2, _flags); if (isOffline()) writeOfflineBytes(out); }
@Override protected byte[] doWriteMessage() throws I2CPMessageException, IOException { if (_sessionId == null) throw new I2CPMessageException("Unable to write out the message as there is not enough data"); ByteArrayOutputStream os = new ByteArrayOutputStream(64); try { _sessionId.writeBytes(os); DataHelper.writeLong(os, 1, _status); } catch (DataFormatException dfe) { throw new I2CPMessageException("Error writing out the message data", dfe); } return os.toByteArray(); }
@Override public void writeBytes(OutputStream out) throws DataFormatException, IOException { if ((_gateway == null) || (_tunnelId == null)) throw new DataFormatException("Not enough data to write out a Lease"); _gateway.writeBytes(out); _tunnelId.writeBytes(out); DataHelper.writeLong(out, 4, _end.getTime() / 1000); }
@Override protected byte[] doWriteMessage() throws I2CPMessageException, IOException { if (_sessionId == null) throw new I2CPMessageException("No data"); ByteArrayOutputStream os = new ByteArrayOutputStream(256); try { _sessionId.writeBytes(os); DataHelper.writeLong(os, 1, _endpoints.size()); for (int i = 0; i < _endpoints.size(); i++) { _endpoints.get(i).writeBytes(os); } } catch (DataFormatException dfe) { throw new I2CPMessageException("Error writing out the message data", dfe); } return os.toByteArray(); }
/** * Write out the full message to the stream, including the 4 byte size and 1 * byte type header. * * @throws IOException */ public void writeMessage(OutputStream out) throws I2CPMessageException, IOException { byte[] data = doWriteMessage(); try { DataHelper.writeLong(out, 4, data.length); out.write((byte) getType()); } catch (DataFormatException dfe) { throw new I2CPMessageException("Unable to write the message length or type", dfe); } out.write(data); }
/** * 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); }
protected void writeOfflineBytes(OutputStream out) throws DataFormatException, IOException { if (_transientSigningPublicKey == null || _offlineSignature == null) throw new DataFormatException("No offline key/sig"); DataHelper.writeLong(out, 4, _transientExpires / 1000); DataHelper.writeLong(out, 2, _transientSigningPublicKey.getType().getCode()); _transientSigningPublicKey.writeBytes(out); _offlineSignature.writeBytes(out); }
/** * Overridden because we have a blinded key, not a dest */ @Override protected void writeOfflineBytes(OutputStream out) throws DataFormatException, IOException { if (_transientSigningPublicKey == null || _offlineSignature == null) throw new DataFormatException("No offline key/sig"); DataHelper.writeLong(out, 4, _transientExpires / 1000); DataHelper.writeLong(out, 2, _signingKey.getType().getCode()); _transientSigningPublicKey.writeBytes(out); _offlineSignature.writeBytes(out); }
/** * Without sig. This does NOT validate the signature */ @Override protected void writeBytesWithoutSig(OutputStream out) throws DataFormatException, IOException { if (_signingKey == null) throw new DataFormatException("Not enough data to write out a LeaseSet"); // LS2 header writeHeader(out); // Encrypted LS2 part if (_encryptedData == null) { // TODO encrypt(null); } DataHelper.writeLong(out, 2, _encryptedData.length); out.write(_encryptedData); }
@Test public void testLong() throws Exception{ byte[] temp = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataHelper.writeLong(baos, 4, 42); temp = baos.toByteArray(); long l; ByteArrayInputStream bais = new ByteArrayInputStream(temp); l = DataHelper.readLong(bais, 4); assertEquals(42, l); } }