session-android/src/org/thoughtcrime/securesms/crypto/MessageDisplayHelper.java

70 lines
2.2 KiB
Java
Raw Normal View History

/**
2011-12-20 18:20:44 +00:00
* Copyright (C) 2011 Whisper Systems
*
2011-12-20 18:20:44 +00:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2011-12-20 18:20:44 +00:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms.crypto;
import org.thoughtcrime.securesms.protocol.Prefix;
import org.thoughtcrime.securesms.util.InvalidMessageException;
import java.lang.ref.SoftReference;
import java.util.LinkedHashMap;
2011-12-20 18:20:44 +00:00
public class MessageDisplayHelper {
2011-12-20 18:20:44 +00:00
private static final int MAX_CACHE_SIZE = 2000;
2011-12-20 18:20:44 +00:00
private static final LinkedHashMap<String,SoftReference<String>> decryptedBodyCache = new LinkedHashMap<String,SoftReference<String>>() {
@Override
protected boolean removeEldestEntry(Entry<String,SoftReference<String>> eldest) {
return this.size() > MAX_CACHE_SIZE;
}
};
private static String checkCacheForBody(String body) {
2013-03-04 02:49:39 +00:00
synchronized (decryptedBodyCache) {
if (decryptedBodyCache.containsKey(body)) {
String decryptedBody = decryptedBodyCache.get(body).get();
if (decryptedBody != null) {
return decryptedBody;
} else {
decryptedBodyCache.remove(body);
return null;
}
}
2013-03-04 02:49:39 +00:00
return null;
}
2011-12-20 18:20:44 +00:00
}
public static String getDecryptedMessageBody(MasterCipher bodyCipher, String body) throws InvalidMessageException {
if (body.startsWith(Prefix.SYMMETRIC_ENCRYPT)) {
String cacheResult = checkCacheForBody(body);
if (cacheResult != null)
return cacheResult;
String decryptedBody = bodyCipher.decryptBody(body.substring(Prefix.SYMMETRIC_ENCRYPT.length()));
2013-03-04 02:49:39 +00:00
synchronized (decryptedBodyCache) {
decryptedBodyCache.put(body, new SoftReference<String>(decryptedBody));
}
return decryptedBody;
2011-12-20 18:20:44 +00:00
}
return body;
}
2013-03-04 02:49:39 +00:00
}