60 lines
1.6 KiB
Java
60 lines
1.6 KiB
Java
package org.whispersystems.test;
|
|
|
|
import org.whispersystems.libaxolotl.InvalidKeyIdException;
|
|
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
|
|
import org.whispersystems.libaxolotl.state.SignedPreKeyStore;
|
|
|
|
import java.io.IOException;
|
|
import java.util.HashMap;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
public class InMemorySignedPreKeyStore implements SignedPreKeyStore {
|
|
|
|
private final Map<Integer, byte[]> store = new HashMap<>();
|
|
|
|
@Override
|
|
public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException {
|
|
try {
|
|
if (!store.containsKey(signedPreKeyId)) {
|
|
throw new InvalidKeyIdException("No such signedprekeyrecord! " + signedPreKeyId);
|
|
}
|
|
|
|
return new SignedPreKeyRecord(store.get(signedPreKeyId));
|
|
} catch (IOException e) {
|
|
throw new AssertionError(e);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public List<SignedPreKeyRecord> loadSignedPreKeys() {
|
|
try {
|
|
List<SignedPreKeyRecord> results = new LinkedList<>();
|
|
|
|
for (byte[] serialized : store.values()) {
|
|
results.add(new SignedPreKeyRecord(serialized));
|
|
}
|
|
|
|
return results;
|
|
} catch (IOException e) {
|
|
throw new AssertionError(e);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void storeSignedPreKey(int signedPreKeyId, SignedPreKeyRecord record) {
|
|
store.put(signedPreKeyId, record.serialize());
|
|
}
|
|
|
|
@Override
|
|
public boolean containsSignedPreKey(int signedPreKeyId) {
|
|
return store.containsKey(signedPreKeyId);
|
|
}
|
|
|
|
@Override
|
|
public void removeSignedPreKey(int signedPreKeyId) {
|
|
store.remove(signedPreKeyId);
|
|
}
|
|
}
|