/** * Overridden for efficiency. */ @Override public byte[] toByteArray() { return DataHelper.toLong(4, _tunnelId); }
/** * Overridden for efficiency. */ @Override public byte[] toByteArray() { byte[] rv = DataHelper.toLong(8, _date); rv[0] = (byte) ((_flags >> 8) & 0xff); rv[1] = (byte) (_flags & 0xff); return rv; }
/** * Big endian. * * @param numBytes 1-8 * @param value non-negative * @return an array of length numBytes */ public static byte[] toLong(int numBytes, long value) throws IllegalArgumentException { byte val[] = new byte[numBytes]; toLong(val, 0, numBytes, value); return val; }
public int writeData(byte[] tgt, int off) { DataHelper.toLong(tgt, off, 4, now / 1000); return off + 4; } }
public static void toDate(byte target[], int offset, long when) throws IllegalArgumentException { toLong(target, offset, DATE_LENGTH, when); }
/** write the message body to the output array, starting at the given index */ protected int writeMessageBody(byte out[], int curIndex) throws I2NPMessageException { DataHelper.toLong(out, curIndex, 4, _data.length); curIndex += 4; System.arraycopy(_data, 0, out, curIndex, _data.length); curIndex += _data.length; return curIndex; }
/** * @return the written length (NOT the new offset) */ public int writeBytes(byte target[], int offset) { int cur = offset; DataHelper.toLong(target, cur, 1, _type); cur++; if (_payload != null) { DataHelper.toLong(target, cur, 2, _payload.length); cur += 2; System.arraycopy(_payload, 0, target, cur, _payload.length); cur += _payload.length; } else { DataHelper.toLong(target, cur, 2, 0); cur += 2; } return cur - offset; }
/** write the message body to the output array, starting at the given index */ protected int writeMessageBody(byte out[], int curIndex) { if (_data == null) { out[curIndex++] = 0x0; out[curIndex++] = 0x0; out[curIndex++] = 0x0; out[curIndex++] = 0x0; } else { DataHelper.toLong(out, curIndex, 4, _data.length); curIndex += 4; System.arraycopy(_data, 0, out, curIndex, _data.length); curIndex += _data.length; } return curIndex; }
@Override protected byte[] doWriteMessage() throws I2CPMessageException, IOException { if ((_sessionId < 0) || (_messageId < 0)) throw new I2CPMessageException("Unable to write out the message as there is not enough data"); byte rv[] = new byte[2+4]; DataHelper.toLong(rv, 0, 2, _sessionId); DataHelper.toLong(rv, 2, 4, _messageId); return rv; }
/** write the message body to the output array, starting at the given index */ protected int writeMessageBody(byte out[], int curIndex) throws I2NPMessageException { if ( (_id < 0) || (_arrival <= 0) ) throw new I2NPMessageException("Not enough data to write out"); DataHelper.toLong(out, curIndex, 4, _id); curIndex += 4; DataHelper.toLong(out, curIndex, DataHelper.DATE_LENGTH, _arrival); curIndex += DataHelper.DATE_LENGTH; return curIndex; }
/** * @return the written length (NOT the new offset) */ public int writeBytes(byte target[], int offset) { if (_encryptedData == null) throw new IllegalStateException("Not yet encrypted. Please set the encrypted data"); DataHelper.toLong(target, offset, 4, _encryptedData.length); offset += 4; System.arraycopy(_encryptedData, 0, target, offset, _encryptedData.length); return 4 + _encryptedData.length; }
/** @return new offset */ public int write(byte[] tgt, int off) { tgt[off++] = (byte) type; // we do it this way so we don't call getDataLength(), // which may be inefficient // off is where the length goes int rv = writeData(tgt, off + 2); DataHelper.toLong(tgt, off, 2, rv - (off + 2)); return rv; }
static final String toId(long id) { return Base64.encode(DataHelper.toLong(4, id)).replace("==", ""); }
public void setReceiveTunnelId(TunnelId id) { _receiveTunnelId = DataHelper.toLong(4, id.getTunnelId()); }
/** * Creates 54-byte compact info * @throws IllegalArgumentException */ private void initialize() { if (port <= 0 || port >= 65535) throw new IllegalArgumentException("Bad port"); byte[] compactInfo = new byte[LENGTH]; System.arraycopy(nID.getData(), 0, compactInfo, 0, NID.HASH_LENGTH); System.arraycopy(hash.getData(), 0, compactInfo, NID.HASH_LENGTH, Hash.HASH_LENGTH); DataHelper.toLong(compactInfo, NID.HASH_LENGTH + Hash.HASH_LENGTH, 2, port); setData(compactInfo); }
/** * The Base 64 of 24 bytes: (now, md5 of (now, proxy nonce)) * @since 0.9.4 */ private String getNonce() { byte[] b = new byte[DataHelper.DATE_LENGTH + PROXYNONCE_BYTES]; byte[] n = new byte[NONCE_BYTES]; long now = _context.clock().now(); DataHelper.toLong(b, 0, DataHelper.DATE_LENGTH, now); System.arraycopy(_proxyNonce, 0, b, DataHelper.DATE_LENGTH, PROXYNONCE_BYTES); System.arraycopy(b, 0, n, 0, DataHelper.DATE_LENGTH); byte[] md5 = PasswordManager.md5Sum(b); System.arraycopy(md5, 0, n, DataHelper.DATE_LENGTH, MD5_BYTES); String rv = Base64.encode(n); _nonces.putIfAbsent(rv, new NonceInfo(now + MAX_NONCE_AGE)); return rv; }
/** write the message body to the output array, starting at the given index */ protected int writeMessageBody(byte out[], int curIndex) throws I2NPMessageException { if (_now <= 0) throw new I2NPMessageException("Not enough data to write out"); DataHelper.toLong(out, curIndex, DataHelper.DATE_LENGTH, _now); curIndex += DataHelper.DATE_LENGTH; return curIndex; }
/** * Write the message with a short 5-byte header. * THe header consists of a one-byte type and a 4-byte expiration in seconds only. * Used by SSU only! * * @return the new written length */ public int toRawByteArray(byte buffer[]) { try { int off = 0; buffer[off++] = (byte) getType(); // January 19 2038? No, unsigned, good until Feb. 7 2106 // in seconds, round up so we don't lose time every hop DataHelper.toLong(buffer, off, 4, (_expiration + 500) / 1000); off += 4; return writeMessageBody(buffer, off); } catch (I2NPMessageException ime) { _context.logManager().getLog(getClass()).log(Log.CRIT, "Error writing", ime); throw new IllegalStateException("Unable to serialize the message " + getClass().getSimpleName(), ime); } }
private static void checkLong(int numBytes, long value) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(numBytes); DataHelper.writeLong(baos, numBytes, value); byte written[] = baos.toByteArray(); byte extract[] = DataHelper.toLong(numBytes, value); assertTrue(extract.length == numBytes); assertTrue(DataHelper.eq(written, extract)); byte extract2[] = new byte[numBytes]; DataHelper.toLong(extract2, 0, numBytes, value); assertTrue(DataHelper.eq(extract, extract2)); long read = DataHelper.fromLong(extract, 0, numBytes); assertTrue(read == value); ByteArrayInputStream bais = new ByteArrayInputStream(written); read = DataHelper.readLong(bais, numBytes); assertTrue(read == value); read = DataHelper.fromLong(written, 0, numBytes); assertTrue(read == value); }