1
0
mirror of https://github.com/moparisthebest/k-9 synced 2025-02-07 02:30:10 -05:00

Refactor to improve readability

This commit is contained in:
cketti 2015-01-23 14:56:52 +01:00
parent 6825eafb87
commit 977d15c190

View File

@ -1354,47 +1354,51 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
Body body = part.getBody(); Body body = part.getBody();
if (body instanceof Multipart) { if (body instanceof Multipart) {
cv.put("data_location", DataLocation.IN_DATABASE); multipartToContentValues(cv, (Multipart) body);
} else if (body == null) {
missingPartToContentValues(cv, part);
} else {
leafPartToContentValues(cv, part, body);
}
}
Multipart multipart = (Multipart) body; private void multipartToContentValues(ContentValues cv, Multipart multipart) {
cv.put("data_location", DataLocation.IN_DATABASE);
cv.put("preamble", multipart.getPreamble()); cv.put("preamble", multipart.getPreamble());
cv.put("epilogue", multipart.getEpilogue()); cv.put("epilogue", multipart.getEpilogue());
cv.put("boundary", multipart.getBoundary()); cv.put("boundary", multipart.getBoundary());
} else { }
private void missingPartToContentValues(ContentValues cv, Part part) throws MessagingException {
AttachmentViewInfo attachment = LocalMessageExtractor.extractAttachmentInfo(part, PLACEHOLDER_URI); AttachmentViewInfo attachment = LocalMessageExtractor.extractAttachmentInfo(part, PLACEHOLDER_URI);
cv.put("display_name", attachment.displayName); cv.put("display_name", attachment.displayName);
if (body == null) {
//TODO: deal with missing parts
cv.put("data_location", DataLocation.MISSING); cv.put("data_location", DataLocation.MISSING);
cv.put("decoded_body_size", attachment.size); cv.put("decoded_body_size", attachment.size);
} else { }
private void leafPartToContentValues(ContentValues cv, Part part, Body body)
throws MessagingException, IOException {
AttachmentViewInfo attachment = LocalMessageExtractor.extractAttachmentInfo(part, PLACEHOLDER_URI);
cv.put("display_name", attachment.displayName);
cv.put("data_location", DataLocation.IN_DATABASE); cv.put("data_location", DataLocation.IN_DATABASE);
byte[] bodyData = getBodyBytes(body); byte[] bodyData = getBodyBytes(body);
String encoding = getTransferEncoding(part); String encoding = getTransferEncoding(part);
long size = decodeAndCountBytes(bodyData, encoding); long size = decodeAndCountBytes(bodyData, encoding, bodyData.length);
if (size == AttachmentViewInfo.UNKNOWN_SIZE) {
cv.put("decoded_body_size", bodyData.length);
} else {
cv.put("decoded_body_size", size); cv.put("decoded_body_size", size);
}
cv.put("encoding", encoding); cv.put("encoding", encoding);
cv.put("data", bodyData); cv.put("data", bodyData);
cv.put("content_id", part.getContentId()); cv.put("content_id", part.getContentId());
} }
}
}
private long decodeAndCountBytes(byte[] bodyData, String encoding) { private long decodeAndCountBytes(byte[] bodyData, String encoding, long fallbackValue) {
ByteArrayInputStream rawInputStream = new ByteArrayInputStream(bodyData); ByteArrayInputStream rawInputStream = new ByteArrayInputStream(bodyData);
return decodeAndCountBytes(encoding, rawInputStream); return decodeAndCountBytes(rawInputStream, encoding, fallbackValue);
} }
private long decodeAndCountBytes(String encoding, ByteArrayInputStream rawInputStream) { private long decodeAndCountBytes(ByteArrayInputStream rawInputStream, String encoding, long fallbackValue) {
InputStream decodingInputStream = localStore.getDecodingInputStream(rawInputStream, encoding); InputStream decodingInputStream = localStore.getDecodingInputStream(rawInputStream, encoding);
try { try {
CountingOutputStream countingOutputStream = new CountingOutputStream(); CountingOutputStream countingOutputStream = new CountingOutputStream();
@ -1403,7 +1407,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
return countingOutputStream.getCount(); return countingOutputStream.getCount();
} catch (IOException e) { } catch (IOException e) {
return AttachmentViewInfo.UNKNOWN_SIZE; return fallbackValue;
} }
} finally { } finally {
try { try {